⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 common.h

📁 采用arp欺骗的方法 让指定计算机无法连接其它任意计算机
💻 H
字号:


///////////////////////////////////////////////////////////////
#include <stdlib.h>
#include <stdio.h>
#include <pcap.h>                 //winpcap的头文件
#include <winsock2.h>
#include <process.h>              //多线程编程的头文件
#include <windows.h>
#include <Iphlpapi.h>             //提取网关用的头文件


#pragma comment(lib,"ws2_32")
#pragma comment(lib,"wpcap")
#pragma comment(lib,"IPHlpApi")

#define IPTOSBUFFERS 12               
#define MOSTCHEATNUMBER 256         //最大的欺骗数目
#define ETH_IP       0x0800          //定义各种标识的数值
#define ETH_ARP      0x0806
#define ARP_REPLY    0x0002
#define ARP_REQUEST  0x0001
#define ARP_HARDWARE 0x0001

#pragma pack(push,1)  
typedef struct ethdr                  //以太头结构
{
    unsigned char   eh_dst[6];
    unsigned char   eh_src[6];
    unsigned short  eh_type;
}ETHDR,*PETHDR;

typedef struct arphdr                  //arp头结构
{
    unsigned short  arp_hdr;//2      硬件类型
    unsigned short  arp_pro;//2      协议类型
    unsigned char   arp_hln;//1      硬件地址长度
    unsigned char   arp_pln;//1      协议地址长度
    unsigned short  arp_opt;//2      操作类型
    unsigned char   arp_sha[6];//6   发送端以太网地址
    unsigned long   arp_spa;//4      发送端ip地址
    unsigned char   arp_tha[6];//6 //目的以太网地址
    unsigned long   arp_tpa;//4    //目的ip地址
}ARPHDR,*PARPHDR;

typedef struct acttiveIpwithMac          //用于存储ip与对应mac的结构
{
    acttiveIpwithMac* next;
	unsigned long ip;
	unsigned char mac[6];
}acttiveIpwithMac,*PacttiveIpwithMac;
#pragma pack(pop)


#define  myDebugPrint(x)  OutputDebugString(x)
//获得网卡控制句柄,获得对应的ip地址  
pcap_t*   GetNetCardCtrlHandle(unsigned long *LocalIP);
//根据网卡描述字符串,来判断该网卡是否为物理网卡
BOOL      IsPhysicsNetCard(char *pDescrible);
//将ip地址网络字节顺序转化为字符串
char*     IPToString(unsigned long in);
BOOL      GetLocalHostMac(pcap_t* pNetCtrlHandle,PacttiveIpwithMac myip);
BOOL      GetMacByIP(pcap_t* pNetCtrlHandle,PacttiveIpwithMac myip,PacttiveIpwithMac pm);
//可以获得本网卡的ip地址
unsigned long GetNetCardIPAddress(pcap_if_t *pNetCard);
unsigned long GetGateWayIP(unsigned long uLocalIP);

int  macequal(PacttiveIpwithMac m,PacttiveIpwithMac n);
void FreeAddressList(PacttiveIpwithMac pList);


void FreeAddressList(PacttiveIpwithMac pList)
{
	PacttiveIpwithMac p=NULL;	
	while (pList!=NULL)	
	{
		p=pList->next;
		delete pList;
		pList=NULL;
		pList=p;
	}
}

int macequal(PacttiveIpwithMac m,PacttiveIpwithMac n)
{
	int i=0;
    if(memcmp(n->mac,m->mac,6)==0)
	i=1;
	return i;
}

pcap_t*   GetNetCardCtrlHandle(unsigned long *LocalIP)
{
	pcap_if_t    *pNetCard=NULL;
    pcap_if_t    *pAllNetCard=NULL;
	char         szError[PCAP_ERRBUF_SIZE]={0};
	//枚举系统网卡 ,并判断物理网卡
	if(pcap_findalldevs(&pAllNetCard, szError)==-1)      
	{					
		myDebugPrint(szError);
		return NULL;
	}
	pNetCard=pAllNetCard;
	while (pNetCard)
	{
		if (pNetCard->description&&IsPhysicsNetCard(pNetCard->description))
		{					
			break;
		}
		pNetCard=pNetCard->next;
	}
	if (pNetCard==NULL)
	{
		pcap_freealldevs(pAllNetCard);
		return NULL;
	}
	DWORD dwIP=0;
	dwIP=GetNetCardIPAddress(pNetCard);
	*LocalIP=dwIP;
	//打开物理网卡 ,获得网卡控制句柄
	pcap_t* pReturnNetAdapterHandle=NULL;  
	//根据过滤类型  可以设置捕获的长度
	if((pReturnNetAdapterHandle=pcap_open_live(pNetCard->name,
		70,1,1,szError))==NULL)
	{
		myDebugPrint(szError);
		pcap_freealldevs(pAllNetCard);
		return NULL;
	}
	pcap_freealldevs(pAllNetCard);
	return pReturnNetAdapterHandle;
} 		

BOOL  IsPhysicsNetCard(char *pDescrible)
{
	char szDescription[200]={0};
	int nLen=strlen(pDescrible);	
	memcpy(szDescription,pDescrible,nLen);
	szDescription[nLen]='\0';	
	if (strstr(szDescription,"Virtual")||strstr(szDescription,"PPP"))
	{
		return FALSE;
	}   
	if (strstr(szDescription,"MS")||strstr(szDescription,"Wireless"))
	{
		return FALSE;
	}
	if (strstr(szDescription,"dialup")||strstr(szDescription,"Bluetooth"))
	{
		return FALSE;
	}
	return TRUE;
}
//将ip地址转化为字符串
char*    IPToString(unsigned long in)
{
	 static char output[12][3*4+3+1];
     static short which;
     u_char *p;
     p = (u_char *)&in;
     which = (which + 1 == 12 ? 0 : which + 1);
     sprintf(output[which], "%d.%d.%d.%d", p[0], p[1], p[2], p[3]);
     return output[which];
}

//可以考虑在此函数中为抓包设置过滤规则
//根据物理网卡得到物理网卡控制句柄
//可以获得本网卡的ip地址
unsigned long  GetNetCardIPAddress(pcap_if_t *pNetCard)
{
	if(pNetCard==NULL)
	{
		myDebugPrint("[GetNICControlHandle][pNetCard==NULL]");
		return NULL;
	}
	unsigned long LocalIP=0;
	pcap_addr_t *a=NULL;
	char szMsg[200]={0};
	char szErrorBuff[200]={0};
	for (a=pNetCard->addresses;a;a=a->next)
	{
		if (a->addr->sa_family==AF_INET)
		{
			if (a->addr)//打印IP地址
			{
				LocalIP=((struct sockaddr_in *)(a->addr))->sin_addr.S_un.S_addr;
				sprintf(szMsg,"\tAddress: %s\n",
			    IPToString(((struct sockaddr_in *)a->addr)->sin_addr.s_addr));
				myDebugPrint(szMsg);			
			}
		}	
	} 
	return LocalIP;
}


unsigned long GetGateWayIP(unsigned long uLocalIP)
{
	PIP_ADAPTER_INFO pAdapterInfo;
    PIP_ADAPTER_INFO pAdapter = NULL;
    DWORD dwRetVal = 0;;
	//ULONG p;
	
    pAdapterInfo = (IP_ADAPTER_INFO *) malloc( sizeof(IP_ADAPTER_INFO) );
    ULONG ulOutBufLen = sizeof(IP_ADAPTER_INFO);
	
    // Make an initial call to GetAdaptersInfo to get
    // the necessary size into the ulOutBufLen variable
    if (GetAdaptersInfo( pAdapterInfo, &ulOutBufLen) == ERROR_BUFFER_OVERFLOW)   
	{
		free(pAdapterInfo);                          //malloc动态声请的空间要free
		pAdapterInfo = (IP_ADAPTER_INFO *) malloc (ulOutBufLen); 
	}
	
    if ((dwRetVal = GetAdaptersInfo( pAdapterInfo, &ulOutBufLen)) == NO_ERROR) 
	{
        pAdapter = pAdapterInfo;
		while (pAdapter) 
		{
			if(uLocalIP==inet_addr(pAdapter->IpAddressList.IpAddress.String))
			{
				dwRetVal=inet_addr(pAdapter->GatewayList.IpAddress.String);
				break;
			}
            pAdapter = pAdapter->Next;
		}
		free(pAdapterInfo);
		return dwRetVal;
	}
	return 0;
}

BOOL  GetMacByIP(pcap_t* pNetCtrlHandle,PacttiveIpwithMac myip,PacttiveIpwithMac pm)
{
	unsigned char   sendbuf[42];
	int    k;
    ETHDR  eth;
    ARPHDR arp;
	for(k=0;k<6;k++)
    {
        eth.eh_dst[k]=0xff;
        eth.eh_src[k]=myip->mac[k];
        arp.arp_sha[k]=myip->mac[k];
        arp.arp_tha[k]=0x00;
    }
    eth.eh_type=htons(ETH_ARP);
    arp.arp_hdr=htons(ARP_HARDWARE);
    arp.arp_pro=htons(ETH_IP);
    arp.arp_hln=6;
    arp.arp_pln=4;
    arp.arp_opt=htons(ARP_REQUEST);
	arp.arp_spa=myip->ip;
	arp.arp_tpa=pm->ip;
    memset(sendbuf,0,sizeof(sendbuf));
    memcpy(sendbuf,&eth,sizeof(eth));  	
	
    memcpy(sendbuf+sizeof(eth),&arp,sizeof(arp));
	if(pcap_sendpacket(pNetCtrlHandle,sendbuf,42)!=0)
	{
		printf("Getallactive ip PacketSendPacket in getmine Error: %d\n",GetLastError());
		return FALSE;
	}
	//receive arp reply
	struct pcap_pkthdr *  pkt_header;
	u_char * pkt_data;  
	int i;      
	while((pcap_next_ex(pNetCtrlHandle,&pkt_header,(const u_char**)&pkt_data))>0)
    {   
        if(*(unsigned short *)(pkt_data+12)==htons(ETH_ARP)&&*(unsigned short*)(pkt_data+20)==htons(ARP_REPLY)&&*(unsigned long*)(pkt_data+38)==myip->ip)
		{
			
			for(i=0;i<6;i++)
			{
				pm->mac[i]=*(unsigned char*)(pkt_data+22+i);
			}		
			break;
		}		
	}
	return TRUE;	
}


BOOL GetLocalHostMac(pcap_t* pNetCtrlHandle,PacttiveIpwithMac myip)
{
	unsigned char   sendbuf[42];
    int    i=7,k;
    ETHDR  eth;
    ARPHDR arp;
	struct pcap_pkthdr *  pkt_header;
	u_char * pkt_data; 
    for(k=0;k<6;k++)
    {
        eth.eh_dst[k]=0xff;
        eth.eh_src[k]=0x0f;
        arp.arp_sha[k]=0x0f;
        arp.arp_tha[k]=0x00;
    }
    eth.eh_type=htons(ETH_ARP);
    arp.arp_hdr=htons(ARP_HARDWARE);
    arp.arp_pro=htons(ETH_IP);
    arp.arp_hln=6;
    arp.arp_pln=4;
    arp.arp_opt=htons(ARP_REQUEST);
    arp.arp_tpa=myip->ip;
    arp.arp_spa=inet_addr("127.0.0.2");  //随便设的请求方ip
    memset(sendbuf,0,sizeof(sendbuf));
    memcpy(sendbuf,&eth,sizeof(eth));
    memcpy(sendbuf+sizeof(eth),&arp,sizeof(arp));
    if(pcap_sendpacket(pNetCtrlHandle,sendbuf,42)==0)
	{
		myDebugPrint("PacketSend succeed\n\n");
		//AfxMessageBox("PacketSend succeed");
	}
	else
	{
		//printf("PacketSendPacket in getmine Error: %d\n",GetLastError());
		myDebugPrint("PacketSendPacket in getmine Error");
		return 0;
	}	
	while((k=pcap_next_ex(pNetCtrlHandle,&pkt_header,(const u_char**)&pkt_data))>=0)
    {       
        if(*(unsigned short *)(pkt_data+12)==htons(ETH_ARP)&&*(unsigned short*)(pkt_data+20)==htons(ARP_REPLY)&&*(unsigned long*)(pkt_data+38)==inet_addr("127.0.0.2"))
		{
			
			for(i=0;i<6;i++)
			{
				myip->mac[i]=*(unsigned char*)(pkt_data+22+i);
			}
			break;
		}
    }
	if(i==6)
    {
		return 1;
	}
	else
	{
		return 0;
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -