5 มิ.ย. 2556

เขียนโปรแกรม Remote capture packet ด้วย libpcap (รวบรัด)


ก่อนอื่น Download : ครับผม http://www.winpcap.org/archive/ หาไฟล์ที่มีคำว่า “Src” ข้างหลัง
ตัวอย่าง เครื่อง remote จะเป็น red hat enterprise
Copy source ลงไป โลด
(1)
เข้า ssh
ไป folder “libpcap”
cd /<upload>/libpcap
./configure
make

แล้วก็
cd rpcapd
make

ได้โปรแกรม rpcapd

(2)


Run service
./rpcapd -4  –p1113 -n
Command นี้แปลว่า เฉพาะ ipv4 + listen port 1113 + ไม่ต้อง authen

มาเทสด้วย Wireshark
Add remote adapter

(3)


ผ่าน

(4)


เริ่มเขียนโปรแกรม
Build Packet.lib ก่อน  เลือก 9x หรือ Ntx

(5)


ได้ Packet.dll

(6)


Build wpcap.lib 

(7)


สร้าง WpdPack (Include File) Run “build_wpdpack.bat”

(8)

คลังอาวุธ
Include files

(9)

Lib files

(10)





เริ่มเขียนโปรแกรม
H  File
#define YY_NEVER_INTERACTIVE
#define YY_NO_UNISTD_H
#define yylval pcap_lval
#define _USRDLL
#define LIBPCAP_EXPORTS
#define HAVE_STRERROR
#define INET6
#define SIZEOF_CHAR 1
#define SIZEOF_SHORT 2
#define SIZEOF_INT 4
#define HAVE_ADDRINFO
#define _U_
#define HAVE_SNPRINTF
#define HAVE_VSNPRINTF
#define HAVE_REMOTE
#define HAVE_TC_API
#define _WINDOWS
#include <pcap.h>

#ifndef _DEBUG
#pragma comment(lib,"wpcap.lib")
#pragma comment(lib,"packet.lib")
#else
#pragma comment(lib,"wpcap_d.lib")
#pragma comment(lib,"packet_d.lib")
#endif // DEBUG

Cpp File
       pcap_if_t *alldevs;
       pcap_if_t *d;
       pcap_t *fp;
       int res;
       struct pcap_pkthdr *header;
       const u_char *pkt_data;
       u_int netmask;
       struct bpf_program fcode;
       char errbuf[PCAP_ERRBUF_SIZE+1];
       if (pcap_findalldevs_ex("rpcap://192.168.220.129:1113", NULL, &alldevs, errbuf) == -1)
       {
              //error
       }
       else
       {
              for(d=alldevs;d;d=d->next)
              {
                    /* list รายชื่อ adapter ในเครื่อ งremote */
                     OutputDebugStringA(d->name);
                     OutputDebugStringA("\n");
                     if ( (fp= pcap_open(d->name, 100 /*snaplen*/,
                           PCAP_OPENFLAG_PROMISCUOUS /*flags*/,
                           20 /*read timeout*/,
                           NULL /* remote authentication */,
                           errbuf)
                           ) == NULL)
                     {
                           return ;
                     }
                     else
                     {
                            /* ตัวอย่าง set filter เฉพาะ tcp */
                           netmask = 0xffffff00;
                           if (pcap_compile(fp, &fcode, "tcp", 1, netmask) <0 )
                           {
                                  return;
                           }
                           else
                           {
                                  if (pcap_setfilter(fp, &fcode)<0)
                                  {
                                         pcap_close(fp);
                                         return;
                                  }
                           }
                     }
              }

              /* Read the packets */
              while((res = pcap_next_ex( fp, &header, &pkt_data)) >= 0)
              {

                     if(res == 0)
                                  continue;

                     char tmp[256];
                     sprintf(tmp,"\n %ld:%ld (%ld) \n", header->ts.tv_sec, header->ts.tv_usec, header->len);              
                     OutputDebugStringA(tmp);

                     /* Print the packet */
                     for (size_t i=1; (i < header->caplen + 1 ) ; i++)
                     {
                           sprintf(tmp,"%.2x ", pkt_data[i-1]);
                           OutputDebugStringA(tmp);
                           if ( (i % 16) == 0) OutputDebugStringA("\n");;
                     }

                     printf("\n\n");           
              }

              pcap_close(fp);
              pcap_freealldevs(alldevs);
       }