Home > Back-end >  Windows 7 can use TCP RAW_SOCKET do half connection scanning?
Windows 7 can use TCP RAW_SOCKET do half connection scanning?

Time:09-22

I use in Windows 7 environment RAW_SOCKET do TCP connection scanning, using sendto function sends a TCP syn requests, sendto function return value indicates that has been sent successfully, but wireshark has not caught the corresponding packet, I don't know why, bother everybody to help! Code is I find from the Internet:
[code=c # include & lt; the stdio. H>
#include
#include
#include
# include "mstcpip. H"
# pragma comment (lib, "WS2_32. Lib")
//define the IP address of the target host
Char * DestIpAddr="10.8.180.125";
Char * SourceIpAddr="10.8.184.157";
//define the IP header
Typedef struct IpHeader
{
Unsigned char Version_HLen;//the length of the first IP version
Unsigned char TOS.//service type TOS
Unsigned short Length; The total length//
Unsigned short Ident.//logo
Unsigned short Flags_Offset;//sign a
Unsigned char TTL.//survival time TTL
Unsigned char Protocol;//agreement
Unsigned short Checksum.//IP header checksum
Unsigned int SourceAddr;//the source IP address
Unsigned int DestinationAddr;//destination IP address
} Ip_Header;
//TCP marks a
# define URG 0 x20
# define ACK 0 x10
# define PSH 0 x08
# define RST 0 x04
# define the SYN 0 x02
# define FIN 0 x01
//define TCP header
Typedef struct TcpHeader
{
USHORT SrcPort;//16 source port
USHORT DstPort;//16 purpose port
Unsigned int SequenceNum;//a 32-bit number
Unsigned int Acknowledgment;//32-bit confirmation number
Unsigned char Hdrlen;//the length of the first
Unsigned char Flags;//six flags
USHORT AdvertisedWindow;//16 window size
USHORT Checksum.//16 bits checksum
USHORT UrgPtr;//16 emergency pointer
} Tcp_Header;
//function reference
//analysis data packet
Int PacketAnalyzer (char *);
//send data
Int SendTCPSYNPacket (int);
//the main function
Int main (int arg c, char * * argv)
{
//port
Int PortStart=80;
//the end port
Int PortEnd=90;
//socket
The SOCKET RecSocket;
Int Result;
Char RecvBuf [65535]={0};
The frequency of the timer//
LARGE_INTEGER nFreq;
Char Name [255].
//start get timer values
LARGE_INTEGER StartTime.
//terminate the timer values
LARGE_INTEGER EndTime;
HANDLE hCon;
WSADATA wsaData;
DWORD dwBufferLen [10].
DWORD dwBufferInLen=1;
DWORD dwBytesReturned=0;
Struct hostent * pHostent;
//initialize the SOCKET
Result=WSAStartup (MAKEWORD (2, 1), & amp; WsaData);
If (Result==SOCKET_ERROR)
{
Printf (" WSAStartup failed with the error % d \ n ", Result);
Return 0;
}
//create the receiving data of socket
RecSocket=socket (AF_INET SOCK_RAW, IPPROTO_IP);
If (Result==SOCKET_ERROR)
{
Printf (" socket failed with the error % d \ n ", WSAGetLastError ());
Closesocket (RecSocket);
Return 0;
}
//get the machine IP address
Result=gethostname (Name, 255);
If (Result==SOCKET_ERROR)
{
Printf (" gethostname1 failed with the error % d \ n ", WSAGetLastError ());
Closesocket (RecSocket);
Return 0;
}
PHostent=(struct hostent *) malloc (sizeof (struct hostent));
PHostent=gethostbyname (Name);
SOCKADDR_IN sock.
The sock. Sin_family=AF_INET;
The sock. Sin_port=htons (5555);
Memcpy (& amp; The sock. Sin_addr. S_un S_addr, pHostent - & gt; H_addr_list [0], pHostent - & gt; H_length);
//bind socket
Result=bind (RecSocket, (PSOCKADDR) & amp; The sock, sizeof (sock));
If (Result==SOCKET_ERROR)
{
Printf (" bind failed with the error % d \ n ", WSAGetLastError ());
Closesocket (RecSocket);
Return 0;
}
//set the SOCK_RAW to SIO_RCVALL
Result=WSAIoctl (
RecSocket,
SIO_RCVALL,//receive all IP packets
& DwBufferInLen,
Sizeof (dwBufferInLen),
& DwBufferLen,
Sizeof (dwBufferLen),
& DwBytesReturned, NULL, NULL);
If (Result==SOCKET_ERROR)
{
Printf (" WSAIoctl failed with the error % d \ n ", WSAGetLastError ());
Closesocket (RecSocket);
Return 0;
}
HCon=GetStdHandle (STD_OUTPUT_HANDLE);
CONSOLE_SCREEN_BUFFER_INFO bInfo;
GetConsoleScreenBufferInfo (hCon, & amp; BInfo);
If (QueryPerformanceFrequency (& amp; NFreq))
{
//get the timer values
The QueryPerformanceCounter (& amp; StartTime);
//loop scanning each port
For (int p=PortStart; P<=PortEnd; P++)
{
//send the tectonic tcpsyn packet
SendTCPSYNPacket (p);
//loop to monitor whether there is a packet arrived at
While (true)
{
Memset (RecvBuf, 0, sizeof (RecvBuf));
Result=recv (RecSocket, RecvBuf, sizeof (RecvBuf), 0).
If (Result==SOCKET_ERROR)
{
Printf (" recv failed with the error % d \ n ", WSAGetLastError ());
Closesocket (RecSocket);
Return 0;
}
//analysis data packet
Result=PacketAnalyzer (RecvBuf);
If (Result==0)
{
continue;
}
The else
{
break;
}
}
SetConsoleTextAttribute hCon, (14);
The QueryPerformanceCounter (& amp; The EndTime);
}//end for
}
//time
Double fInterval=EndTime. QuadPart - StartTime. QuadPart;
Printf (" Total Time: % FFMS \ n ", fInterval * 1000/(double) nFreq QuadPart);
SetConsoleTextAttribute (hCon, bInfo. WAttributes);
//close the socket
If (closesocket (RecSocket)==SOCKET_ERROR)
{
Printf (" closesocket failed with the error % d \ n ", WSAGetLastError ());
Return 0;
}
If (WSACleanup ()==SOCKET_ERROR)
{
Printf (" WSACleanup failed with the error % d \ n ", WSAGetLastError ());
Return 0;
}
return 1;
}
//calculating checksum
USHORT checksum (USHORT * buffer, int size)
{
Unsigned long cksum=0; nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related