-
Notifications
You must be signed in to change notification settings - Fork 4
/
FTP.cs
350 lines (336 loc) · 16.4 KB
/
FTP.cs
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
using FluentFTP;
using FluentFTP.Client.BaseClient;
using FluentFTP.Exceptions;
using System;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Net;
namespace Reecon
{
class FTP // Port 21
{
public static (string, string) GetInfo(string target, int port)
{
string ftpUsername = "";
string ftpLoginInfo = "";
try
{
ftpLoginInfo = FtpLogin2(target, port, ftpUsername);
}
catch (Exception ex)
{
Console.WriteLine("Rewrite Error: " + ex.Message);
}
ftpLoginInfo = ftpLoginInfo.Trim(Environment.NewLine.ToCharArray());
return ("FTP", ftpLoginInfo);
}
public static string FtpLogin2(string target, int port, string username = "", string password = "")
{
// Console.WriteLine("In FtpLogin2");
string ftpLoginResult = "";
if (username == "")
{
username = "anonymous";
}
NetworkCredential networkCredential = new NetworkCredential(username, password);
FtpClient ftpClient = new FtpClient(target, networkCredential, port);
ftpClient.Config.EncryptionMode = FtpEncryptionMode.Auto; // Port 990 for Implicit
ftpClient.ValidateCertificate += new FtpSslValidation(OnValidateCertificate);
ftpClient.LegacyLogger = OnLogMessage;
void OnLogMessage(FtpTraceLevel t, string loggerString)
{
if (loggerString.StartsWith("Response: 220 "))
{
// Remove the logger-related stuff
string bannerMessage = loggerString.Remove(0, "Response: ".Length);
// logger messages end off with a time stamp in some weird format - [738959.568d]
bannerMessage = bannerMessage.Substring(0, bannerMessage.LastIndexOf("[") - 1);
ftpLoginResult += ParseBannerMessageResponse(bannerMessage) + Environment.NewLine;
}
/*
* For debugging
else
{
Console.WriteLine("Woof: " + loggerString);
}
*/
}
void OnValidateCertificate(BaseFtpClient control, FtpSslValidationEventArgs e)
{
string issuer = e.Certificate.Issuer;
string subject = e.Certificate.Subject;
ftpLoginResult += "-- SSL Cert Issuer: " + issuer + Environment.NewLine;
if (issuer != subject)
{
ftpLoginResult += "-- SSL Cert Subject: " + subject;
}
/*
Console.WriteLine("Issuer: " + e.Certificate.Issuer);
Console.WriteLine("Subject: " + e.Certificate.Subject);
Console.WriteLine("Raw: " + e.Certificate.GetRawCertDataString());
*/
// add logic to test if certificate is valid here
e.Accept = true;
}
try
{
ftpClient.Connect();
if (ftpClient.IsConnected)
{
string portInfo = port == 21 ? "" : $":{port}";
ftpLoginResult += "- " + $"Anonymous login allowed -> ftp ftp://anonymous:@{target}{portInfo}".Recolor(Color.Orange) + Environment.NewLine;
// Console.WriteLine("FtpLogin2 - Connected");
if (ftpClient.IsAuthenticated)
{
ftpLoginResult += "-- OS: " + ftpClient.ServerOS + Environment.NewLine;
// Console.WriteLine("FtpLogin2 - Auth'd");
FtpListItem[] items = ftpClient.GetListing();
foreach (var item in items)
{
// Ack
ftpLoginResult += "-- " + item.FullName + " (" + (item.Type == FtpObjectType.Directory ? "" : ("Size: " + item.Size + " -> ")) + "Perms: " + item.Chmod + " -> ";
if (item.Type == FtpObjectType.Directory)
{
ftpLoginResult += "Directory - Might want to look into this)" + Environment.NewLine;
FtpListItem[] innerItems = ftpClient.GetListing(item.FullName);
foreach (var innerItem in innerItems)
{
ftpLoginResult += "--- " + innerItem.FullName + " (Size: " + innerItem.Size + " -> Perms: " + innerItem.Chmod + " -> " + innerItem.Type + ")" + Environment.NewLine;
if (innerItem.Type == FtpObjectType.File && innerItem.Name.EndsWith(".txt"))
{
using Stream stream = ftpClient.OpenRead(innerItem.FullName);
using StreamReader reader = new(stream);
while (!reader.EndOfStream)
{
string line = reader.ReadLine();
ftpLoginResult += "---- Text: " + line + Environment.NewLine;
}
}
}
}
else if (item.Type == FtpObjectType.File)
{
ftpLoginResult += "File)";
}
ftpLoginResult += Environment.NewLine;
if (item.Type == FtpObjectType.File && item.Name.EndsWith(".txt"))
{
using Stream stream = ftpClient.OpenRead(item.FullName);
using StreamReader reader = new(stream);
while (!reader.EndOfStream)
{
string line = reader.ReadLine();
ftpLoginResult += "--- Text: " + line + Environment.NewLine;
}
}
}
}
}
}
catch (FtpAuthenticationException aex)
{
ftpLoginResult += "- OS: " + ftpClient.ServerOS + Environment.NewLine;
string banner = ftpClient.LastReplies.First(x => x.Code == "220").Message;
if (username == "anonymous" && aex.CompletionCode == "530")
{
ftpLoginResult += "- No anonymous access permitted";
}
else
{
ftpLoginResult += "- Ftp.cs - Unknown FtpAuthenticationException: " + aex.Message; ;
}
}
catch (Exception ex)
{
if (ftpClient.LastReplies == null)
{
if (ex.Message == "Unable to read data from the transport connection: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond.")
{
ftpLoginResult += "- Might've been an FTP Server, but it stopped responding." + Environment.NewLine;
}
else
{
ftpLoginResult += "- Weird FTP Server - Bug Reelix: " + ex.Message + Environment.NewLine;
}
}
else
{
string banner = ftpClient.LastReplies.First(x => x.Code == "220").Message;
ftpLoginResult += "- Banner: " + banner + Environment.NewLine;
ftpLoginResult += "- Ftp.cs - Unknown Exception - Bug Reelix: " + ex.Message; ;
}
}
return ftpLoginResult;
}
public static string FtpLogin(string target, int port, string username = "", string password = "")
{
string ftpLoginResult = "";
string ftpServer = target;
if (!ftpServer.StartsWith("ftp://"))
{
ftpServer = $"ftp://{ftpServer}:{port}";
}
// https://github.com/dotnet/platform-compat/blob/master/docs/DE0003.md
// About that....
#pragma warning disable SYSLIB0014 // Type or member is obsolete
// Test
FtpWebRequest request = (FtpWebRequest)WebRequest.Create(ftpServer);
#pragma warning restore SYSLIB0014 // Type or member is obsolete
request.Timeout = 5000;
request.UseBinary = true; // Better for downloading files if we ever need
request.UsePassive = true; // A better way to receive file listing
request.KeepAlive = false; // Closes FTP after we're done
request.Method = WebRequestMethods.Ftp.PrintWorkingDirectory;
request.Credentials = new NetworkCredential(username, password);
// FtpState state = new FtpState();
// state.Request = request;
// state.FileName = fileName;
try
{
FtpWebResponse response = (FtpWebResponse)request.GetResponse();
// If it gets here - It's connected!
string bannerMessage = ParseBannerMessageResponse(response.BannerMessage);
ftpLoginResult += bannerMessage + Environment.NewLine;
if (response.WelcomeMessage.Trim() != "230 Login successful.")
{
ftpLoginResult += "- Welcome Message: " + response.WelcomeMessage.Trim() + Environment.NewLine;
}
if (response.SupportsHeaders)
{
WebHeaderCollection headers = response.Headers;
if (headers != null && headers.Count != 0)
{
ftpLoginResult += "- Headers (Contact Reelix): " + string.Join(",", headers.AllKeys) + Environment.NewLine;
}
}
if (string.IsNullOrEmpty(username) || username == "anonymous")
{
ftpLoginResult += "- " + "Anonymous login allowed (Username: anonymous Password: *Leave Blank*)".Recolor(Color.Orange) + Environment.NewLine;
}
else
{
Console.WriteLine("Woof!");
}
return ftpLoginResult.Trim(Environment.NewLine.ToCharArray());
}
catch (WebException ex)
{
if (ex.Message == "Unable to connect to the remote server")
{
return "- Unable to connect :<";
}
if (ex.Response != null)
{
FtpWebResponse response = (FtpWebResponse)ex.Response;
if (response != null)
{
if (response.BannerMessage != null && response.StatusDescription != null)
{
string bannerMessage = ParseBannerMessageResponse(response.BannerMessage.Trim());
ftpLoginResult += bannerMessage + Environment.NewLine;
ftpLoginResult += "- Status: " + response.StatusDescription.Trim() + Environment.NewLine;
}
else
{
ftpLoginResult += "- Unable to get any FTP response: " + ex.Message + Environment.NewLine;
try
{
ftpLoginResult += "- Banner: " + General.BannerGrab(target, port);
}
catch (Exception iex)
{
ftpLoginResult += "- Unable to get any banner response: " + iex.Message;
}
}
}
else
{
ftpLoginResult += "- Unable to get FTP response: " + ex.Message + Environment.NewLine;
try
{
ftpLoginResult += "- Banner: " + General.BannerGrab(target, port);
}
catch (Exception iex)
{
ftpLoginResult += "- Unable to get any banner response: " + iex.Message;
}
}
return ftpLoginResult.Trim(Environment.NewLine.ToCharArray());
}
else
{
ftpLoginResult += "- Unable to get any any response: " + ex.Message;
return ftpLoginResult;
}
}
}
private static string ParseBannerMessageResponse(string bannerMessage)
{
string toReturn = "";
if (!string.IsNullOrEmpty(bannerMessage))
{
bannerMessage = bannerMessage.Trim();
if (bannerMessage.StartsWith("220 "))
{
bannerMessage = bannerMessage.Remove(0, 4);
if (bannerMessage.StartsWith("(") && bannerMessage.EndsWith(")"))
{
bannerMessage = bannerMessage.Remove(0, 1);
bannerMessage = bannerMessage.Remove(bannerMessage.Length - 1, 1);
}
}
toReturn += "- Banner: " + bannerMessage + Environment.NewLine;
// All versions of ProFTPD 1.3.5 before 1.3.5a
// All versions of ProFTPD 1.3.6 before 1.3.6rc1
if (bannerMessage.Contains("ProFTPD "))
{
if (bannerMessage.Contains("ProFTPD 1.3.5") || bannerMessage.Contains("ProFTPD 1.3.6"))
{
toReturn += "-- " + "Vulnerable ProFTPD Version Detected (Potential RCE) - CVE-2015-3306".Recolor(Color.Orange) + Environment.NewLine;
}
else
{
toReturn += "-- ProFTPD detected - Maybe vulnerable - Bug Reelix" + Environment.NewLine;
}
}
}
return toReturn.Trim(Environment.NewLine.ToCharArray());
}
public static string TryListFiles(string ftpServer, int port, bool usePassive, string username = "", string password = "")
{
string toReturn = "";
FtpClient client = new(ftpServer, username, password, port);
client.Connect();
/*
Console.WriteLine("Test Type: " + client.ServerType.ToString());
Console.WriteLine("Test Handler: " + client.ServerHandler);
Console.WriteLine("Test OS: " + client.ServerOS);
*/
toReturn += "-- OS Type: " + client.ServerOS + Environment.NewLine;
FtpListItem[] itemList = client.GetListing("/", FtpListOption.AllFiles);
if (itemList.Length == 0)
{
toReturn += "-- No Files Or Folders Found" + Environment.NewLine;
}
foreach (FtpListItem item in client.GetListing("/", FtpListOption.AllFiles))
{
string fileType = "";
if (item.Type == FtpObjectType.Directory)
{
fileType = " (Directory - Might want to look into this)";
}
else if (item.Type == FtpObjectType.File)
{
fileType = " (File)";
}
else
{
fileType = " (Fix Me!)";
}
toReturn += "-- " + item.Name + fileType + Environment.NewLine;
}
return toReturn;
}
}
}