-
Notifications
You must be signed in to change notification settings - Fork 37
/
ftp.c
231 lines (166 loc) · 6.36 KB
/
ftp.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
#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 "file2pcap.h"
extern int packetLen4, packetLen6;
/**************************************************************************/
int ftpCommandsStartActive(struct handover *ho) {
char buffer[200];
char ftpBanner[] = "220 Welcome to file2pcap ftp server\r\n";
char username[] = "USER wrl\r\n";
char usernameReply[] = "331 Please specify the password\r\n";
char password[] = "PASS wrl\r\n";
char loginSuccessful[] = "230 Login successful.\r\n";
char syst[] = "SYST\r\n";
char systReply[] = "UNIX Type: L8\r\n";
char typeI[] = "TYPE I\r\n";
char typeIReply[] = "200 Switching to Binary mode\r\n";
char portCommand[] = "PORT 192,168,0,1,197,17\r\n"; //FIXME - port 50449 - hard-coded IP:Port is bad
char portCommandReply[] = "200 Port command successful\r\n";
char eprtCommand[] = "EPRT |2|2a00:1450:4007:80d::200e|50449|\r\n";
char eprtCommandReply[] = "200 EPRT: Command successful\r\n";
char retr[] = "RETR ";
char dataConnection[] = "150 Opening BINARY mode data connection\r\n";
tcpSendString(ho, ftpBanner, FROM_SERVER);
tcpSendString(ho, username, TO_SERVER);
tcpSendString(ho, usernameReply, FROM_SERVER);
tcpSendString(ho, password, TO_SERVER);
tcpSendString(ho, loginSuccessful, FROM_SERVER);
tcpSendString(ho, syst, TO_SERVER);
tcpSendString(ho, systReply, FROM_SERVER);
tcpSendString(ho, typeI, TO_SERVER);
tcpSendString(ho, typeIReply, FROM_SERVER);
if(ho->ipV == 4)
{
tcpSendString(ho, portCommand, TO_SERVER);
tcpSendString(ho, portCommandReply, FROM_SERVER);
}
else if(ho->ipV == 6)
{
tcpSendString(ho, eprtCommand, TO_SERVER);
tcpSendString(ho, eprtCommandReply, FROM_SERVER);
}
memset(buffer, 0, sizeof(buffer));
snprintf(buffer, sizeof(buffer)-1, "%s%s\r\n", retr, ho->srcFile);
tcpSendString(ho, buffer, TO_SERVER);
tcpSendString(ho, dataConnection, FROM_SERVER);
//FIXME - randomize port and return the value
return 50449;
}
/**************************************************************************/
int ftpCommandsStartPassive(struct handover *ho) {
char buffer[200];
char ftpBanner[] = "220 Welcome to file2pcap ftp server\r\n";
char username[] = "USER wrl\r\n";
char usernameReply[] = "331 Please specify the password\r\n";
char password[] = "PASS wrl\r\n";
char loginSuccessful[] = "230 Login successful.\r\n";
char syst[] = "SYST\r\n";
char systReply[] = "UNIX Type: L8\r\n";
char typeI[] = "TYPE I\r\n";
char typeIReply[] = "200 Switching to Binary mode\r\n";
char pasvCommand[] = "PASV\r\n";
char pasvCommandReply[] = "227 Entering Passive Mode (173,37,145,84,197,17)\r\n"; //port 50449
char epsvCommand[] = "EPSV\r\n";
char epsvCommandReply[] = "229 Entering Extended Passive Mode (|||50449|)\r\n";
char retr[] = "RETR ";
char dataConnection[] = "150 Opening BINARY mode data connection\r\n";
tcpSendString(ho, ftpBanner, FROM_SERVER);
tcpSendString(ho, username, TO_SERVER);
tcpSendString(ho, usernameReply, FROM_SERVER);
tcpSendString(ho, password, TO_SERVER);
tcpSendString(ho, loginSuccessful, FROM_SERVER);
tcpSendString(ho, syst, TO_SERVER);
tcpSendString(ho, systReply, FROM_SERVER);
tcpSendString(ho, typeI, TO_SERVER);
tcpSendString(ho, typeIReply, FROM_SERVER);
if(ho->ipV == 4)
{
tcpSendString(ho, pasvCommand, TO_SERVER);
tcpSendString(ho, pasvCommandReply, FROM_SERVER);
}
else if(ho->ipV == 6)
{
tcpSendString(ho, epsvCommand, TO_SERVER);
tcpSendString(ho, epsvCommandReply, FROM_SERVER);
}
memset(buffer, 0, sizeof(buffer));
snprintf(buffer, sizeof(buffer)-1, "%s%s\r\n", retr, ho->srcFile);
tcpSendString(ho, buffer, TO_SERVER);
tcpSendString(ho, dataConnection, FROM_SERVER);
//FIXME - randomize port and return the value
return 50449;
}
/***************************************************************************************************************/
int ftpCommandsEnd(struct handover *ho) {
char transferComplete[] = "226 Transfer complete\r\n";
char quit[] = "QUIT\r\n";
char goodbye[] = "221 Goodbye.\r\n";
tcpSendString(ho, transferComplete, FROM_SERVER);
tcpSendString(ho, quit, TO_SERVER);
tcpSendString(ho, goodbye, FROM_SERVER);
return 0;
}
/**************************************************************************************************************/
int ftpTransferFile(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, ho->blockSize);
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 == TO_SERVER)
write(fileno(ho->outFile), ho->toEther, sizeof(ho->toEther)-1);
else
write(fileno(ho->outFile), ho->fromEther, sizeof(ho->fromEther)-1);
craftTcp(buffer, count, ho->direction, 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 == TO_SERVER)
{
write(fileno(ho->outFile), ho->fromEther, sizeof(ho->fromEther)-1);
craftTcp(NULL,0, FROM_SERVER, TH_ACK, ho);
}
else
{
write(fileno(ho->outFile), ho->toEther, sizeof(ho->toEther)-1);
craftTcp(NULL,0, TO_SERVER, TH_ACK, ho);
}
}
ho->time = ph.time;
ho->usec = ph.usec;
return(0);
}