?? common.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 *)∈
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,ð,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,ð,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 + -