Http 代理穿透

原理

HTTP代理服务器中能够提供一种HTTP CONNECT代理服务,能够允许用户建立TCP连接到任何端口。通过CONNECT方法穿透代理的实现方法为:

CONNECT代理服务器的代理端口(如:8080);如果成功返回就可以按照正常的Socket进行通讯。当然在此之前需要先获得代理相关的信息。

如何获取HTTP代理相关信息

程序中是通过读取注册表来获取计算机HTTP代理相关信息的,在注册表的\\Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings键值下就保存着HTTP代理服务器及端口等信息,

根据该键值下的ProxyEnable值可以判断是否有HTTP代理,没有代理的情况下ProxyEnable值为0。

当然一般情况下程序去中访问该键值可能是受限的,所有就需要获取到访问令牌不受限制的UserSid,使用该UserSid去读取HTTP代理信息。

cpp 复制代码
char tmpBuffer[128] = { 0 };
	::sprintf(tmpBuffer, "CONNECT %s:%d HTTP/1.1\r\nUser-Agent: Mozilla/4.0\r\nConnection: Keep-Alive\r\n\r\n", ip.c_str(), port);
	Send(tmpBuffer, strlen(tmpBuffer));
	Receive(tmpBuffer, sizeof(tmpBuffer));

实现

头文件

cpp 复制代码
#pragma once

#include <string>
#include <vector>

using namespace std;

class addrinfo;

class CHttpProxy
{
public:
	CHttpProxy(std::string ip, unsigned short port)
		:m_proxyIp(ip), m_proxyPort(port), m_socket(-1)
	{}

	~CHttpProxy(void);

	void ConnectProxyServer();
	void ConnectServer(std::string ip, unsigned short port);

	bool Send(const char* buf, int len);
	int Receive(char* buf, int bufLen);

	int GetSocket()
	{
		return m_socket;
	}
private:
	std::string m_proxyIp;
	addrinfo* m_addrinfo = nullptr;
	unsigned short m_proxyPort;
	int m_socket;
};

实现

cpp 复制代码
#include "HttpProxy.h"
#include <string.h>

#include <time.h>
#ifdef WIN32
#include <WS2tcpip.h>
#else 
#include <unistd.h>
#include <netdb.h> 
#include <arpa/inet.h>
#include <sys/types.h>          /* See NOTES */
#include <sys/socket.h>
#include <netinet/in.h>  
#define INVALID_SOCKET -1
#endif

CHttpProxy::~CHttpProxy(void)
{
	if (m_addrinfo)
	{
		freeaddrinfo(m_addrinfo);
	}
}
void CHttpProxy::ConnectProxyServer()
{
	struct sockaddr_in addr;
	m_socket = socket(AF_INET, SOCK_STREAM, 0);
	if (m_socket == INVALID_SOCKET)
	{
		throw CException("Error on creating socket fd");
	}
	
	addrinfo inAddrInfo = { 0 };
	inAddrInfo.ai_family = AF_INET;
	inAddrInfo.ai_socktype = SOCK_STREAM;

	std::string strPort = std::to_string(m_proxyPort);
	if (getaddrinfo(m_proxyIp.c_str(), strPort.c_str(), &inAddrInfo, &m_addrinfo) != 0) // error occurs
	{
		throw CException("Error on calling getadrrinfo().");
	}

	if (connect(m_socket, m_addrinfo->ai_addr, m_addrinfo->ai_addrlen))
	{
		throw CException("Error on calling connect().");
	}
}

void CHttpProxy::ConnectServer(std::string ip, unsigned short port)
{
	if (m_socket == INVALID_SOCKET)
	{
		throw CException("Invalid socket fd");
	}
	char tmpBuffer[128] = { 0 };
	::sprintf(tmpBuffer, "CONNECT %s:%d HTTP/1.1\r\nUser-Agent: Mozilla/4.0\r\nConnection: Keep-Alive\r\n\r\n", ip.c_str(), port);
	Send(tmpBuffer, strlen(tmpBuffer));
	Receive(tmpBuffer, sizeof(tmpBuffer));

	int mj, mi, code;
	::sscanf(tmpBuffer, "HTTP/%d.%d %d", &mj, &mi, &code);
	if (code != 200)
	{
		throw CException("http connect emial server faild");
	}
}

int CHttpProxy::Receive(char* buf, int bufLen)
{
	return recv(m_socket, buf, bufLen, 0);
}

bool CHttpProxy::Send(const char* buf, int len)
{
	long ilen = len;
	int sendCnt = 0;
	int ret;

	while (sendCnt < ilen)
	{
		if ((ret = send(m_socket, buf + sendCnt, ilen - sendCnt, 0)) == INVALID_SOCKET)
		{
			return false;
		}
		else
		{
			sendCnt += ret;
		}
	}

	return true;
}
相关推荐
雅欣鱼子酱2 小时前
USB Type-C PD取电(诱骗,诱电,SINK),筋膜枪专用取电芯片
网络·人工智能·芯片·电子元器件
AI大模型应用之禅10 小时前
全球股市估值与可持续农业垂直种植技术的关系
网络·ai
掘根10 小时前
【仿Muduo库项目】HTTP模块2——HttpRequest子模块,HttpResponse子模块
网络·网络协议·http
MOON404☾13 小时前
006.Backdoor后门编写
网络·安全·网络安全·系统安全
ha204289419413 小时前
Linux操作系统学习记录之---TcpSocket
linux·网络·c++·学习
李白你好14 小时前
Burp Suite插件 | 高级HTTP头操作工具
网络·网络协议·http
石像鬼₧魂石14 小时前
22端口(OpenSSH 4.7p1)渗透测试完整复习流程(含实战排错)
大数据·网络·学习·安全·ubuntu
xwj_86557433215 小时前
HTTP协议、接口测试流程、接口测试流程&文档&用例
网络·网络协议·http
RisunJan16 小时前
Linux命令-iptables-restore命令(从文件批量恢复 iptables 防火墙规则)
linux·运维·网络
..过云雨17 小时前
网络计算器实现 - 自定义套接字+序列化+守护进程
网络·网络协议·tcp/ip