2011-12-4 code

37905 단어 code
   1:  #pragma comment(lib,"wpcap.lib")
   2:  #pragma comment(lib,"ws2_32.lib")
   3:  #pragma comment(lib,"wsock32.lib")
   4:   
   5:  #include <stdio.h> 
   6:  #define HAVE_REMOTE
   7:  #include <pcap.h> 
   8:  //#include "remote-ext.h"
   9:  #include <conio.h> 
  10:  #include <packet32.h> 
  11:  #include <ntddndis.h> 
  12:  #include "ArpCheat.h" 
  13:  #define LINE_LEN 16
  14:   
  15:   
  16:  /* packet handler      */
  17:  void packet_handler(u_char *param, const struct pcap_pkthdr *header, const u_char *pkt_data);
  18:   
  19:   
  20:  int main(int argc,char* argv[])
  21:  { 
  22:      pcap_if_t *alldevs; //       
  23:      pcap_if_t *d; //     
  24:      int inum; //          
  25:      int i=0; //     
  26:      pcap_t *adhandle; //  pcap   
  27:      pcap_t *fp;//          
  28:      char errbuf[PCAP_ERRBUF_SIZE]; //      
  29:      unsigned char *mac; //  MAC   
  30:      unsigned char *packet; //ARP  
  31:      unsigned long fakeIp; //     IP   
  32:      pcap_addr_t *pAddr; //     
  33:      unsigned long ip; //IP   
  34:      unsigned long netmask; //     
  35:   
  36:      struct bpf_program fcode;
  37:   
  38:      pcap_dumper_t *dumpfile;
  39:      char source[PCAP_BUF_SIZE];
  40:   
  41:      char packet_filter[] = "tcp";
  42:      // 0a 04  09  d9  ip:10.4.9.217
  43:      // 0a 04  09  e4  ip:10.4.9.228
  44:   
  45:   
  46:      /*if(argc!=2){ 
  47:          printf("Usage: %s inet_addr
",argv[0]);
  48:          return -1; 
  49:      } */
  50:      //strcpy(argv[1],"");
  51:   
  52:      //           IP   
  53:      /*fakeIp = inet_addr(argv[1]); 
  54:       
  55:      if(INADDR_NONE==fakeIp){ 
  56:          fprintf(stderr,"Invalid IP: %s
",argv[1]);
  57:          return -1; 
  58:      } */
  59:   
  60:      /*          */ 
  61:      if (pcap_findalldevs_ex(PCAP_SRC_IF_STRING, NULL, &alldevs, errbuf) == -1) 
  62:      { 
  63:          fprintf(stderr,"Error in pcap_findalldevs: %s
"
, errbuf);
  64:          exit(1); 
  65:      } 
  66:   
  67:      /*        */ 
  68:      for(d=alldevs; d; d=d->next) 
  69:      { 
  70:          printf("%d", ++i); 
  71:          if (d->description) 
  72:              printf(". %s
"
, d->description);
  73:          else 
  74:              printf(". No description available
"
);
  75:      } 
  76:      //         
  77:      if(i==0) 
  78:      { 
  79:          printf("
No interfaces found! Make sure WinPcap is installed.
"
);
  80:          return -1; 
  81:      } 
  82:      //          
  83:      printf("Enter the interface number (1-%d):",i); 
  84:      scanf("%d", &inum); 
  85:   
  86:      //                 ,    
  87:      if(inum < 1 || inum > i) 
  88:      { 
  89:          printf("
Interface number out of range.
"
);
  90:          /* Free the device list */ 
  91:          pcap_freealldevs(alldevs); 
  92:          return -1; 
  93:      } 
  94:   
  95:   
  96:   
  97:   
  98:      /*              */ 
  99:      for(d=alldevs, i=0; i< inum-1 ;d=d->next, i++); 
 100:   
 101:  //    mac = GetSelfMac(d->name+8); //+8   "rpcap://" 
 102:   
 103:      /*printf("  ARP   ,  (%.2X-%.2X-%.2X-%.2X-%.2X-%.2X)      %s
",
 104:          mac[0],mac[1],mac[2],mac[3],mac[4],mac[5],argv[1]); */
 105:   
 106:   
 107:   
 108:      /*      */ 
 109:      if ( (adhandle= pcap_open(d->name, // name of the device 
 110:          65536, // portion of the packet to capture 
 111:          PCAP_OPENFLAG_PROMISCUOUS,    //        @add by chai          //0, //open flag 
 112:          1000, // read timeout 
 113:          NULL, // authentication on the remote machine 
 114:          errbuf // error buffer 
 115:          ) ) == NULL) 
 116:      { 
 117:          fprintf(stderr,"
Unable to open the adapter. %s is not supported by WinPcap
"
,
 118:              d->name); 
 119:          /* Free the device list */ 
 120:          pcap_freealldevs(alldevs); 
 121:          return -1; 
 122:      } 
 123:      printf("
listening on %s...
"
, d->description);
 124:      //      
 125:      netmask=((struct sockaddr_in *)(d->addresses->netmask))->sin_addr.S_un.S_addr;
 126:   
 127:      /*       */
 128:      dumpfile = pcap_dump_open(adhandle, "cd.txt");
 129:      if(dumpfile==NULL)
 130:      {
 131:          fprintf(stderr,"
Error opening output file
"
);
 132:          return -1;
 133:      }
 134:      //    
 135:      if (pcap_compile(adhandle, &fcode, packet_filter, 1, netmask) <0 )
 136:      {
 137:          fprintf(stderr,"
Unable to compile the packet filter. Check the syntax.
"
);
 138:          /*        */
 139:          pcap_freealldevs(alldevs);
 140:          return -1;
 141:      }
 142:      //     
 143:      if (pcap_setfilter(adhandle, &fcode)<0)
 144:      {
 145:          fprintf(stderr,"
Error setting the filter.
"
);
 146:          /*        */
 147:          pcap_freealldevs(alldevs);
 148:          return -1;
 149:      }/////
 150:      printf("
listening on %s... Press Ctrl+C to stop...
"
, d->description);
 151:   
 152:   
 153:   
 154:   
 155:   
 156:      /*        */
 157:      pcap_freealldevs(alldevs);
 158:   
 159:      /*      */
 160:      //pcap_loop(adhandle, 10, packet_handler, NULL);
 161:      
 162:      pcap_loop(adhandle, 2, packet_handler2, (unsigned char *)dumpfile);
 163:   
 164:      /*    WinPcap           */
 165:      if ( pcap_createsrcstr( source,         //     
 166:          PCAP_SRC_FILE, //         
 167:          NULL,           //     
 168:          NULL,           //       
 169:          "cd.txt",        //          
 170:          errbuf          //      
 171:          ) != 0)
 172:      {
 173:          fprintf(stderr,"
Error creating a source string
"
);
 174:          return -1;
 175:      }
 176:      /*        */
 177:      if ( (fp= pcap_open(source,         //    
 178:          65536,          //           
 179:          // 65535                         
 180:          PCAP_OPENFLAG_PROMISCUOUS,     //     
 181:          1000,              //       
 182:          NULL,              //       
 183:          errbuf         //      
 184:          ) ) == NULL)
 185:      {
 186:          fprintf(stderr,"
Unable to open the file %s.
"
, source);
 187:          return -1;
 188:      }
 189:   
 190:      //         ,  EOF  
 191:      pcap_loop(fp, 0, dispatcher_handler, NULL);
 192:   
 193:   
 194:   
 195:      //for(pAddr=d->addresses; pAddr; pAddr=pAddr->next)
 196:      //{ 
 197:      //    //            IP   
 198:      //    ip = ((struct sockaddr_in *)pAddr->addr)->sin_addr.s_addr; 
 199:      //    //   IP          
 200:      //    netmask = ((struct sockaddr_in *)(pAddr->netmask))->sin_addr.S_un.S_addr; 
 201:      //    if (!ip || !netmask){ 
 202:      //        continue; 
 203:      //    } 
 204:      //    //    IP     IP         
 205:      //    if((ip&netmask)!=(fakeIp&netmask)){ 
 206:      //        continue; //        ,         
 207:      //    } 
 208:   
 209:      //    unsigned long netsize = ntohl(~netmask); //      
 210:      //    unsigned long net = ip & netmask; //     
 211:   
 212:      //    for(unsigned long n=1; n<netsize; n++){ 
 213:      //        // i    IP  ,       
 214:      //        unsigned long destIp = net | htonl(n); 
 215:      //        //    ARP   ,          IP      
 216:      //        while(1)
 217:      //        {
 218:      //            packet = BuildArpPacket(mac,fakeIp,destIp); 
 219:      //            if(pcap_sendpacket(adhandle, packet, 60)==-1){ 
 220:      //                fprintf(stderr,"pcap_sendpacket error.
");
 221:      //            }
 222:      //        }
 223:      //    } 
 224:   
 225:      //} 
 226:   
 227:      return 0; 
 228:  } 
 229:  void dispatcher_handler(u_char *temp1, const struct pcap_pkthdr *header, const u_char *pkt_data)
 230:  {
 231:      u_int i=0;
 232:   
 233:      /*   pkt    pkt   */
 234:      printf("%ld:%ld (%ld)
"
, header->ts.tv_sec, header->ts.tv_usec, header->len);
 235:   
 236:      /*       */
 237:      for (i=1; (i < header->caplen + 1 ) ; i++)
 238:      {
 239:          printf("%.2x ", pkt_data[i-1]);
 240:          if ( (i % LINE_LEN) == 0) printf("
"
);
 241:      }
 242:   
 243:      printf("

"
);
 244:   
 245:  }
 246:   
 247:  /*     ,        */
 248:  void packet_handler2(u_char *dumpfile, const struct pcap_pkthdr *header, const u_char *pkt_data)
 249:  {
 250:      /*           */
 251:      pcap_dump(dumpfile, header, pkt_data);
 252:  }
 253:   
 254:   
 255:  /*          ,libpcap             */
 256:  void packet_handler(u_char *param, const struct pcap_pkthdr *header, const u_char *pkt_data)
 257:  {
 258:      struct tm *ltime;
 259:      char timestr[16];
 260:      time_t local_tv_sec;
 261:   
 262:      /*               */
 263:      local_tv_sec = header->ts.tv_sec;
 264:      ltime=localtime(&local_tv_sec);
 265:      strftime( timestr, sizeof timestr, "%H:%M:%S", ltime);
 266:   
 267:      printf("%s,%.6d len:%d
"
, timestr, header->ts.tv_usec, header->len);
 268:   
 269:  }
 270:   
 271:  /** 
 272:  *      MAC   
 273:  * pDevName         
 274:  */ 
 275:  //unsigned char* GetSelfMac(char* pDevName)
 276:  //{ 
 277:  //
 278:  //    static u_char mac[6]; 
 279:  //
 280:  //    memset(mac,0,sizeof(mac)); 
 281:  //
 282:  //    LPADAPTER lpAdapter = PacketOpenAdapter(pDevName); 
 283:  //
 284:  //    if (!lpAdapter || (lpAdapter->hFile == INVALID_HANDLE_VALUE)) 
 285:  //    { 
 286:  //        return NULL; 
 287:  //    } 
 288:  //
 289:  //    PPACKET_OID_DATA OidData = (PPACKET_OID_DATA)malloc(6 + sizeof(PACKET_OID_DATA)); 
 290:  //    if (OidData == NULL) 
 291:  //    { 
 292:  //        PacketCloseAdapter(lpAdapter); 
 293:  //        return NULL; 
 294:  //    } 
 295:  //    // 
 296:  //    // Retrieve the adapter MAC querying the NIC driver 
 297:  //    // 
 298:  //    OidData->Oid = OID_802_3_CURRENT_ADDRESS; 
 299:  //
 300:  //    OidData->Length = 6; 
 301:  //    memset(OidData->Data, 0, 6); 
 302:  //    BOOLEAN Status = PacketRequest(lpAdapter, FALSE, OidData); 
 303:  //    if(Status) 
 304:  //    { 
 305:  //        memcpy(mac,(u_char*)(OidData->Data),6); 
 306:  //    } 
 307:  //    free(OidData); 
 308:  //    PacketCloseAdapter(lpAdapter); 
 309:  //    return mac; 
 310:  //
 311:  //} 
 312:   
 313:  /** 
 314:  *   ARP    
 315:  * source_mac  MAC   
 316:  * srcIP  IP 
 317:  * destIP   IP 
 318:  */ 
 319:  //unsigned char* BuildArpPacket(unsigned char* source_mac, unsigned long srcIP,unsigned long destIP) 
 320:  //{ 
 321:  //    static struct arp_packet packet; 
 322:  //    //  MAC       ,FF-FF-FF-FF-FF-FF 
 323:  //    memset(packet.eth.dest_mac,0xFF,6); 
 324:  //    // MAC   
 325:  //    memcpy(packet.eth.source_mac,source_mac,6); 
 326:  //    //     ARP  ,0x0806 
 327:  //    packet.eth.eh_type = htons(0x0806); 
 328:  //    //    ,Ethernet 0x0001 
 329:  //    packet.arp.hardware_type = htons(0x0001); 
 330:  //    //      ,IP 0x0800 
 331:  //    packet.arp.protocol_type = htons(0x0800); 
 332:  //    //      :MAC     0x06 
 333:  //    packet.arp.add_len = 0x06; 
 334:  //    //      :IP     0x04 
 335:  //    packet.arp.pro_len = 0x04; 
 336:  //    //  :ARP   1 
 337:  //    packet.arp.option = htons(0x0001); 
 338:  //    // MAC   
 339:  //    memcpy(packet.arp.sour_addr,source_mac,6); 
 340:  //    // IP   
 341:  //    packet.arp.sour_ip = srcIP; 
 342:  //    //  MAC  ,  0 
 343:  //    memset(packet.arp.dest_addr,0,6); 
 344:  //    //  IP   
 345:  //    packet.arp.dest_ip = destIP; 
 346:  //    //    ,18B 
 347:  //    memset(packet.arp.padding,0,18); 
 348:  //    return (unsigned char*)&packet; 
 349:  //} 

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }

좋은 웹페이지 즐겨찾기