I want drop packet by C++

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
<span style="font-family:Verdana,Arial,sans-serif; font-size:13px; line-height:normal Currently, I have captured packet network card using C++ by Winsocks<br style="font-family:Verdana,Arial,sans-serif; font-size:13px; line-height:normal
<span style="font-family:Verdana,Arial,sans-serif; font-size:13px; line-height:normal So ,I want to drop packets using C ++ based on the destination IP address<br style="font-family:Verdana,Arial,sans-serif; font-size:13px; line-height:normal
<span style="font-family:Verdana,Arial,sans-serif; font-size:13px; line-height:normal help me
<span style="font-family:Verdana,Arial,sans-serif; font-size:13px; line-height:normal my code :
#include "stdafx.h"<br/>
#include <conio.h><br/>
#include <string><br/>
#include <cstring><br/>
#include <stdio.h><br/>
#include <iostream><br/>
#define MAX_PACKET_SIZE 65525<br/>
#include <winsock2.h><br/>
#include <mstcpip.h><br/>
#include <ws2tcpip.h>
using namespace std;
typedef struct iphdr1<br/>
{<br/>
<span style="white-space:pre unsigned char VerIHL; //Version and IP Header Length<br/>
<span style="white-space:pre unsigned char Tos;<br/>
<span style="white-space:pre unsigned short Total_len;<br/>
<span style="white-space:pre unsigned short ID;<br/>
<span style="white-space:pre unsigned short Flags_and_Frags; //Flags 3 bits and Fragment offset 13 bits<br/>
<span style="white-space:pre unsigned char TTL;<br/>
<span style="white-space:pre unsigned char Protocol;<br/>
<span style="white-space:pre unsigned short Checksum;<br/>
<span style="white-space:pre unsigned long SrcIP;<br/>
<span style="white-space:pre unsigned long DstIP;<br/>
<span style="white-space:pre //unsigned long Options_and_Padding;<br/>
} IpHeader1;<br/>
<br/>
typedef struct port<br/>
{<br/>
<span style="white-space:pre unsigned short SrcPort;<br/>
<span style="white-space:pre unsigned short DstPort;<br/>
} TcpUdpPort;<br/>
<br/>
void ProcessPacket(char* Buffer, int Size)<br/>
{<br/>
<span style="white-space:pre IpHeader1 *iphdr1;<br/>
<span style="white-space:pre TcpUdpPort *port;<br/>
<span style="white-space:pre struct sockaddr_in SockAddr;<br/>
<span style="white-space:pre unsigned short iphdrlen;<br/>
<span style="white-space:pre char C;<br/>
<br/>
<span style="white-space:pre iphdr1 = (IpHeader1 *)Buffer;<br/>
<br/>
<span style="white-space:pre iphdrlen = (iphdr1->VerIHL << 4);<br/>
<span style="white-space:pre memcpy(&C, &iphdrlen, 1);<br/>
<span style="white-space:pre iphdrlen = (C >> 4) * 4; //20<br/>
<br/>
<br/>
<span style="white-space:pre memset(&SockAddr, 0, sizeof(SockAddr));<br/>
<span style="white-space:pre SockAddr.sin_addr.s_addr = iphdr1->SrcIP;<br/>
<span style="white-space:pre printf("Packet From: %s ", inet_ntoa(SockAddr.sin_addr));<br/>
<span style="white-space:pre memset(&SockAddr, 0, sizeof(SockAddr));<br/>
<span style="white-space:pre SockAddr.sin_addr.s_addr = iphdr1->DstIP;<br/>
<span style="white-space:pre printf("To: %s ", inet_ntoa(SockAddr.sin_addr));<br/>
<br/>
<span style="white-space:pre switch (iphdr1->Protocol)<br/>
<span style="white-space:pre {<br/>
<span style="white-space:pre case 1:<br/>
<span style="white-space:pre printf("Protocol: ICMP ");<br/>
<span style="white-space:pre break;<br/>
<span style="white-space:pre case 2:<br/>
<span style="white-space:pre printf("Protocol: IGMP ");<br/>
<span style="white-space:pre break;<br/>
<span style="white-space:pre case 6:<br/>
<span style="white-space:pre printf("Protocol: TCP ");<br/>
<span style="white-space:pre if (Size > iphdrlen)<br/>
<span style="white-space:pre {<br/>
<span style="white-space:pre port = (TcpUdpPort *)(Buffer + iphdrlen);<br/>
<span style="white-space:pre printf("From Port: %i To Port: %i ", ntohs(port->SrcPort), ntohs(port->DstPort));<br/>
<span style="white-space:pre }<br/>
<span style="white-space:pre break;<br/>
<span style="white-space:pre case 17:<br/>
<span style="white-space:pre printf("Protocol: UDP ");<br/>
<span style="white-space:pre if (Size > iphdrlen)<br/>
<span style="white-space:pre {<br/>
<span style="white-space:pre port = (TcpUdpPort *)(Buffer + iphdrlen);<br/>
<span style="white-space:pre printf("From Port: %i To Port: %i ", ntohs(port->SrcPort), ntohs(port->DstPort));<br/>
<span style="white-space:pre }<br/>
<span style="white-space:pre break;<br/>
<span style="white-space:pre default:<br/>
<span style="white-space:pre printf("Protocol: %i ", iphdr1->Protocol); <br/>
<span style="white-space:pre }<br/>
<br/>
<span style="white-space:pre printf("n");<br/>
}<br/>
<br/>
void StartSniffing(SOCKET Sock)<br/>
{<br/>
<span style="white-space:pre char *RecvBuffer = (char *)malloc(MAX_PACKET_SIZE + 1);<br/>
<span style="white-space:pre int BytesRecv, FromLen;<br/>
<span style="white-space:pre struct sockaddr_in From;<br/>
<br/>
<span style="white-space:pre if (RecvBuffer == NULL)<br/>
<span style="white-space:pre {<br/>
<span style="white-space:pre printf("malloc() failed.n");<br/>
<span style="white-space:pre exit(-1);<br/>
<span style="white-space:pre }<br/>
<br/>
<span style="white-space:pre FromLen = sizeof(From);<br/>
<br/>
<span style="white-space:pre do<br/>
<span style="white-space:pre {<br/>
<span style="white-space:pre memset(RecvBuffer, 0, MAX_PACKET_SIZE + 1);<br/>
<span style="white-space:pre memset(&From, 0, sizeof(From));<br/>
<br/>
<span style="white-space:pre BytesRecv = recvfrom(Sock, RecvBuffer, MAX_PACKET_SIZE, 0, (sockaddr *)&From, &FromLen);<br/>
<span style="white-space:pre printf("BytesRecv la:%i",BytesRecv);<br/>
<span style="white-space:pre if (BytesRecv > 0)<br/>
<span style="white-space:pre {<br/>
<span style="white-space:pre ProcessPacket(RecvBuffer, BytesRecv);<br/>
<span style="white-space:pre }<br/>
<span style="white-space:pre else<br/>
<span style="white-space:pre {<br/>
<span style="white-space:pre printf( "recvfrom() failed.n");<br/>
<span style="white-space:pre }<br/>
<br/>
<span style="white-space:pre } while (BytesRecv > 0);<br/>
<span style="white-space:pre free(RecvBuffer);<br/>
}<br/>
///////////////////////////////////////////////<br/>
char* GetLocalAddress()<br/>
{<br/>
// WSADATA wsaData;<br/>
struct hostent *remoteHost;<br/>
<span style="white-space:pre struct in_addr addr;<br/>
<span style="white-space:pre int i=0;<br/>
<span style="white-space:pre //WSAStartup(MAKEWORD(2, 2), &wsaData);<br/>
<span style="white-space:pre char* buffer="";<br/>
<span style="white-space:pre gethostname (buffer,strlen(buffer));<br/>
<span style="white-space:pre remoteHost = gethostbyname(buffer);<br/>
<span style="white-space:pre if (remoteHost == NULL) <br/>
<span style="white-space:pre {<br/>
<span style="white-space:pre printf("ko tim thay :");<br/>
<span style="white-space:pre }<br/>
else <br/>
<span style="white-space:pre {<br/>
//printf("tOfficial name: %sn", remoteHost->h_name);<br/>
<span style="white-space:pre addr.s_addr = *(u_long *) remoteHost->h_addr_list[i++];<br/>
<span style="white-space:pre //printf("tIP Address #%d: %sn", i, inet_ntoa(addr));<br/>
<span style="white-space:pre return (char*)inet_ntoa(addr);<br/>
<span style="white-space:pre } <br/>
<br/>
<span style="white-space:pre <br/>
}<br/>
////////////////////////////////////////////////<br/>
void main()<br/>
{<br/>
<span style="white-space:pre WSAData wsaData;<br/>
<span style="white-space:pre SOCKET Sock;<br/>
<span style="white-space:pre struct sockaddr_in SockAddr;<br/>
<span style="white-space:pre DWORD BytesReturned;<br/>
<span style="white-space:pre int I = 1;<br/>
<span style="white-space:pre try<br/>
<span style="white-space:pre {<br/>
<br/>
<span style="white-space:pre if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0)<br/>
<span style="white-space:pre {<br/>
<span style="white-space:pre printf("WSAStartup() failed.n");<br/>
<span style="white-space:pre exit(-1);<br/>
<span style="white-space:pre }<br/>
<br/>
<span style="white-space:pre Sock = socket(AF_INET, SOCK_RAW, IPPROTO_IP);<br/>
<br/>
<span style="white-space:pre if (Sock == INVALID_SOCKET)<br/>
<span style="white-space:pre {<br/>
<span style="white-space:pre printf("socket() failed.n");<br/>
<span style="white-space:pre exit(-1);<br/>
<span style="white-space:pre }<br/>
<br/>
<span style="white-space:pre memset(&SockAddr, 0, sizeof(SockAddr));<br/>
<span style="white-space:pre //SockAddr.sin_addr.s_addr = inet_addr(BIND2IP);<br/>
<span style="white-space:pre SockAddr.sin_addr.s_addr = inet_addr(GetLocalAddress());<br/>
<span style="white-space:pre SockAddr.sin_family = AF_INET;<br/>
<span style="white-space:pre SockAddr.sin_port = 0;<br/>
<br/>
<span style="white-space:pre if (bind(Sock, (sockaddr *)&SockAddr, sizeof(SockAddr))== SOCKET_ERROR)<br/>
<span style="white-space:pre {<br/>
<span style="white-space:pre printf("bind(%s) failed.n", GetLocalAddress());<br/>
<span style="white-space:pre exit(-1);<br/>
<span style="white-space:pre }<br/>
<span style="white-space:pre if (WSAIoctl(Sock, SIO_RCVALL, &I, sizeof(I), NULL, NULL, &BytesReturned, NULL, NULL) == SOCKET_ERROR)<br/>
<span style="white-space:pre {<br/>
<span style="white-space:pre printf("WSAIoctl() failed.n");<br/>
<span style="white-space:pre exit(-1);<br/>
<span style="white-space:pre }<br/>
<br/>
<span style="white-space:pre StartSniffing(Sock);<br/>
<span style="white-space:pre }<br/>
<span style="white-space:pre catch (...)<br/>
<span style="white-space:pre {<br/>
<span style="white-space:pre printf("CRASHn");<br/>
<span style="white-space:pre }<br/>
<span style="white-space:pre closesocket(Sock);<br/>
<span style="white-space:pre WSACleanup();<span style="white-space:pre
<br/>
<span style="white-space:pre getch();<br/>
}<br/>
<br/>
///////////////////////////////////////////////////////////////////////////////////<br/>

<span style="font-family:Verdana,Arial,sans-serif; font-size:13px; line-height:normal

View the full article
 
Back
Top