반응형

Receive Final

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#include<pcap.h>
#include<malloc.h>
#include"Header.h"
#pragma comment(lib,"ws2_32.lib")
#pragma comment(lib,"wpcap.lib")
#pragma warning(disable:4996)
void packet_view(u_char *const struct pcap_pkthdr *const u_char *);
void print_hex(const u_char *int);
int main(void)
{
    char *dev;
    pcap_t *pd;    
    bpf_u_int32 net, subnet;    
    char errbuf[PCAP_ERRBUF_SIZE];
    struct bpf_program fcode;
 
    if (!(dev = pcap_lookupdev(errbuf))) {
        printf("[!] pcap_lookupdev\n");
        return -1;
    }
    if (pcap_lookupnet(dev, &net, &subnet, errbuf) < 0) {
        printf("[!] pcap_lookupnet\n");
        return -1;
    }
    if (!(pd = pcap_open_live(dev, 6553513000, errbuf))) {
        printf("[!] pcap_open_live\n");
        return -1;
    }
    if (pcap_compile(pd, &fcode, "tcp port 9623"0, subnet) < 0) {
        printf("[!] pcap_complie\n");
        return -1;
    }
    if (pcap_setfilter(pd, &fcode) < 0) {
        printf("[!] pcap_setfilter\n");
        return -1;
    }
    printf("[-] Listening\n");
    if (pcap_loop(pd, 0, packet_view, NULL< 0) {
        printf("[!] pcap_loop\n");
        return -1;
    }
    return 0;
}
void packet_view(u_char *param, const struct pcap_pkthdr *header, const u_char *pkt_data)
{
    int datalen = 0;
    char* buffer;
    ether_header *eh;
    ip_header *iph;
    udp_header *uh;
    tcp_header *th;
    u_int ip_len;
    
    printf("[*] Received Packet\n");
    printf("[*] Length: 0x%x\n", header->len);
    print_hex(pkt_data, header->len);
 
    eh = (ether_header*)pkt_data;
    printf("[*] Ethernet\n");
    printf("\tDestination: %02x-%02x-%02x-%02x-%02x-%02x\n", eh->eth_dst[0], eh->eth_dst[1], eh->eth_dst[2], \
        eh->eth_dst[3], eh->eth_dst[4], eh->eth_dst[5]);
    printf("\tSource:      %02x-%02x-%02x-%02x-%02x-%02x\n", eh->eth_src[0], eh->eth_src[1], eh->eth_src[2], \
        eh->eth_src[3], eh->eth_src[4], eh->eth_src[5]);
    if (ntohs(eh->eth_type) == ETHERTYPE_IP)
    {
        iph = (ip_header*)(pkt_data + ETH_HLEN);
        ip_len = (iph->ip_len & 0xf* 4;
 
        printf("[*] IP\n");
        printf("\tDestination:    %d.%d.%d.%d\n", iph->ip_dst[0], iph->ip_dst[1], iph->ip_dst[2], iph->ip_dst[3]);
        printf("\tSource:        %d.%d.%d.%d\n", iph->ip_src[0], iph->ip_src[1], iph->ip_src[2], iph->ip_src[3]);
        if (iph->ip_p == IPPROTO_TCP)
        {
            th = (tcp_header*)(pkt_data + ETH_HLEN + IP_HLEN);
            printf("[*] TCP\n");
            printf("\tDestPort:    %d\n", ntohs(th->tcp_dport));
            printf("\tSrcPort:    %d\n", ntohs(th->tcp_sport));
            printf("\tSEQ:        %X\n", ntohl(th->tcp_seqnum));
            printf("\tACK:        %X\n", ntohl(th->tcp_acknum));
            datalen = header->len - ETH_HLEN - IP_HLEN - th->tcp_hlen;
            buffer = (char*)_alloca(datalen+2);
            strncpy(buffer, (char*)pkt_data + ETH_HLEN + IP_HLEN + th->tcp_hlen, datalen);
            buffer[datalen] = NULL;
            printf("\tData: %s\n", buffer);
        }
        if (iph->ip_p == IPPROTO_UDP)
        {
            uh = (udp_header*)(pkt_data + ETH_HLEN + IP_HLEN);
            printf("[*] UDP\n");
            printf("\tDestPort:    %d\n", ntohs(uh->udp_dport));
            printf("\tSrcPort:    %d\n", ntohs(uh->udp_sport));
            datalen = header->len - ETH_HLEN - IP_HLEN - sizeof(udp_header);
            buffer = (char*)_alloca(datalen + 2);
            strncpy(buffer, (char*)pkt_data + ETH_HLEN + IP_HLEN + sizeof(udp_header), datalen);
            buffer[datalen] = NULL;
            printf("\tData: %s\n", buffer);
 
        }
    }
    printf("----------------------------------------\n");
}
void print_hex(const u_char *data, int len)
{
    int i, j;
    printf("Addr   ");
    for (i = 0; i < 16; i++)
        printf("%02X ", i);
    for (i = 0; i < 16; i++)
        printf("%X", i);
    printf("\n");
    for (i = 0; i * 16 < len - (len%16); i++) {
        printf("0x%04X ", i * 16);
        for (j = 0; j < 16; j++)
            printf("%02X ", (data + 16 * i)[j]);
        for (j = 0; j < 16; j++) {
            if (((data + 16 * i)[j]>0x1f) && ((data + 16 * i)[j] < 0x7F))
                printf("%c", (data + 16 * i)[j]);
            else
                printf(".");
        }
        printf("\n");
    }
    printf("0x%04X ", i * 16);
    for (j = 0; j < len % 16; j++)
        printf("%02X ", (data + 16 * i)[j]);
    for (; j < 16; j++)
        printf("   ");
    for (j = 0; j < len % 16; j++) {
        if (((data + 16 * i)[j]>0x1f) && ((data + 16 * i)[j] < 0x7F))
            printf("%c", (data + 16 * i)[j]);
        else
            printf(".");
    }
    printf("\n");
}
cs



'Network' 카테고리의 다른 글

LibTins ARP Spoofing  (0) 2016.06.24
WinPcap 3  (0) 2016.02.23
WinPcap 2  (0) 2016.02.23
WinPcap 1  (0) 2016.02.23
패킷 캡슐화 구현  (0) 2015.11.20

+ Recent posts