forked from aiguozhedaodan/url_recoder
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpptp_url_recorder.c
369 lines (294 loc) · 6.89 KB
/
pptp_url_recorder.c
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <linux/if.h>
#include <netinet/in.h>
#include <linux/netlink.h>
#include <linux/rtnetlink.h>
#include <arpa/inet.h>
#include <pcap.h>
#include <unistd.h>
#include <time.h>
#include <pthread.h>
#define PROMISC 1
#define SNAPLEN 1600
static char log_file[] = "/var/log/url_record.txt";
static char pidfile[] = "/var/run/url_recorder";
static FILE* log_fp;
static pthread_mutex_t log_mutex;
static int nic_bitmap[20];
static char nic[10];
int log_init()
{
int ret;
pthread_mutex_init(&log_mutex,NULL);
log_fp = fopen(log_file,"a+");
if (log_fp == NULL){
printf("open log file failed\n");
}
return 0;
}
int logging(char *data,int len)
{
time_t timet;
struct tm *p;
char str_time[100];
timet = time(NULL);
p = localtime(&timet);
strftime(str_time,sizeof(str_time),"<--%m-%d %H:%M:%S--> ",p);
pthread_mutex_lock(&log_mutex);
fwrite(str_time,strlen(str_time),1,log_fp);
fwrite(data,len,1,log_fp);
fputc('\n',log_fp);
fflush(log_fp);
pthread_mutex_unlock(&log_mutex);
return 0;
}
int create_pidfile()
{
return 0;
}
int is_http_request(const unsigned char* buf, int len)
{
if (buf[0] == 'G' && buf[1] == 'E' && buf[2] == 'T'){
return 1;
}
if (buf[0] == 'P' && buf[1] == 'O' && buf[2] == 'S' && buf[3] == 'T'){
return 2;
}
return 0;
}
int http_hdr_len(const unsigned char* buf, int len)
{
int i = 0;
int hdr_len = 0;
for (i = 0; i < len - 3; i++){
if (buf[i] == '\r' && buf[i + 1] == '\n'
&& buf[i + 2] == '\r' && buf[i + 3] == '\n'){
hdr_len = i;
break;
}
}
return hdr_len;
}
int get_url(unsigned char* buf, int *url_len, const unsigned char *pkt_data, int hdr_len)
{
const unsigned char* cur;
int host_len;
int uri_len;
memcpy(buf, "http://", 7);
buf += 7;
*url_len = 7;
cur = strstr(pkt_data, "Host:");
if (cur == NULL)
return -1;
cur += 6;
host_len = field_len(cur, hdr_len - (cur - pkt_data));
if (host_len && (*url_len +host_len<1200)){
memcpy(buf, cur, host_len);
buf += host_len;
*url_len += host_len;
}
else
{
return -1;
}
cur = pkt_data;
if (pkt_data[0] == 'G')
cur += 4;
else if (pkt_data[0] == 'P'){
cur += 5;
}
uri_len = field_len(cur, hdr_len - (cur - pkt_data));
uri_len -= 9;
if((*url_len + uri_len) < 1200){
memcpy(buf, cur, uri_len);
*url_len += uri_len;
}
return 0;
}
int field_len(const unsigned char* buf, int len)
{
int i = 0;
int field_len = 0;
for (i = 0; i < len-1; i++){
if (buf[i] == '\r' && buf[i + 1] == '\n'){
field_len = i;
break;
}
}
return field_len;
}
int is_tcp(const unsigned char* buf, int len)
{
if (len < 54)
return 0;
//l2proto = Linux cooked capture
//if (buf[12] != 0x08 || buf[13] != 0x00){
if (buf[14] != 0x08 || buf[15] != 0x00){
return 0;
}
//if (buf[23] != 0x06){//tcp flag
if (buf[25] != 0x06){//tcp flag
return 0;
}
return 1;
}
void callback(unsigned char *user, const struct pcap_pkthdr *h, const unsigned char *bytes)
{
int len;
const unsigned char *buf;
const unsigned char *cur;
unsigned char url[1024];
int url_len = 0;
char data[1400];
int is_req;
int hdr_len;
int ret;
buf = bytes;
len = h->caplen;
cur = buf;
if (!is_tcp(buf, len)){
return;
}
//cur += 54;
//len -= 54;//tcp hdr ip hdr
cur += 56;
len += 56;
is_req = is_http_request(cur, len);
hdr_len = http_hdr_len(cur, len);
switch (is_req){
case 0:
break;
case 1:
ret = get_url(url,&url_len, cur, hdr_len);
if (ret == 0){
logging(url,url_len);
//printf("url_len:%d\n",url_len);
}
break;
case 2:
ret = get_url(url,&url_len, cur, hdr_len);
if (ret == 0){
logging(url,url_len);
}
//get_data(data, curl, len);
break;
default:
break;
}
}
void *capture_thread(void* arg)
{
char dev[10];
pcap_t *pt;
char errbuf[PCAP_ERRBUF_SIZE];
strcpy(dev,(char*)arg);
dev[9] = 0;
pt = pcap_open_live(dev, SNAPLEN, PROMISC, -1, errbuf);
if (pt == NULL){
printf("open dev failed\n");
return;
}
pcap_loop(pt, -1, callback,NULL);
// pthread_detach(pthread_self());
printf("capture thread create\n");
}
int start_capture(char *dev)
{
char dev_vpath[30] = "/sys/class/net/";
pthread_t thread;
pthread_attr_t attr;
strcpy(nic,dev);
sleep(1);
strcat(dev_vpath,nic);
if(access(dev_vpath,0) == 0){
int ret;
pthread_attr_init(&attr);
pthread_attr_setdetachstate(&attr,PTHREAD_CREATE_DETACHED);
pthread_create(&thread,&attr,capture_thread,nic);
pthread_attr_destroy(&attr);
}
return 0;
}
void parseBinaryNetLinkMessage(struct nlmsghdr *nlh)
{
int len = nlh->nlmsg_len - sizeof(*nlh);
struct ifinfomsg *ifi;
if(sizeof(*ifi) > (size_t)len){
printf("Got a short message\n");
return ;
}
ifi = (struct ifinfomsg*)NLMSG_DATA(nlh);
if((ifi->ifi_flags & IFF_LOOPBACK) != 0){
return ;
}
struct rtattr *rta = (struct rtattr*)
((char*)ifi + NLMSG_ALIGN(sizeof(*ifi)));
len = NLMSG_PAYLOAD(nlh,sizeof(*ifi));
while(RTA_OK(rta,len)){
switch(rta->rta_type){
case IFLA_IFNAME:
{
char ifname[IFNAMSIZ];
int up;
int id;
snprintf(ifname,sizeof(ifname),"%s",
(char*)RTA_DATA(rta));
up = (ifi->ifi_flags & IFF_RUNNING)? 1 : 0;
if (up && ((ifname[0] == 'p')&&(ifname[1] == 'p')&&
(ifname[2] == 'p'))){
printf("msg from:%s",ifname);
sscanf(ifname+3,"%d",&id);
if(nic_bitmap[id])
break;
start_capture(ifname);
nic_bitmap[id] = 1;
printf("%s %d\n",ifname,up);
}
if (!up && ((ifname[0] == 'p')&&(ifname[1] == 'p')&&
(ifname[2] == 'p'))){
sscanf(ifname+3,"%d",(int*)&id);
nic_bitmap[id] = 0;
printf("%s %d\n",ifname,up);
}
}
}
rta = RTA_NEXT(rta,len);
}
}
int main(int argc,char** argv)
{
struct sockaddr_nl addr;
struct nlmsghdr *nlh;
char buffer[4096];
int sock,len;
daemon(0,0);
create_pidfile();
log_init();
if((sock = socket(AF_NETLINK,SOCK_RAW,NETLINK_ROUTE)) == -1){
printf("open NETLINK_ROUTE socket failed\n");
goto exit;
}
memset(&addr,0,sizeof(addr));
addr.nl_family = AF_NETLINK;
addr.nl_groups = RTMGRP_LINK |RTMGRP_IPV4_IFADDR;
if(bind(sock,(struct sockaddr*)&addr,sizeof(addr)) == -1){
printf("bind failed\n");
goto exit;
}
while((len = recv(sock,buffer,4096,0)) > 0){
nlh = (struct nlmsghdr*)buffer;
while((NLMSG_OK(nlh,len)) && (nlh->nlmsg_type != NLMSG_DONE)){
if(nlh->nlmsg_type == RTM_NEWLINK){
parseBinaryNetLinkMessage(nlh);
}
nlh = NLMSG_NEXT(nlh,len);
}
}
close(sock);
exit:
exit(0);
}