-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
155 lines (141 loc) · 3.85 KB
/
main.cpp
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#include <pcap.h>
#include <stdio.h>
#include <arpa/inet.h>
#define ETHER_ADDR_LEN 6
#define ETHERTYPE_IP 0x800
#define ETHERTYPE_ARP 0x806
#define ETHER_IP_OFFSET 14
struct ethernet{
u_char ether_dhost[ETHER_ADDR_LEN];
u_char ether_shost[ETHER_ADDR_LEN];
u_short ether_type;
};
struct ipH{
u_char ip_header_len:4;
u_char ip_version:4;
u_char ip_tos;
u_short ip_total_length;
u_short ip_id;
u_char ip_frag_offset:5;
u_char ip_more_fragment:1;
u_char ip_dont_fragment:1;
u_char ip_reserved_zero:1;
u_char ip_frag_offset1;
u_char ip_ttl;
u_char ip_protocol;
u_short ip_checksum;
struct in_addr ip_src,ip_dst;
};
struct tcp{
u_short th_sport; /* source port */
u_short th_dport; /* destination port */
u_int th_seq; /* sequence number */
u_int th_ack; /* acknowledgement number */
u_char th_off; /* data offset, rsvd */
#define TH_OFF(th) (((th)->th_offx2 & 0xf0) >> 4)
u_char th_flags;
#define TH_FIN 0x01
#define TH_SYN 0x02
#define TH_RST 0x04
#define TH_PUSH 0x08
#define TH_ACK 0x10
#define TH_URG 0x20
#define TH_ECE 0x40
#define TH_CWR 0x80
#define TH_FLAGS (TH_FIN|TH_SYN|TH_RST|TH_ACK|TH_URG|TH_ECE|TH_CWR)
u_short th_win; /* window */
u_short th_sum; /* checksum */
u_short th_urp; /* urgent pointer */
};
u_int16_t show_ether_header(struct ethernet *);
u_char show_ip_header(struct ipH *);
u_int8_t show_tcp_header(struct tcp * );
void show_data(u_char *,int);
void print_mac_addr(u_char * mac_addr);
void usage() {
printf("syntax: pcap_test <interface>\n");
printf("sample: pcap_test wlan0\n");
}
int main(int argc, char* argv[]) {
if (argc != 2) {
usage();
return -1;
}
char* dev = argv[1];
char errbuf[PCAP_ERRBUF_SIZE];
pcap_t* handle = pcap_open_live(dev, BUFSIZ, 1, 1000, errbuf);
if (handle == NULL) {
fprintf(stderr, "couldn't open device %s: %s\n", dev, errbuf);
return -1;
}
while (true) {
struct pcap_pkthdr* header;
const u_char* packet;
struct ethernet *ether_header;
struct ipH * ip_header;
struct tcp * tcp_header;
int total_len = 0, ip_tcp_offset = 0, tcp_data_offset = 0,data_len=0;
int res = pcap_next_ex(handle, &header, &packet);
if (res == 0) continue;
if (res == -1 || res == -2) break;
printf("%u bytes captured\n", header->caplen);
ether_header = (struct ethernet*)(packet);
if(show_ether_header(ether_header) == ETHERTYPE_IP)
{
ip_header = (struct ipH *)(packet+ETHER_IP_OFFSET);
ip_tcp_offset = ip_header->ip_header_len * 4;
total_len = ip_header->ip_total_length;
if(show_ip_header(ip_header) == IPPROTO_TCP)
{
tcp_header = (struct tcp *)((u_char *)ip_header+ip_tcp_offset);
tcp_data_offset = tcp_header->th_off * 4;
show_tcp_header(tcp_header);
if(tcp_data_offset > 0)
{
data_len = total_len-ETHER_IP_OFFSET-ip_tcp_offset-tcp_data_offset;
show_data((u_char *)tcp_header+tcp_data_offset,data_len);
}
}
}
}
pcap_close(handle);
return 0;
}
u_int16_t show_ether_header(struct ethernet * ether)
{
printf("[+] Source ADDR : "); print_mac_addr(ether->ether_shost);
printf("[+] Dest ADDR : "); print_mac_addr(ether->ether_dhost);
printf("[+] ETHER TYPE : 0x%x\n", ntohs(ether->ether_type));
return ntohs(ether->ether_type);
}
void print_mac_addr(u_char * mac_addr)
{
printf("[%02x:%02x:%02x:%02x:%02x:%02x]\n",
mac_addr[0],
mac_addr[1],
mac_addr[2],
mac_addr[3],
mac_addr[4],
mac_addr[5]);
}
u_char show_ip_header(struct ipH * ip_header)
{
printf("[*] Source IP : %s\n", inet_ntoa(ip_header->ip_src));
printf("[*] Dest IP : %s\n", inet_ntoa(ip_header->ip_dst));
return ip_header->ip_protocol;
}
uint8_t show_tcp_header(struct tcp * tcp_header)
{
printf("[-] Source Port : %d\n", ntohs(tcp_header->th_sport));
printf("[-] Dest Port :%d\n", ntohs(tcp_header->th_dport));
return tcp_header->th_off;
}
void show_data(u_char * data,int size)
{
for(int i = 0;i<size;i++)
{
if(i>16) break;
printf("%c ",data[i]);
}
printf("\n");
}