EDN Admin
Well-known member
<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-spacere unsigned char VerIHL; //Version and IP Header Length<br/>
<span style="white-spacere unsigned char Tos;<br/>
<span style="white-spacere unsigned short Total_len;<br/>
<span style="white-spacere unsigned short ID;<br/>
<span style="white-spacere unsigned short Flags_and_Frags; //Flags 3 bits and Fragment offset 13 bits<br/>
<span style="white-spacere unsigned char TTL;<br/>
<span style="white-spacere unsigned char Protocol;<br/>
<span style="white-spacere unsigned short Checksum;<br/>
<span style="white-spacere unsigned long SrcIP;<br/>
<span style="white-spacere unsigned long DstIP;<br/>
<span style="white-spacere //unsigned long Options_and_Padding;<br/>
} IpHeader1;<br/>
<br/>
typedef struct port<br/>
{<br/>
<span style="white-spacere unsigned short SrcPort;<br/>
<span style="white-spacere unsigned short DstPort;<br/>
} TcpUdpPort;<br/>
<br/>
void ProcessPacket(char* Buffer, int Size)<br/>
{<br/>
<span style="white-spacere IpHeader1 *iphdr1;<br/>
<span style="white-spacere TcpUdpPort *port;<br/>
<span style="white-spacere struct sockaddr_in SockAddr;<br/>
<span style="white-spacere unsigned short iphdrlen;<br/>
<span style="white-spacere char C;<br/>
<br/>
<span style="white-spacere iphdr1 = (IpHeader1 *)Buffer;<br/>
<br/>
<span style="white-spacere iphdrlen = (iphdr1->VerIHL << 4);<br/>
<span style="white-spacere memcpy(&C, &iphdrlen, 1);<br/>
<span style="white-spacere iphdrlen = (C >> 4) * 4; //20<br/>
<br/>
<br/>
<span style="white-spacere memset(&SockAddr, 0, sizeof(SockAddr));<br/>
<span style="white-spacere SockAddr.sin_addr.s_addr = iphdr1->SrcIP;<br/>
<span style="white-spacere printf("Packet From: %s ", inet_ntoa(SockAddr.sin_addr));<br/>
<span style="white-spacere memset(&SockAddr, 0, sizeof(SockAddr));<br/>
<span style="white-spacere SockAddr.sin_addr.s_addr = iphdr1->DstIP;<br/>
<span style="white-spacere printf("To: %s ", inet_ntoa(SockAddr.sin_addr));<br/>
<br/>
<span style="white-spacere switch (iphdr1->Protocol)<br/>
<span style="white-spacere {<br/>
<span style="white-spacere case 1:<br/>
<span style="white-spacere printf("Protocol: ICMP ");<br/>
<span style="white-spacere break;<br/>
<span style="white-spacere case 2:<br/>
<span style="white-spacere printf("Protocol: IGMP ");<br/>
<span style="white-spacere break;<br/>
<span style="white-spacere case 6:<br/>
<span style="white-spacere printf("Protocol: TCP ");<br/>
<span style="white-spacere if (Size > iphdrlen)<br/>
<span style="white-spacere {<br/>
<span style="white-spacere port = (TcpUdpPort *)(Buffer + iphdrlen);<br/>
<span style="white-spacere printf("From Port: %i To Port: %i ", ntohs(port->SrcPort), ntohs(port->DstPort));<br/>
<span style="white-spacere }<br/>
<span style="white-spacere break;<br/>
<span style="white-spacere case 17:<br/>
<span style="white-spacere printf("Protocol: UDP ");<br/>
<span style="white-spacere if (Size > iphdrlen)<br/>
<span style="white-spacere {<br/>
<span style="white-spacere port = (TcpUdpPort *)(Buffer + iphdrlen);<br/>
<span style="white-spacere printf("From Port: %i To Port: %i ", ntohs(port->SrcPort), ntohs(port->DstPort));<br/>
<span style="white-spacere }<br/>
<span style="white-spacere break;<br/>
<span style="white-spacere default:<br/>
<span style="white-spacere printf("Protocol: %i ", iphdr1->Protocol); <br/>
<span style="white-spacere }<br/>
<br/>
<span style="white-spacere printf("n");<br/>
}<br/>
<br/>
void StartSniffing(SOCKET Sock)<br/>
{<br/>
<span style="white-spacere char *RecvBuffer = (char *)malloc(MAX_PACKET_SIZE + 1);<br/>
<span style="white-spacere int BytesRecv, FromLen;<br/>
<span style="white-spacere struct sockaddr_in From;<br/>
<br/>
<span style="white-spacere if (RecvBuffer == NULL)<br/>
<span style="white-spacere {<br/>
<span style="white-spacere printf("malloc() failed.n");<br/>
<span style="white-spacere exit(-1);<br/>
<span style="white-spacere }<br/>
<br/>
<span style="white-spacere FromLen = sizeof(From);<br/>
<br/>
<span style="white-spacere do<br/>
<span style="white-spacere {<br/>
<span style="white-spacere memset(RecvBuffer, 0, MAX_PACKET_SIZE + 1);<br/>
<span style="white-spacere memset(&From, 0, sizeof(From));<br/>
<br/>
<span style="white-spacere BytesRecv = recvfrom(Sock, RecvBuffer, MAX_PACKET_SIZE, 0, (sockaddr *)&From, &FromLen);<br/>
<span style="white-spacere printf("BytesRecv la:%i",BytesRecv);<br/>
<span style="white-spacere if (BytesRecv > 0)<br/>
<span style="white-spacere {<br/>
<span style="white-spacere ProcessPacket(RecvBuffer, BytesRecv);<br/>
<span style="white-spacere }<br/>
<span style="white-spacere else<br/>
<span style="white-spacere {<br/>
<span style="white-spacere printf( "recvfrom() failed.n");<br/>
<span style="white-spacere }<br/>
<br/>
<span style="white-spacere } while (BytesRecv > 0);<br/>
<span style="white-spacere free(RecvBuffer);<br/>
}<br/>
///////////////////////////////////////////////<br/>
char* GetLocalAddress()<br/>
{<br/>
// WSADATA wsaData;<br/>
struct hostent *remoteHost;<br/>
<span style="white-spacere struct in_addr addr;<br/>
<span style="white-spacere int i=0;<br/>
<span style="white-spacere //WSAStartup(MAKEWORD(2, 2), &wsaData);<br/>
<span style="white-spacere char* buffer="";<br/>
<span style="white-spacere gethostname (buffer,strlen(buffer));<br/>
<span style="white-spacere remoteHost = gethostbyname(buffer);<br/>
<span style="white-spacere if (remoteHost == NULL) <br/>
<span style="white-spacere {<br/>
<span style="white-spacere printf("ko tim thay :");<br/>
<span style="white-spacere }<br/>
else <br/>
<span style="white-spacere {<br/>
//printf("tOfficial name: %sn", remoteHost->h_name);<br/>
<span style="white-spacere addr.s_addr = *(u_long *) remoteHost->h_addr_list[i++];<br/>
<span style="white-spacere //printf("tIP Address #%d: %sn", i, inet_ntoa(addr));<br/>
<span style="white-spacere return (char*)inet_ntoa(addr);<br/>
<span style="white-spacere } <br/>
<br/>
<span style="white-spacere <br/>
}<br/>
////////////////////////////////////////////////<br/>
void main()<br/>
{<br/>
<span style="white-spacere WSAData wsaData;<br/>
<span style="white-spacere SOCKET Sock;<br/>
<span style="white-spacere struct sockaddr_in SockAddr;<br/>
<span style="white-spacere DWORD BytesReturned;<br/>
<span style="white-spacere int I = 1;<br/>
<span style="white-spacere try<br/>
<span style="white-spacere {<br/>
<br/>
<span style="white-spacere if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0)<br/>
<span style="white-spacere {<br/>
<span style="white-spacere printf("WSAStartup() failed.n");<br/>
<span style="white-spacere exit(-1);<br/>
<span style="white-spacere }<br/>
<br/>
<span style="white-spacere Sock = socket(AF_INET, SOCK_RAW, IPPROTO_IP);<br/>
<br/>
<span style="white-spacere if (Sock == INVALID_SOCKET)<br/>
<span style="white-spacere {<br/>
<span style="white-spacere printf("socket() failed.n");<br/>
<span style="white-spacere exit(-1);<br/>
<span style="white-spacere }<br/>
<br/>
<span style="white-spacere memset(&SockAddr, 0, sizeof(SockAddr));<br/>
<span style="white-spacere //SockAddr.sin_addr.s_addr = inet_addr(BIND2IP);<br/>
<span style="white-spacere SockAddr.sin_addr.s_addr = inet_addr(GetLocalAddress());<br/>
<span style="white-spacere SockAddr.sin_family = AF_INET;<br/>
<span style="white-spacere SockAddr.sin_port = 0;<br/>
<br/>
<span style="white-spacere if (bind(Sock, (sockaddr *)&SockAddr, sizeof(SockAddr))== SOCKET_ERROR)<br/>
<span style="white-spacere {<br/>
<span style="white-spacere printf("bind(%s) failed.n", GetLocalAddress());<br/>
<span style="white-spacere exit(-1);<br/>
<span style="white-spacere }<br/>
<span style="white-spacere if (WSAIoctl(Sock, SIO_RCVALL, &I, sizeof(I), NULL, NULL, &BytesReturned, NULL, NULL) == SOCKET_ERROR)<br/>
<span style="white-spacere {<br/>
<span style="white-spacere printf("WSAIoctl() failed.n");<br/>
<span style="white-spacere exit(-1);<br/>
<span style="white-spacere }<br/>
<br/>
<span style="white-spacere StartSniffing(Sock);<br/>
<span style="white-spacere }<br/>
<span style="white-spacere catch (...)<br/>
<span style="white-spacere {<br/>
<span style="white-spacere printf("CRASHn");<br/>
<span style="white-spacere }<br/>
<span style="white-spacere closesocket(Sock);<br/>
<span style="white-spacere WSACleanup();<span style="white-spacere
<br/>
<span style="white-spacere getch();<br/>
}<br/>
<br/>
///////////////////////////////////////////////////////////////////////////////////<br/>
<span style="font-family:Verdana,Arial,sans-serif; font-size:13px; line-height:normal
View the full article
<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-spacere unsigned char VerIHL; //Version and IP Header Length<br/>
<span style="white-spacere unsigned char Tos;<br/>
<span style="white-spacere unsigned short Total_len;<br/>
<span style="white-spacere unsigned short ID;<br/>
<span style="white-spacere unsigned short Flags_and_Frags; //Flags 3 bits and Fragment offset 13 bits<br/>
<span style="white-spacere unsigned char TTL;<br/>
<span style="white-spacere unsigned char Protocol;<br/>
<span style="white-spacere unsigned short Checksum;<br/>
<span style="white-spacere unsigned long SrcIP;<br/>
<span style="white-spacere unsigned long DstIP;<br/>
<span style="white-spacere //unsigned long Options_and_Padding;<br/>
} IpHeader1;<br/>
<br/>
typedef struct port<br/>
{<br/>
<span style="white-spacere unsigned short SrcPort;<br/>
<span style="white-spacere unsigned short DstPort;<br/>
} TcpUdpPort;<br/>
<br/>
void ProcessPacket(char* Buffer, int Size)<br/>
{<br/>
<span style="white-spacere IpHeader1 *iphdr1;<br/>
<span style="white-spacere TcpUdpPort *port;<br/>
<span style="white-spacere struct sockaddr_in SockAddr;<br/>
<span style="white-spacere unsigned short iphdrlen;<br/>
<span style="white-spacere char C;<br/>
<br/>
<span style="white-spacere iphdr1 = (IpHeader1 *)Buffer;<br/>
<br/>
<span style="white-spacere iphdrlen = (iphdr1->VerIHL << 4);<br/>
<span style="white-spacere memcpy(&C, &iphdrlen, 1);<br/>
<span style="white-spacere iphdrlen = (C >> 4) * 4; //20<br/>
<br/>
<br/>
<span style="white-spacere memset(&SockAddr, 0, sizeof(SockAddr));<br/>
<span style="white-spacere SockAddr.sin_addr.s_addr = iphdr1->SrcIP;<br/>
<span style="white-spacere printf("Packet From: %s ", inet_ntoa(SockAddr.sin_addr));<br/>
<span style="white-spacere memset(&SockAddr, 0, sizeof(SockAddr));<br/>
<span style="white-spacere SockAddr.sin_addr.s_addr = iphdr1->DstIP;<br/>
<span style="white-spacere printf("To: %s ", inet_ntoa(SockAddr.sin_addr));<br/>
<br/>
<span style="white-spacere switch (iphdr1->Protocol)<br/>
<span style="white-spacere {<br/>
<span style="white-spacere case 1:<br/>
<span style="white-spacere printf("Protocol: ICMP ");<br/>
<span style="white-spacere break;<br/>
<span style="white-spacere case 2:<br/>
<span style="white-spacere printf("Protocol: IGMP ");<br/>
<span style="white-spacere break;<br/>
<span style="white-spacere case 6:<br/>
<span style="white-spacere printf("Protocol: TCP ");<br/>
<span style="white-spacere if (Size > iphdrlen)<br/>
<span style="white-spacere {<br/>
<span style="white-spacere port = (TcpUdpPort *)(Buffer + iphdrlen);<br/>
<span style="white-spacere printf("From Port: %i To Port: %i ", ntohs(port->SrcPort), ntohs(port->DstPort));<br/>
<span style="white-spacere }<br/>
<span style="white-spacere break;<br/>
<span style="white-spacere case 17:<br/>
<span style="white-spacere printf("Protocol: UDP ");<br/>
<span style="white-spacere if (Size > iphdrlen)<br/>
<span style="white-spacere {<br/>
<span style="white-spacere port = (TcpUdpPort *)(Buffer + iphdrlen);<br/>
<span style="white-spacere printf("From Port: %i To Port: %i ", ntohs(port->SrcPort), ntohs(port->DstPort));<br/>
<span style="white-spacere }<br/>
<span style="white-spacere break;<br/>
<span style="white-spacere default:<br/>
<span style="white-spacere printf("Protocol: %i ", iphdr1->Protocol); <br/>
<span style="white-spacere }<br/>
<br/>
<span style="white-spacere printf("n");<br/>
}<br/>
<br/>
void StartSniffing(SOCKET Sock)<br/>
{<br/>
<span style="white-spacere char *RecvBuffer = (char *)malloc(MAX_PACKET_SIZE + 1);<br/>
<span style="white-spacere int BytesRecv, FromLen;<br/>
<span style="white-spacere struct sockaddr_in From;<br/>
<br/>
<span style="white-spacere if (RecvBuffer == NULL)<br/>
<span style="white-spacere {<br/>
<span style="white-spacere printf("malloc() failed.n");<br/>
<span style="white-spacere exit(-1);<br/>
<span style="white-spacere }<br/>
<br/>
<span style="white-spacere FromLen = sizeof(From);<br/>
<br/>
<span style="white-spacere do<br/>
<span style="white-spacere {<br/>
<span style="white-spacere memset(RecvBuffer, 0, MAX_PACKET_SIZE + 1);<br/>
<span style="white-spacere memset(&From, 0, sizeof(From));<br/>
<br/>
<span style="white-spacere BytesRecv = recvfrom(Sock, RecvBuffer, MAX_PACKET_SIZE, 0, (sockaddr *)&From, &FromLen);<br/>
<span style="white-spacere printf("BytesRecv la:%i",BytesRecv);<br/>
<span style="white-spacere if (BytesRecv > 0)<br/>
<span style="white-spacere {<br/>
<span style="white-spacere ProcessPacket(RecvBuffer, BytesRecv);<br/>
<span style="white-spacere }<br/>
<span style="white-spacere else<br/>
<span style="white-spacere {<br/>
<span style="white-spacere printf( "recvfrom() failed.n");<br/>
<span style="white-spacere }<br/>
<br/>
<span style="white-spacere } while (BytesRecv > 0);<br/>
<span style="white-spacere free(RecvBuffer);<br/>
}<br/>
///////////////////////////////////////////////<br/>
char* GetLocalAddress()<br/>
{<br/>
// WSADATA wsaData;<br/>
struct hostent *remoteHost;<br/>
<span style="white-spacere struct in_addr addr;<br/>
<span style="white-spacere int i=0;<br/>
<span style="white-spacere //WSAStartup(MAKEWORD(2, 2), &wsaData);<br/>
<span style="white-spacere char* buffer="";<br/>
<span style="white-spacere gethostname (buffer,strlen(buffer));<br/>
<span style="white-spacere remoteHost = gethostbyname(buffer);<br/>
<span style="white-spacere if (remoteHost == NULL) <br/>
<span style="white-spacere {<br/>
<span style="white-spacere printf("ko tim thay :");<br/>
<span style="white-spacere }<br/>
else <br/>
<span style="white-spacere {<br/>
//printf("tOfficial name: %sn", remoteHost->h_name);<br/>
<span style="white-spacere addr.s_addr = *(u_long *) remoteHost->h_addr_list[i++];<br/>
<span style="white-spacere //printf("tIP Address #%d: %sn", i, inet_ntoa(addr));<br/>
<span style="white-spacere return (char*)inet_ntoa(addr);<br/>
<span style="white-spacere } <br/>
<br/>
<span style="white-spacere <br/>
}<br/>
////////////////////////////////////////////////<br/>
void main()<br/>
{<br/>
<span style="white-spacere WSAData wsaData;<br/>
<span style="white-spacere SOCKET Sock;<br/>
<span style="white-spacere struct sockaddr_in SockAddr;<br/>
<span style="white-spacere DWORD BytesReturned;<br/>
<span style="white-spacere int I = 1;<br/>
<span style="white-spacere try<br/>
<span style="white-spacere {<br/>
<br/>
<span style="white-spacere if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0)<br/>
<span style="white-spacere {<br/>
<span style="white-spacere printf("WSAStartup() failed.n");<br/>
<span style="white-spacere exit(-1);<br/>
<span style="white-spacere }<br/>
<br/>
<span style="white-spacere Sock = socket(AF_INET, SOCK_RAW, IPPROTO_IP);<br/>
<br/>
<span style="white-spacere if (Sock == INVALID_SOCKET)<br/>
<span style="white-spacere {<br/>
<span style="white-spacere printf("socket() failed.n");<br/>
<span style="white-spacere exit(-1);<br/>
<span style="white-spacere }<br/>
<br/>
<span style="white-spacere memset(&SockAddr, 0, sizeof(SockAddr));<br/>
<span style="white-spacere //SockAddr.sin_addr.s_addr = inet_addr(BIND2IP);<br/>
<span style="white-spacere SockAddr.sin_addr.s_addr = inet_addr(GetLocalAddress());<br/>
<span style="white-spacere SockAddr.sin_family = AF_INET;<br/>
<span style="white-spacere SockAddr.sin_port = 0;<br/>
<br/>
<span style="white-spacere if (bind(Sock, (sockaddr *)&SockAddr, sizeof(SockAddr))== SOCKET_ERROR)<br/>
<span style="white-spacere {<br/>
<span style="white-spacere printf("bind(%s) failed.n", GetLocalAddress());<br/>
<span style="white-spacere exit(-1);<br/>
<span style="white-spacere }<br/>
<span style="white-spacere if (WSAIoctl(Sock, SIO_RCVALL, &I, sizeof(I), NULL, NULL, &BytesReturned, NULL, NULL) == SOCKET_ERROR)<br/>
<span style="white-spacere {<br/>
<span style="white-spacere printf("WSAIoctl() failed.n");<br/>
<span style="white-spacere exit(-1);<br/>
<span style="white-spacere }<br/>
<br/>
<span style="white-spacere StartSniffing(Sock);<br/>
<span style="white-spacere }<br/>
<span style="white-spacere catch (...)<br/>
<span style="white-spacere {<br/>
<span style="white-spacere printf("CRASHn");<br/>
<span style="white-spacere }<br/>
<span style="white-spacere closesocket(Sock);<br/>
<span style="white-spacere WSACleanup();<span style="white-spacere
<br/>
<span style="white-spacere getch();<br/>
}<br/>
<br/>
///////////////////////////////////////////////////////////////////////////////////<br/>
<span style="font-family:Verdana,Arial,sans-serif; font-size:13px; line-height:normal
View the full article