-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathFtp.cs
More file actions
256 lines (236 loc) · 11.3 KB
/
Ftp.cs
File metadata and controls
256 lines (236 loc) · 11.3 KB
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
using FluentFTP;
using FluentFTP.Client.BaseClient;
using FluentFTP.Exceptions;
// Note: FtpWebRequest isn't used due to https://github.com/dotnet/platform-compat/blob/master/docs/DE0003.md
using System;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Net;
namespace Reecon
{
internal static class Ftp // File Transfer Protocol - Port 21
{
public static (string PortName, string PortData) GetInfo(string target, int port)
{
string ftpUsername = "";
string ftpLoginInfo = "";
try
{
ftpLoginInfo = FtpLogin(target, port, ftpUsername);
}
catch (Exception ex)
{
Console.WriteLine("Rewrite Error: " + ex.Message);
General.HandleUnknownException(ex);
}
ftpLoginInfo = ftpLoginInfo.Trim(Environment.NewLine.ToCharArray());
return ("FTP", ftpLoginInfo);
}
public static string FtpLogin(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 + Environment.NewLine;
}
/*
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");
// You can connect without being auth'd, but you can still read stuff even if so
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") || innerItem.Name.EndsWith(".sh"))
{
using Stream stream = ftpClient.OpenRead(innerItem.FullName);
using StreamReader reader = new(stream);
while (!reader.EndOfStream)
{
string line = reader.ReadLine() ?? "Line Null Error 2 in FTP.cs";
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") || item.Name.EndsWith(".sh"))
{
using Stream stream = ftpClient.OpenRead(item.FullName);
using StreamReader reader = new(stream);
while (!reader.EndOfStream)
{
string line = reader.ReadLine() ?? "Line Null Error in FTP.cs";
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;
General.HandleUnknownException(ex);
}
}
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;
}
}
}