forked from Cisco-Talos/file2pcap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhttp.c
214 lines (139 loc) · 6.76 KB
/
http.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
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#define __FAVOR_BSD
#include <netinet/in_systm.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#include <netinet/tcp.h>
#include <stdlib.h>
#include "file2pcap.h"
#include "helpers.h"
extern int packetLen4, packetLen6;
extern struct stat fileStat;
/**************************************************************************/
int httpGetRequest(struct handover *ho) {
char *encoded=NULL;
char requestEnd[] = "Host: wrl\r\n"
"User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.8.1.17) Gecko/20081007 Firefox/2.0.0.17\r\n"
"Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5\r\n"
"Accept-Language: en-us,en;q=0.5\r\n"
"Accept-Encoding: gzip,deflate\r\n"
"Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7\r\n"
"Keep-Alive: 300\r\n"
"Connection: keep-alive\r\n\r\n";
char tmp[700];
encoded=URLencoder(ho->srcFile);
if(encoded==NULL)
exit(-1);
snprintf(tmp, sizeof(tmp)-1,"GET /file2pcap/%s HTTP/1.1\r\n%s", encoded, requestEnd);
tcpSendString(ho, tmp, TO_SERVER);
return(0);
}
/**************************************************************************/
int httpPostRequest(struct handover *ho) {
char tmp[850];
int fullSize=0;
char requestStart[] = "POST /file2pcap.cgi HTTP/1.1\r\n"
"Host: wrl\r\n"
"User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.8.1.17) Gecko/20081007 Firefox/2.0.0.17\r\n"
"Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5\r\n"
"Accept-Language: en-us,en;q=0.5\r\n"
"Content-Length: ";
char requestCenter[] = "\r\nContent-Type: multipart/form-data; boundary=---------------------------8173728711543081858379436204\r\n"
"Accept-Encoding: gzip,deflate\r\n"
"Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7\r\n"
"Keep-Alive: 300\r\n"
"Connection: keep-alive\r\n\r\n"
"-----------------------------8173728711543081858379436204\r\n"
"Content-Disposition: form-data; name=\"file\"; filename=\"";
char requestEnd[] = "\"\r\nContent-Type: application/octet-stream\r\n\r\n";
//Calculating the content-length: everything after the http header
fullSize = strlen("-----------------------------8173728711543081858379436204\r\n") +
strlen("Content-Disposition: form-data; name=\"file\"; filename=\"") +
strlen(ho->srcFile) +
strlen("\"\r\nContent-Type: application/octet-stream\r\n\r\n") +
(int)fileStat.st_size +
strlen("\r\n-----------------------------8173728711543081858379436204--\r\n");
snprintf(tmp, sizeof(tmp)-1,"%s%d%s%s%s", requestStart, fullSize, requestCenter, ho->srcFile, requestEnd);
tcpSendString(ho, tmp, TO_SERVER);
return(0);
}
/**************************************************************************************************/
int httpPostFinalBoundary(struct handover *ho) {
char finalBoundary[] = "\r\n-----------------------------8173728711543081858379436204--\r\n";
tcpSendString(ho, finalBoundary, TO_SERVER);
return 0;
}
/************************************************************************************************************************/
int httpGetRequestAcknowledge(struct handover *ho) {
char http_header[] = "HTTP/1.1 200 Ok\r\n"
"Date: Wed, 29 Jul 2009 13:35:26 GMT\r\n"
"Server: Apache/2.2.3 (Debian) PHP/5.2.0-8+etch10 mod_ssl/2.2.3 OpenSSL/0.9.8c\r\n"
"Last-Modified: Sun, 20 Jan 2008 12:01:21 GMT\r\n"
"ETag: \"a801c-1bbd1c-22416640\"\r\n"
"Accept-Ranges: bytes\r\n"
"Content-Length: ";
char http_header_2[] = "\r\n"
"Keep-Alive: timeout=15, max=99\r\n"
"Connection: Keep-Alive\r\n"
"Content-Type: application/octet-stream\r\n\r\n";
char headerBuffer[500];
snprintf(headerBuffer,sizeof(headerBuffer)-1,"%s%d%s", http_header, (unsigned int)fileStat.st_size, http_header_2);
//this line replaces all the old code below. And it also gets the ethernet addresses right, unlike before
tcpSendString(ho, headerBuffer, FROM_SERVER);
return(0);
}
/**************************************************************************************************************/
int httpTransferFile(struct handover *ho) {
int packetLen;
unsigned int count;
char buffer[1500];
struct pcap_packet_header ph;
if(ho->inFile != NULL)
rewind(ho->inFile);
//initialize ph with the current values
ph.time = ho->time;
ph.usec = ho->usec;
if(ho->ipV == 4)
packetLen = packetLen4;
else
packetLen = packetLen6;
while(!(feof(ho->inFile)))
{
count=read(fileno(ho->inFile), buffer, 1200);
if(count<=0)
{
ho->time = ph.time;
ho->usec = ph.usec;
return 0; //FIXME ??
}
ph.usec += INTERVAL;
if((ph.usec + INTERVAL) >= 1000000)
{
ph.time+=1;
ph.usec=0;
}
ph.length1 = packetLen + count;
ph.length2 = packetLen + count;
write(fileno(ho->outFile), &ph, sizeof(struct pcap_packet_header));
if(ho->direction == FROM_SERVER)
write(fileno(ho->outFile), ho->fromEther, sizeof(ho->fromEther)-1);
else
write(fileno(ho->outFile), ho->toEther, sizeof(ho->toEther)-1);
craftTcp(buffer, count, ho->direction == FROM_SERVER ? FROM_SERVER : TO_SERVER, TH_ACK|TH_PUSH, ho);
//and now send the ack
ph.usec+=INTERVAL;
ph.length1=packetLen;
ph.length2=packetLen;
write(fileno(ho->outFile), &ph, sizeof(struct pcap_packet_header));
if(ho->direction == FROM_SERVER)
write(fileno(ho->outFile), ho->toEther, sizeof(ho->toEther)-1);
else
write(fileno(ho->outFile), ho->fromEther, sizeof(ho->fromEther)-1);
craftTcp(NULL,0, ho->direction == FROM_SERVER ? TO_SERVER : FROM_SERVER, TH_ACK, ho); //direction - client to server
}
ho->time = ph.time;
ho->usec = ph.usec;
return(0);
}