如何捕捉到socket编程的发送和接收提琴手的请求(how to catch the socket

2019-10-22 18:25发布

我有用于创建在C ++插座代码运行罚款。代码是代码:

#include <winsock2.h>
#include <WS2tcpip.h>
#include <windows.h>
#include <iostream>    
#pragma comment(lib,"ws2_32.lib") using namespace std;
int main (){
// Initialize Dependencies to the Windows Socket.
WSADATA wsaData;
if (WSAStartup(MAKEWORD(2,2), &wsaData) != 0) {
  cout << "WSAStartup failed.\n";
  system("pause");
  return -1;
}

// We first prepare some "hints" for the "getaddrinfo" function
// to tell it, that we are looking for a IPv4 TCP Connection.
struct addrinfo hints;
ZeroMemory(&hints, sizeof(hints));
hints.ai_family = AF_INET;          // We are targeting IPv4
hints.ai_protocol = IPPROTO_TCP;    // We are targeting TCP
hints.ai_socktype = SOCK_STREAM;    // We are targeting TCP so its SOCK_STREAM

// Aquiring of the IPv4 address of a host using the newer
// "getaddrinfo" function which outdated "gethostbyname".
// It will search for IPv4 addresses using the TCP-Protocol.
struct addrinfo* targetAdressInfo = NULL;
DWORD getAddrRes = getaddrinfo("www.google.com", NULL, &hints, &targetAdressInfo);
if (getAddrRes != 0 || targetAdressInfo == NULL)
{
  cout << "Could not resolve the Host Name" << endl;
  system("pause");
  WSACleanup();
  return -1;
}

// Create the Socket Address Informations, using IPv4
// We dont have to take care of sin_zero, it is only used to extend the length of SOCKADDR_IN to the size of SOCKADDR
SOCKADDR_IN sockAddr;
sockAddr.sin_addr = ((struct sockaddr_in*) targetAdressInfo->ai_addr)->sin_addr;    // The IPv4 Address from the Address Resolution Result
sockAddr.sin_family = AF_INET;  // IPv4
sockAddr.sin_port = htons(80);  // HTTP Port: 80

// We have to free the Address-Information from getaddrinfo again
freeaddrinfo(targetAdressInfo);

// Creation of a socket for the communication with the Web Server,
// using IPv4 and the TCP-Protocol
SOCKET webSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (webSocket == INVALID_SOCKET)
{
  cout << "Creation of the Socket Failed" << endl;
  system("pause");
  WSACleanup();
  return -1;
}

// Establishing a connection to the web Socket
cout << "Connecting...\n";
if(connect(webSocket, (SOCKADDR*)&sockAddr, sizeof(sockAddr)) != 0)
{
  cout << "Could not connect";
  system("pause");
  closesocket(webSocket);
  WSACleanup();
  return -1;
}
cout << "Connected.\n";

// Sending a HTTP-GET-Request to the Web Server
const char* httpRequest = "GET / HTTP/1.1\r\nHost: www.google.com\r\nConnection: close\r\n\r\n";
int sentBytes = send(webSocket, httpRequest, strlen(httpRequest),0);
if (sentBytes < strlen(httpRequest) || sentBytes == SOCKET_ERROR)
{
  cout << "Could not send the request to the Server" << endl;
  system("pause");
  closesocket(webSocket);
  WSACleanup();
  return -1;
}

// Receiving and Displaying an answer from the Web Server
char buffer[10000];
ZeroMemory(buffer, sizeof(buffer));
int dataLen;
while ((dataLen = recv(webSocket, buffer, sizeof(buffer), 0) > 0))
{
  int i = 0;
  while (buffer[i] >= 32 || buffer[i] == '\n' || buffer[i] == '\r') {
    cout << buffer[i];
    i += 1;
  }
}

// Cleaning up Windows Socket Dependencies
 closesocket(webSocket);
 WSACleanup();

 system("pause");
 return 0; }

我想同时发送和接收请求,但菲德勒不抓它来捕捉在提琴手的请求和响应。 提前致谢

Answer 1:

的Fiddler自身插入到堆作为HTTP代理服务器。 它依赖于Web浏览器要认识到的是PC上配置了代理,并通过发送。 您的代码未检测到一个代理通过发送 - 所以提琴手将无法监控您的流量。

您有几种选择。

  1. 既然你自己的Windows,只是使用直接套接字使用开关的WinInet HTTP API。 它会为你自动代理检测,您无需考虑一下。 它会做代理认证,以及如果其所需。

  2. 要么。 使用Wireshark的或的NetMon来分析您的流量,而不是提琴手。

我建议#1,因为这意味着你的代码将在一个真正的代理服务器的存在工作(在企业网络中常见的)和提琴手将只使用它。

我想,有一个第三个选项,您自动检测浏览器的代理服务器设置,然后创建一个套接字代理,说话HTTP代理协议,等等......但是这不是最好的做法。



文章来源: how to catch the socket programming's send and receive request in fiddler