forked from mifan/url_recoder
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathurl_recoder.c
249 lines (196 loc) · 4.2 KB
/
url_recoder.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
#include <stdio.h>
#include <time.h>
#include <string.h>
#include <stdlib.h>
#include <pcap.h>
#include <unistd.h>
#define PROMISC 1
#define SNAPLEN 1600
char log_file[] = "/var/log/url_record.txt";
char pidfile[] = "/var/run/url_recoder";
FILE* log_fp;
int log_init()
{
int ret;
char mode[2] = {0,'+'};
ret = access(log_file,0);
if(ret == 0){
mode[0] = 'a';
}
else if(ret == -1){
mode[0] = 'w';
}
else{
printf("log init error\n");
exit(0);
}
log_fp = fopen(log_file,mode);
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);
fwrite(str_time,strlen(str_time),1,log_fp);
fwrite(data,len,1,log_fp);
fputc('\n',log_fp);
fflush(log_fp);
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;
if (buf[12] != 0x08 || buf[13] != 0x00){
return 0;
}
if (buf[23] != 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
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;
}
}
int main(int argc,char** argv)
{
pcap_t *pt;
char dev[10];
char errbuf[PCAP_ERRBUF_SIZE];
if(argc != 2){
printf("./url_recoder eth0\n");
exit(0);
}
daemon(0,0);
create_pidfile();
log_init();
strcpy(dev,argv[1]);
dev[9] = 0;
pt = pcap_open_live(dev, SNAPLEN, PROMISC, -1, errbuf);
if (pt == NULL){
printf("open dev failed\n");
exit(0);
}
pcap_loop(pt, -1, callback,NULL);
}