ARPSender by Refdom代码


//    
//     ARPSender
//    
//     File     : ARPSender.cpp
//     Comment   : A program for sending ARP packet
//    
//     Created at : 2002.8.6
//     Created by : Refdom
//            Email       : [email]refdom@263.net[/email]
//            Home Page : [url]www.opengram.com[/url]
//
//            If you modify the code, or add more functions, please email me a copy.
//    


#include "Mac.h"
#include <stdio.h>
#include <conio.h>
#include <windows.h>
#include <Packet32.h>

#pragma comment (lib, "ws2_32.lib")
#pragma comment (lib, "packet.lib")

#define EPT_IP            0x0800                  /* type: IP      */
#define EPT_ARP            0x0806                  /* type: ARP */
#define EPT_RARP      0x8035                  /* type: RARP */
#define ARP_HARDWARE 0x0001                  /* Dummy type for 802.3 frames */
#define      ARP_REQUEST      0x0001                  /* ARP request */
#define      ARP_REPLY      0x0002                  /* ARP reply */

#define Max_Num_Adapter 10

#pragma pack(push, 1)

typedef struct ehhdr
{
     unsigned char      eh_dst[6];            /* destination ethernet addrress */
     unsigned char      eh_src[6];            /* source ethernet addresss */
     unsigned short      eh_type;            /* ethernet pachet type      */
}EHHDR, *PEHHDR;


typedef struct arphdr
{
     unsigned short      arp_hrd;                  /* format of hardware address */
     unsigned short      arp_pro;                  /* format of protocol address */
     unsigned char      arp_hln;                  /* length of hardware address */
     unsigned char      arp_pln;                  /* length of protocol address */
     unsigned short      arp_op;                        /* ARP/RARP operation */

     unsigned char      arp_sha[6];                  /* sender hardware address */
     unsigned long      arp_spa;                  /* sender protocol address */
     unsigned char      arp_tha[6];                  /* target hardware address */
     unsigned long      arp_tpa;                  /* target protocol address */
}ARPHDR, *PARPHDR;

typedef struct arpPacket
{
     EHHDR      ehhdr;
     ARPHDR      arphdr;
} ARPPACKET, *PARPPACKET;

#pragma pack(pop)


void Usage()
{
     printf("******************************************/n");
     printf("ARPSender/n");
     printf("/t Written by Refdom/n");
     printf("/t Email: [email]refdom@263.net[/email]/n");
     printf("/n");
     printf("Usage: ARPSender.exe sha spa tha tpa arp_op times/n");
     printf("/nComment:/n");
     printf("/tsha:the MAC address of packet sender, eg:AAAAAABBBBBB/n");
     printf("/tspa:the IP address of packet sender, eg:192.168.1.1/n");
     printf("/ttha:the MAC address of target/n");
     printf("/ttpa:the IP address of target/n");
     printf("/tarp_op: the operation of ARP, 1:request, 2:reply/n");
     printf("/ttimes: the times of sending ARP packet.(int)/n");
     printf("*******************************************/n");
}


int main(int argc, char* argv[])
{
     static char AdapterList[Max_Num_Adapter][1024];     
     char szPacketBuf[600];
     char MacAddr[6];

     LPADAPTER      lpAdapter;
     LPPACKET      lpPacket;
     WCHAR            AdapterName[2048];
     WCHAR            *temp,*temp1;
     ARPPACKET ARPPacket;

     ULONG AdapterLength = 1024;
    
     int AdapterNum = 0;
     int nRetCode, i;
     int nARPOP = 0;
     int nTimes = 0;
     int nAdapter = 0;

     Usage();
     if (argc < 7)
     {
           return 0;
     }

     nARPOP = atoi(argv[5]);
     if (!(nARPOP == 1 || nARPOP == 2))
     {
           printf("/nPlease enter the ARP op!/n");
     }

     nTimes = atoi(argv[6]);
     if (nTimes <= 0)
     {
           nTimes = 1;
     }

     //Get The list of Adapter
     if(PacketGetAdapterNames((char*)AdapterName, &AdapterLength) == FALSE)
     {
           printf("Unable to retrieve the list of the adapters!/n");
           return 0;
     }

     temp = AdapterName;
     temp1=AdapterName;
     i = 0;
     while ((*temp != '/0')||(*(temp-1) != '/0'))
     {
           if (*temp == '/0')
           {
                 memcpy(AdapterList[i],temp1,(temp-temp1)*2);
                 temp1=temp+1;
                 i++;
           }
          
           temp++;
     }
    
     AdapterNum = i;
     for (i = 0; i < AdapterNum; i++)
     {
           wprintf(L"/n%d- %s/n", i+1, AdapterList[i]);
     }

     while((nAdapter <= 0) || (nAdapter > AdapterNum))
     {
           printf("/nPlease choose your Adapter:");
           scanf("%1d", &nAdapter);
     }
    
     printf("/n");

     //Default open the 0
     lpAdapter = (LPADAPTER) PacketOpenAdapter((LPTSTR) AdapterList[nAdapter - 1]);
     if (!lpAdapter || (lpAdapter->hFile == INVALID_HANDLE_VALUE))
     {
           nRetCode = GetLastError();
           printf("Unable to open the driver, Error Code : %lx/n", nRetCode);
           return 0;
     }

     lpPacket = PacketAllocatePacket();
     if(lpPacket == NULL)
     {
           printf("/nError:failed to allocate the LPPACKET structure.");
           return 0;
     }

     ZeroMemory(szPacketBuf, sizeof(szPacketBuf));

     if (!GetMacAddr(argv[3], MacAddr))
     {
           printf ("Get Mac address error!/n");
           return 0;
     }
     memcpy(ARPPacket.ehhdr.eh_dst, MacAddr, 6);

     if (!GetMacAddr(argv[1], MacAddr))
     {
           printf ("Get Mac address error!/n");
           return 0;
     }
     memcpy(ARPPacket.ehhdr.eh_src, MacAddr, 6);

     ARPPacket.ehhdr.eh_type = htons(EPT_ARP);

     ARPPacket.arphdr.arp_hrd = htons(ARP_HARDWARE);
     ARPPacket.arphdr.arp_pro = htons(EPT_IP);
     ARPPacket.arphdr.arp_hln = 6;
     ARPPacket.arphdr.arp_pln = 4;
     ARPPacket.arphdr.arp_op = htons(nARPOP);

     if (!GetMacAddr(argv[1], MacAddr))
     {
           printf ("Get Mac address error!/n");
           return 0;
     }
     memcpy(ARPPacket.arphdr.arp_sha, MacAddr, 6);

     ARPPacket.arphdr.arp_spa = inet_addr(argv[2]);

     if (!GetMacAddr(argv[3], MacAddr))
     {
           printf ("Get Mac address error!/n");
           return 0;
     }
     memcpy(ARPPacket.arphdr.arp_tha , MacAddr, 6);

     ARPPacket.arphdr.arp_tpa = inet_addr(argv[4]);

     memcpy(szPacketBuf, (char*)&ARPPacket, sizeof(ARPPacket));
     PacketInitPacket(lpPacket, szPacketBuf, 60);

     if(PacketSetNumWrites(lpAdapter, 1)==FALSE)
     {
           printf("warning: Unable to send more than one packet in a single write!/n");
     }
    
     for (i = 1; i <= nTimes; i++)
     {
           Sleep(10);

           if(PacketSendPacket(lpAdapter, lpPacket, TRUE)==FALSE)
           {
                 printf("Error sending the packets!/n");
                 return 0;
           }
           printf(".");
     }

     printf("/n");

     printf ("/nSend ok!/n");

     // close the adapter and exit
     PacketFreePacket(lpPacket);
     PacketCloseAdapter(lpAdapter);

     return 0;
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
ArpSender是C#写的ARP发包器,写的初衷是想试下什么样的ARP包才会引起ARP攻击,构造一些包可能会导致别人上不了网。至于包怎么填,发挥大家的想象吧,嘿嘿。 ArpSender用了SharpPcap这个开源API,有兴趣的到网上查下,用起来挺简单的。编程过程中最大的问题就是线程的问题。发包的代码中用了BackgroundWorker控件,因为参数只能传一个,并且线程是不允许直接调用主窗口控件,后来用了一个结构体struct ArpPac来传,感觉还是很麻烦啊!不过还好在BackgroundWorker的RunWorkerCompleted可以直接操作主窗口控件了,能把结果显示到主窗口上。 在编写获取IP的MAC地址部分,一开始还是用BackgroundWorker,在获取存在的IP地址MAC时可以正常工作,可是当IP不存在是,DOWORK方法一直没结束。因为里面一个Resolve方法一直没返回,也没超时设定,线程就一直卡在那不动了。也不知道该怎么结束这个线程。于是改用了Thread,传参数用了个object数组,嘿嘿,所有参数都封起来。到了那边再解封,很好用。后来才发现,线程是没有返回值的。。又不能直接操作窗体控件。。没办法,只好设个全局变量来保存结果了。获取MAC部分还用了个Timer控件,1秒钟如果还没得到返回的MAC,直接结束该线程。。。 BackgroundWorker 用起来简单也很好用,如果有个abort方法的话。。。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值