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

📄 ipman.cpp

📁 一个简单的sniffer,下载后请用winrar
💻 CPP
📖 第 1 页 / 共 3 页
字号:
/*	IPMan.CPP
	Console application to send and receive IP packet
        Written by HiHint
        Using vpacket.vxd */

#include "stdafx.h"
#include <stdio.h>
#include <windows.h>
#include <string.h>

#include "ntddpack.h"
#include "conpack.h"

#include "ip.h"

#define ERROR_OUT(str) { fprintf(stderr,str); CloseHandle(hEvent); return(0); }

#define RECV_MAX 32
#define SEND_MAX 32
#define MAX_CONNECTION 64
#define MAX_COMMAND 64
#define MAX_DATA 64
#define MAX_QUERY 64
#define SYSERR -1
#define OK 0

#define OPT_DO_NOTHING 0x0	/* Do nothing, print local ethernet address */
#define OPT_LISTEN_ALL 0x1	/* Listen all packet */
#define OPT_LISTEN_SPEC 0x41e	/* Listen specially */
#define OPT_LISTEN	0x41f	/* Listen */
#define OPT_LISTEN_IP 0xa	/* Listen IP packet carry special address */
#define OPT_LISTEN_IP_1	0x2	/* Listen IP packet carry one special address */
#define OPT_LISTEN_PORT 0x14	/* Listen IP packet carry special port */
#define OPT_LISTEN_PORT_1 0x4	/* Listen IP packet carry one special port */
#define OPT_LISTEN_IP_2 0x8 /* Listern IP packet between two address */
#define OPT_LISTEN_PORT_2 0x10	/* Listen IP packet between two port */

#define OPT_QUE_IP 0x20	/* Query ethernet address from IP address */
#define OPT_QUE_ETHER 0x40	/* Query IP address from ethernet address */

#define OPT_COMMAND_FILE 0x80	/* Work with command file */

#define OPT_OUTPUT_FILE 0x100	/* Output to file */

#define OPT_HELP 0x200	/* Print help message */

#define COMMAND_SEND_ARP 0x1	/* Send ARP packet */
#define COMMAND_SEND_TCP 0x2	/* Send TCP packet */
#define COMMAND_RECV_TCP 0x4	/* Receive TCP packet */
#define COMMAND_CONNECT_TCP 0x8	/* Connect TCP */
#define COMMAND_CLOSE_TCP 0x10	/* Close TCP */

#define COMMENT 0x8000	/* Comment line */

#define COMMAND_UNKNOWN 0x0	/* Unknown command */

#define OPT_LISTEN_INIT 0x400	/* Listen IP packet match INI file speciallity */
#define INITIAL_FILE "ipman.ini"	/* Initial file name */
#define INITIAL_MAX_DATA 0x01	/* Initial file max data legth */
#define INITIAL_IP_ADDR 0x02	/* Initial file my IP address */
#define INITIAL_NDIS 0x04	/* Initial file NDIS */
#define INITIAL_QUERY_IP 0x08	/* Initial file query ip array */
#define INITIAL_QUERY_PORT 0x10	/* Initial file query port array */
#define INITIAL_TIMEOUT	0x20	/* Initial file timeout */
#define INITIAL_INFINITE "INFINITE"	/* Initial file timeout infinite */
#define INITIAL_END_SEGMENT 0x40	/* Initial file blank line as end of segment */

struct CommandLine {
	DWORD option;
	char outfile[50];
	char cmdfile[50];
	struct IPAddr queip[2];
	struct EtherAddr queeth[2];
	WORD queport[2];
};

struct InitialFile {
	int maxdatalen;
	struct IPAddr mipaddr;
	char ndis[16];
	struct IPAddr qip[MAX_QUERY];
	int ipno;
	WORD qport[MAX_QUERY];
	int portno;
	DWORD timeout;
};

struct TCPConnection {
	struct EtherAddr sendeth,recveth;
	struct IPAddr sourip,destip;
	WORD sourport,destport;
	WORD id;
	DWORD ackno,seqno;
	BOOL inuse;
};	

BYTE InBuff[ sizeof(PACKET_OID_DATA) + 128 ];

DWORD Bind(HANDLE hVxD, BYTE* inBuffer);
WORD  GetHardEtherAddr(HANDLE hVxD,struct EtherAddr *petheraddr);
BYTE* SetOid(HANDLE hVxD, ULONG ulOid, ULONG ulLength, ULONG data);
DWORD QueryPacket(HANDLE hVxD,ULONG ioctl,BYTE* inBuffer,DWORD cbIn,
				  BYTE* outBuffer,DWORD cbOut);
BYTE* QueryOid(HANDLE hVxD, ULONG ulOid, ULONG ulLength);
WORD RecvStart(HANDLE hVxD,struct PacketTable *packtab);
WORD SendStart(HANDLE hVxD,struct PacketTable *packtab);
WORD RecvPacket(HANDLE hVxD,BYTE *pbuf);
WORD SendPacket(HANDLE hVxD,BYTE *pbuf,WORD len);
WORD CheckSum(WORD *pbuf,WORD len);
WORD ListenStart(HANDLE hVxD);

WORD SendARPPacket(HANDLE hVxD,
				   struct EtherAddr *psourether,
				   struct EtherAddr *pdestether,
				   struct EtherAddr *psendether,
				   struct EtherAddr *precvether,
				   struct IPAddr *psendip,
				   struct IPAddr *precvip,
				   WORD oper);
WORD SendTCPPacket(HANDLE hVxD,
				   struct EtherAddr *psourether,
				   struct EtherAddr *pdestether,
				   struct IPAddr *psourip,
				   struct IPAddr *pdestip,
				   WORD sourport,
				   WORD destport,
				   WORD flag,
				   DWORD seqno,
				   DWORD ackno,
				   char *pbuf,
				   WORD len);
WORD RecvTCPPacket(HANDLE hVxD,
				   struct IPAddr *psourip,
				   struct IPAddr *pdestip,
				   WORD sourport,
				   WORD destport,
				   WORD *flag,
				   DWORD *seqno,
				   DWORD *ackno,
				   char *pbuf,
				   WORD *size);
WORD swaps(WORD net);
DWORD swapl(DWORD net);
WORD GetOption(int argc,char *argv[],struct CommandLine *cmdline);
WORD GetInitial(FILE *pfini,struct InitialFile *inifile);
void ReadCommand(FILE *pf,char *pbuf);
WORD GetCommand(char *pbuf);
void GetSendARPArg(char *pbuf,
   				   struct EtherAddr *psendeth,
				   struct EtherAddr *precveth,
				   struct EtherAddr *soureth,
				   struct EtherAddr *desteth,
				   struct IPAddr *sourip,
				   struct IPAddr *destip,
				   WORD *oper);
void GetSendTCPArg(char *pbuf,WORD *id,char *pdata,WORD *size);
void GetRecvTCPArg(char *pbuf,WORD *id);
void GetConnectArg(char *pbuf,
				   struct EtherAddr *sendeth,
				   struct EtherAddr *recveth,
				   struct IPAddr *sourip,
				   struct IPAddr *destip,
				   WORD *sourport,
				   WORD *destport,
				   WORD *id);
void GetCloseArg(char *pbuf,WORD *id);
void GetDataArg(char *pbuf,char *pdata,WORD *size);
void GetIPAddr(char *pbuf,struct IPAddr *pipaddr);
void GetEtherAddr(char *pbuf,struct EtherAddr *pethaddr);
void PrintEtherAddr(FILE *pf,struct EtherAddr *pethaddr);
void PrintIPAddr(FILE *pf,struct IPAddr *pIPaddr);
void PrintData(FILE *pf,BYTE *pdata,int size);
void ToUpper(char *pbuf);

static Max_Data_Len;

int main(int argc,char *argv[])
{
	HANDLE hVxD;
	DWORD dwErrorCode;
	struct EtherAddr m_EtherAddr;
	struct IPAddr m_IPAddr;
	HANDLE hEvent;
	BOOL Continue;
	int i,j,k;
	BYTE Buffer[BUFFER_SIZE];
	WORD DataLen;
	struct EtherPacketHead *pEtherHead;
	struct IPPacketHead *pIPHead;
	struct TCPPacketHead *pTCPHead;
	struct ARPPacket *pARPHead;
	struct ICMPPacketHead *pICMPHead;
	struct UDPPacketHead *pUDPHead;
	int headlen,totallen;
	struct IPAddr *psourip,*pdestip;
	struct EtherAddr *psoureth,*pdesteth;
	WORD sourport,destport;
	DWORD seqno,ackno;
	BYTE *pdata;
	WORD filter;
	BOOL flag;
	FILE *pfout=NULL,*pfcmd=NULL,*pfini=NULL;
	struct EtherAddr ethernull,etherbroad;
	struct IPAddr ipnull;
	struct CommandLine cmdline;
	struct InitialFile inifile;
	char Command[BUFFER_SIZE];
	char *pbufh,*pbuft;
	struct EtherAddr sendethaddr,recvethaddr,sourethaddr,destethaddr;
	struct IPAddr sipaddr,dipaddr;
	WORD arpoper,tcpid;
	struct TCPConnection TCP[MAX_CONNECTION];
	WORD maxconn=0;
	WORD TCPFlag;

	/* Set null ethernet address and broadcast ethernet address */
	memset((void*)&ethernull,0,6);
	memset((void*)&etherbroad,0xff,6);
	memset((void*)&ipnull,0,6);

	/* Get initial file property */
	if((pfini=fopen(INITIAL_FILE,"r"))==NULL) {
		fprintf(stderr,"Can not open %s\n",INITIAL_FILE);
		return 0;
	}
	if(GetInitial(pfini,&inifile)!=OK) {
		fprintf(stderr,"Not valid initial file.\n");
		return 0;
	}
	Max_Data_Len=inifile.maxdatalen;
	memcpy((void *)&m_IPAddr,(void *)&inifile.mipaddr,4);

	/* Get command line option */
	if(GetOption(argc,argv,&cmdline)!=OK||cmdline.option&OPT_HELP) {
                fprintf(stderr,"IPMan Version 0.5b\n");
                fprintf(stderr,"Author: HiHint\nEmail:HiHint@hotmail.com\n\n");
                fprintf(stderr,"Usage ipman [-?] [-l[ini] [-ip:sour_ip[:dest_ip]] [-port:sour_port[:dest_port]]] [-qeth:ip_addr] [-qip:eth_addr] [-fout:output_file] [-fcmd:command_file]\n");
		return 0;
	}

	if((cmdline.option&OPT_LISTEN)!=0) filter=NDIS_PACKET_TYPE_PROMISCUOUS;
	else filter=NDIS_PACKET_TYPE_DIRECTED;

	/* Open device */
    hVxD = CreateFile("\\\\.\\VPACKET.VXD", 
                      GENERIC_READ | GENERIC_WRITE,
                      0,
                      NULL,
                      OPEN_EXISTING,
                      FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OVERLAPPED |
                      FILE_FLAG_DELETE_ON_CLOSE,
                      NULL);

    if (hVxD == INVALID_HANDLE_VALUE) {
        dwErrorCode = GetLastError();
        if (dwErrorCode == ERROR_NOT_SUPPORTED) {
            fprintf(stderr,"Unable to open VxD,\ndevice does not support DeviceIOCTL\n");
        }
        else
            fprintf(stderr,"Unable to open VxD, Error code: %lx\n", dwErrorCode);
		return(0);
    }

	/* Device opened successfully */
	/* Bind driver to NDIS3 adapter	*/

	Bind(hVxD,inifile.ndis);

	/* Get ethernet address and IP */
	if(GetHardEtherAddr(hVxD,&m_EtherAddr)!=OK) return 0;

	/* Set Filter */
	SetOid(hVxD, OID_GEN_CURRENT_PACKET_FILTER, 4, filter);
	
	if((cmdline.option&OPT_OUTPUT_FILE)!=0) {	/* Open output file */
		if((pfout=fopen(cmdline.outfile,"w"))==NULL) {
			fprintf(stderr,"Can not open output file.\n");
			goto ENDOFTASK;
		}
	}
	else pfout=stdout;		

	if(cmdline.option==0) {
		fprintf(pfout,"Ether address:");
		PrintEtherAddr(pfout,&m_EtherAddr);
		goto ENDOFTASK;
	}
	
	if((cmdline.option&OPT_COMMAND_FILE)!=0) {	/* Open command file */
		if((pfcmd=fopen(cmdline.cmdfile,"r"))==NULL) {
			fprintf(stderr,"Can not open command file.\n");
			goto ENDOFTASK;
		}
		for(i=0;i<MAX_CONNECTION;i++) TCP[i].inuse=FALSE;
	}

	if((cmdline.option&OPT_LISTEN)!=0) {	
		/* Do listen */
		Continue=TRUE;
		while(Continue) {
			DataLen=RecvPacket(hVxD,Buffer);
			if(DataLen==SYSERR) goto ENDOFTASK;
			/* Begin to process data */
			pEtherHead=(struct EtherPacketHead *)Buffer;
			switch (swaps(pEtherHead->ServType)) {
			case ETHER_PROTO_IP:	/* IP packet */
				pIPHead=(struct IPPacketHead *)(Buffer+ETHER_HEAD_LEN);
				headlen=(pIPHead->VerHLen)&0xf;
				headlen*=4;
				totallen=swaps(pIPHead->TtlLen);
				psourip=&(pIPHead->SourIP);
				pdestip=&(pIPHead->DestIP);
				flag=TRUE;
				if(cmdline.option&OPT_LISTEN_IP) {
					flag=FALSE;
					if((cmdline.option&OPT_LISTEN_IP_1)&&
					   (memcmp((void *)&cmdline.queip[0],(void *)psourip,4)==0||
						memcmp((void *)&cmdline.queip[0],(void *)pdestip,4)==0))  
						flag=TRUE;
					else if((cmdline.option&OPT_LISTEN_IP_2)&&
							((memcmp((void *)&cmdline.queip[0],(void *)psourip,4)==0&&
							  memcmp((void *)&cmdline.queip[1],(void *)pdestip,4))||
							 (memcmp((void *)&cmdline.queip[0],(void *)pdestip,4)==0&&
							  memcmp((void *)&cmdline.queip[1],(void *)psourip,4))))
						flag=TRUE;
				}
				else if(cmdline.option&OPT_LISTEN_INIT) {
					flag=FALSE;
					for(i=0;i<inifile.ipno;i++) 
						if(memcmp((void *)&inifile.qip[i],(void *)psourip,4)==0||
						   memcmp((void *)&inifile.qip[i],(void *)pdestip,4)==0) break;
					if(i<inifile.ipno) flag=TRUE;
				}
				if(flag) {
					switch (pIPHead->Proto) {
					case IP_PROTO_TCP:	/* TCP packet */
						pTCPHead=(struct TCPPacketHead *)(Buffer+ETHER_HEAD_LEN+headlen);
						totallen-=headlen;
						sourport=swaps(pTCPHead->SourPort);
						destport=swaps(pTCPHead->DestPort);
						seqno=swapl(pTCPHead->SeqNo);
						ackno=swapl(pTCPHead->AckNo);
						headlen=(pTCPHead->HLen)>>4;
						headlen*=4;
						totallen-=headlen;
						pdata=((BYTE *)pTCPHead)+headlen;
						/* TODO */
						flag=TRUE;
						if(cmdline.option&OPT_LISTEN_PORT) {
							flag=FALSE;
							if((cmdline.option&OPT_LISTEN_PORT_1)&&
							   (cmdline.queport[0]==sourport||
							    cmdline.queport[0]==destport))
								flag=TRUE;
							else if((cmdline.option&OPT_LISTEN_PORT_2)&&
								     ((cmdline.queport[0]==sourport&&cmdline.queport[1]==destport)||
									  (cmdline.queport[0]==destport&&cmdline.queport[1]==sourport)))
								flag=TRUE;										
						}
						else if(cmdline.option&OPT_LISTEN_INIT) {
							flag=FALSE;
							for(i=0;i<inifile.portno;i++) 
								if(sourport==inifile.qport[i]||
								   destport==inifile.qport[i]) break;
							if(i<inifile.portno) flag=TRUE;
						}
						if(flag) {
							PrintIPAddr(pfout,psourip);
							fprintf(pfout,":%d -> ",sourport);
							PrintIPAddr(pfout,pdestip);
							fprintf(pfout,":%d\n",destport);
							fprintf(pfout,"%d Byte(s) TCP Message:",totallen);
							PrintData(pfout,pdata,totallen);
							fprintf(pfout,"\n");
						}
						break;
					case IP_PROTO_UDP:	/* UDP packet */
						pUDPHead=(struct UDPPacketHead *)(Buffer+ETHER_HEAD_LEN+headlen);
						totallen-=headlen;
						sourport=swaps(pUDPHead->SourPort);
						destport=swaps(pUDPHead->DestPort);
						pdata=((BYTE *)pUDPHead)+UDP_HEAD_LEN;
						/* TODO */
						totallen-=UDP_HEAD_LEN;
						flag=TRUE;
						if(cmdline.option&OPT_LISTEN_PORT) {
							flag=FALSE;
							if((cmdline.option&OPT_LISTEN_PORT_1)&&
							   (cmdline.queport[0]==sourport||
							    cmdline.queport[0]==destport))
								flag=TRUE;
							else if((cmdline.option&OPT_LISTEN_PORT_2)&&
								     ((cmdline.queport[0]==sourport&&cmdline.queport[1]==destport)||
									  (cmdline.queport[0]==destport&&cmdline.queport[1]==sourport)))
								flag=TRUE;										
						}
						else if(cmdline.option&OPT_LISTEN_INIT) {
							flag=FALSE;
							for(i=0;i<inifile.portno;i++) 
								if(sourport==inifile.qport[i]||
								   destport==inifile.qport[i]) break;
							if(i<inifile.portno) flag=TRUE;
						}
						if(flag) {
							PrintIPAddr(pfout,psourip);
							fprintf(pfout,":%d -> ",sourport);
							PrintIPAddr(pfout,pdestip);
							fprintf(pfout,":%d\n",destport);
							fprintf(pfout,"%d Byte(s) UDP Message:",totallen);
							PrintData(pfout,pdata,totallen);
							fprintf(pfout,"\n");
						}
						break;
					case IP_PROTO_ICMP:	/* ICMP packet */
						flag=TRUE;
						if(cmdline.option&OPT_LISTEN_PORT) 
						   flag=FALSE;
						if(flag) {
							pICMPHead=(struct ICMPPacketHead *)(Buffer+ETHER_HEAD_LEN+headlen);
							totallen-=headlen;
							pdata=((BYTE *)pICMPHead)+ICMP_HEAD_LEN;
							totallen-=ICMP_HEAD_LEN;
							/* TODO */
							PrintIPAddr(pfout,psourip);
							fprintf(pfout," -> ");
							PrintIPAddr(pfout,pdestip);
							fprintf(pfout,"\n");
							fprintf(pfout,"%d Byte(s) ICMP Message:Type:%d Code:%d\n",totallen,pICMPHead->Type,pICMPHead->Code);
						}
						break;
					default:	/* Unknown packet */
						/*fprintf(stderr,"Unknown ip packet type.\n");*/
						break;
					}
				}
				break;
			case ETHER_PROTO_ARP:	/* ARP packet */
				if((cmdline.option&OPT_LISTEN_SPEC)==0) {
					pARPHead=(struct ARPPacket*)(Buffer+ETHER_HEAD_LEN);
					psourip=&(pARPHead->SourIP);
					pdestip=&(pARPHead->DestIP);
					psoureth=&(pARPHead->SourEther);
					pdesteth=&(pARPHead->DestEther);
					/* TODO */
					fprintf(pfout,"ARP Message:From:");
					PrintEtherAddr(pfout,psoureth);
					fprintf(pfout,":");
					PrintIPAddr(pfout,psourip);
					fprintf(pfout," To:");
					PrintEtherAddr(pfout,pdesteth);
					fprintf(pfout,":");
					PrintIPAddr(pfout,pdestip);
					fprintf(pfout," Operation:%d\n",swaps(pARPHead->Oper));
				}
				break;
			default:	/* Unknown packet */
				/*fprintf(stderr,"Unknown ethernet packet type.\n");*/
				break;
			}
		}
	}
	else if((cmdline.option&OPT_QUE_ETHER)!=0) {
		/* Do query ethernet address form IP */
		ListenStart(hVxD);
		SendARPPacket(hVxD,
					  &m_EtherAddr,
					  &etherbroad,
					  &m_EtherAddr,
					  &ethernull,
					  &m_IPAddr,
					  &cmdline.queip[0],
					  ARP_OPER_ARP_REQ);
		Continue=TRUE;
		while(Continue) {
			if((DataLen=RecvPacket(hVxD,Buffer))==SYSERR) {
				fprintf(stderr,"Can not recv ARP packet.\n");
				goto ENDOFTASK;
			}
			pEtherHead=(struct EtherPacketHead *)Buffer;
			if(swaps(pEtherHead->ServType)!=ETHER_PROTO_ARP) continue;
			pARPHead=(struct ARPPacket *)(Buffer+ETHER_HEAD_LEN);
			if(swaps(pARPHead->Oper)!=ARP_OPER_ARP_ANS) continue;
			if(memcmp((void *)&pARPHead->SourIP,(void *)&cmdline.queip[0],4)!=0) continue;
			/* Get ethernet address */
			fprintf(pfout,"Ethernet Address:");
			PrintEtherAddr(pfout,&pARPHead->SourEther);
			fprintf(pfout," of IP Address:");
			PrintIPAddr(pfout,&cmdline.queip[0]);
			fprintf(pfout,"\n"); 
			Continue=FALSE;
		}
	}
	else if((cmdline.option&OPT_QUE_IP)!=0) {
		/* Do query IP from ethernet address */
		ListenStart(hVxD);
		SendARPPacket(hVxD,
					  &m_EtherAddr,
					  &etherbroad,
					  &m_EtherAddr,

⌨️ 快捷键说明

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