-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathSMB.cs
314 lines (301 loc) · 16.1 KB
/
SMB.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
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Runtime.InteropServices;
namespace Reecon
{
class SMB //445
{
public static (string PortName, string PortData) GetInfo(string target, int port)
{
// if smb2
// if 2008
// if 2008 before r2 -- CVE-2009-3103
string toReturn = "";
// SMB1
try
{
toReturn += "- SMBv1 Response" + Environment.NewLine;
NetworkStream? stream = null;
// Console.WriteLine($"Attempting to connect to {target}:{port}...");
using (TcpClient client = new TcpClient())
{
client.SendTimeout = 5000;
client.ReceiveTimeout = 15000;
var cT = client.ConnectAsync(target, 445);
if (!cT.Wait(TimeSpan.FromSeconds(5)))
{ /* Timeout */ }
cT.GetAwaiter().GetResult();
// Console.WriteLine("Connection successful.");
stream = client.GetStream();
// --- Stage 1: Negotiate ---
// Console.WriteLine("\n--- Stage 1: Sending Negotiate Request ---");
byte[] negReq = SMB1_Protocol.CreateNegotiateRequest();
stream.Write(negReq, 0, negReq.Length);
stream.Flush();
// Console.WriteLine("Negotiate request sent.");
// Console.WriteLine("Waiting for Negotiate response...");
byte[] negotiateResponse = SMB1_Protocol.ReadResponse(stream);
if (negotiateResponse.Length < 32)
{
throw new InvalidDataException("Negotiate response too short.");
}
if (!(negotiateResponse[4] == 0x72 && negotiateResponse[5] == 0))
{
uint s = BitConverter.ToUInt32(negotiateResponse, 5);
throw new InvalidOperationException($"Negotiate failed: 0x{s:X8}");
}
// Console.WriteLine("Negotiate successful (basic check).");
// Console.WriteLine("---------------------------------");
// Console.WriteLine("\n--- PARSED NEGOTIATE RESPONSE (Info Mode) ---");
// toReturn += $"-- Negotiate Response Hex ({negotiateResponse.Length} bytes): {BitConverter.ToString(negotiateResponse).Replace("-", "")}" + Environment.NewLine;
SMB1_Protocol.NegotiateResponse pResp = SMB1_Protocol.ParseNegotiateResponse(negotiateResponse, SMB1_Protocol.DIALECTS);
// Display pResp fields...
// toReturn += $"-- MID: {pResp.Mid}" + Environment.NewLine;
toReturn += $"-- NTStatus: 0x{pResp.NTStatus:X8} ({(pResp.IsSuccessNegotiation ? "Success/MoreProcessing" : "Failure")})" + Environment.NewLine;
toReturn += $"-- Selected Dialect: {pResp.SelectedDialect} (Index: {(pResp.DialectIndex == 0xFFFF ? "N/A" : pResp.DialectIndex.ToString())})" + Environment.NewLine;
// toReturn += $"-- Server Flags2: 0x{pResp.SmbFlags2:X4} (Unicode Supported by Server: {pResp.SupportsUnicode})" + Environment.NewLine; // Use property here
toReturn += $"-- Security Mode: {pResp.GetSecurityModeDescription()} (Raw: {pResp.SecurityMode?.ToString("X2") ?? "N/A"})" + Environment.NewLine;
// toReturn += $"-- Capabilities: Raw=0x{pResp.Capabilities ?? 0:X8}, ExtSec={pResp.SupportsExtendedSecurity}" + Environment.NewLine;
var caps = pResp.GetCapabilityList();
if (caps.Count > 0)
{
toReturn += $"-- Capabilities List: {string.Join(", ", caps)}" + Environment.NewLine;
}
if (pResp.MaxBufferSize.HasValue)
{
// toReturn += $"-- Max Buffer Size: {pResp.MaxBufferSize}" + Environment.NewLine;
}
if (pResp.MaxMpxCount.HasValue)
{
// toReturn += $"-- Max Mpx Count: {pResp.MaxMpxCount}" + Environment.NewLine;
}
if (pResp.Challenge != null)
{
if (pResp.Challenge.Length != 8)
{
toReturn += $"-- Challenge Length: {pResp.Challenge.Length} (Assumed 8 bytes... But it's not?)" + Environment.NewLine;
}
}
else
{
Console.WriteLine($"Challenge: Not Present");
}
if (!string.IsNullOrEmpty(pResp.DomainName))
{
toReturn += $"-- " + $"Domain Name: {pResp.DomainName}".Recolor(Color.Orange) + Environment.NewLine;
}
else
{
Console.WriteLine($"Domain Name: Not Found");
}
if (!string.IsNullOrEmpty(pResp.ServerName))
{
toReturn += "-- " + $"Server Name: {pResp.ServerName}".Recolor(Color.Orange) + Environment.NewLine;
}
else
{
Console.WriteLine($"Server Name: Not Found");
}
SMB1_MS17_010.MS17010CheckResult checkResult = SMB1_MS17_010.CheckIfVulnerable(negotiateResponse, stream, target);
if (checkResult == SMB1_MS17_010.MS17010CheckResult.LikelyVulnerable)
{
toReturn += "-- " + "Vulnerable to MS17-010 (Eternal Blue) !".Recolor(Color.Orange) + Environment.NewLine;
}
else
{
toReturn += "-- MS17-010 (Eternal Blue) (Nope): " + nameof(SMB1_MS17_010.MS17010CheckResult) + Environment.NewLine;
}
}
}
catch (TimeoutException tex)
{
if (tex.Message == "Parsing an SMB1 Header" || tex.Message.StartsWith("Timeout waiting for NBSS header"))
{
toReturn += "-- SMBv1 Timed Out" + Environment.NewLine;
}
else
{
toReturn += "-- SMBv1 Timed Out in a weird placed: " + tex.Message + Environment.NewLine;
}
//Console.WriteLine("Error in SMB.cs (Parsing an SMB1 Header): " + ex.Message + "(" + exType + ")");
}
catch (Exception ex)
{
string exType = ex.GetType().Name;
Console.WriteLine("Error in SMB.cs (Parsing an SMBv1 Header?): " + ex.Message + "(" + exType + ")");
}
// SMB2
try
{
using (TcpClient client = new TcpClient(target, 445))
{
NetworkStream stream = client.GetStream();
byte[] request = SMB2_Protocol.CreateNegotiateRequest();
stream.Write(request, 0, request.Length);
byte[] response = SMB2_Protocol.ReadResponse(stream);
SMB2_Protocol.NegotiateResponse negotiateResponse = SMB2_Protocol.ParseNegotiateResponse(response.ToArray());
// Output format as per nmap :p
toReturn += "- SMBv2 Response" + Environment.NewLine;
toReturn += "-- Clock Skew: " + negotiateResponse.ClockSkew + Environment.NewLine;
toReturn += "-- Date: " + negotiateResponse.SystemTime.ToString("yyyy-MM-ddTHH:mm:ss") + Environment.NewLine;
toReturn += "-- Start Date: " + negotiateResponse.StartDate + Environment.NewLine;
toReturn += "-- Dialect (Mode): " + negotiateResponse.DialectStr + Environment.NewLine;
toReturn += "-- Security Signing: " + negotiateResponse.SigningStatus + Environment.NewLine;
}
}
catch (Exception ex)
{
string exType = ex.GetType().Name;
toReturn += $"- Error in SMB.cs ({exType}): {ex.Message}" + Environment.NewLine;
}
if (General.GetOS() == General.OS.Linux)
{
toReturn += SMB.TestAnonymousAccess_Linux(target);
}
else
{
toReturn += "- Reecon currently lacks testing anonymous SMB Access on Windows (Ironic, I know)";
}
toReturn = toReturn.Trim(Environment.NewLine.ToCharArray());
return ("SMB", toReturn);
}
private static string TestAnonymousAccess_Linux(string target)
{
if (General.IsInstalledOnLinux("smbclient", "/usr/bin/smbclient"))
{
string smbClientItems = "";
List<string> processResults = General.GetProcessOutput("smbclient", $" -L {target} --no-pass -g"); // null auth
if (processResults.Count == 1 && processResults[0].Contains("NT_STATUS_ACCESS_DENIED"))
{
return "- No Anonymous Access";
}
else if (processResults.Count == 1 && processResults[0].Contains("session setup failed: NT_STATUS_NOT_SUPPORTED"))
{
return "- NT_STATUS_NOT_SUPPORTED - Not exactly sure what to do about this one...";
}
else if (processResults.Count == 1 && processResults[0].Contains("NT_STATUS_CONNECTION_DISCONNECTED"))
{
return "- It connected, but instantly disconnected you";
}
else if (processResults.Count == 2 && processResults[0] == "Anonymous login successful" && processResults[1] == "SMB1 disabled -- no workgroup available")
{
return "- Anonymous Access Allowed - But No Shares Found";
}
else if (processResults.Count >= 1 && processResults[0].Contains("NT_STATUS_IO_TIMEOUT"))
{
return "- Timed out :(";
}
foreach (string item in processResults)
{
// type|name|comment
if (item.Trim() != "SMB1 disabled -- no workgroup available" && item.Trim() != "Anonymous login successful")
{
try
{
string itemType = item.Split('|')[0];
string itemName = item.Split('|')[1];
string itemComment = item.Split('|')[2];
smbClientItems += "- " + itemType + ": " + itemName + " " + (itemComment == "" ? "" : "(" + itemComment.Trim() + ")") + Environment.NewLine;
List<string> subProcessResults = General.GetProcessOutput("smbclient", $"//{target}/{itemName} --no-pass -c \"ls\"");
if (subProcessResults.Count > 1 && !subProcessResults.Any(x => x.Contains("NT_STATUS_ACCESS_DENIED") || x.Contains("NT_STATUS_OBJECT_NAME_NOT_FOUND")))
{
smbClientItems += "-- " + $"{itemName} has ls perms - {subProcessResults.Count} items found! -> smbclient //{target}/{itemName} --no-pass".Recolor(Color.Orange) + Environment.NewLine;
smbClientItems += "--- To download the entire contents, add -c \"recurse; prompt; mget *\"" + Environment.NewLine;
}
if (itemType == "IPC" && itemName == "IPC$")
{
if (itemComment.Contains("Samba Server"))
{
smbClientItems += "-- Samba Detected".Recolor(Color.Orange) + Environment.NewLine;
smbClientItems += "-- If version Samba 3.5.0 < 4.4.14/4.5.10/4.6.4, https://www.exploit-db.com/exploits/42084 / msfconsole -x \"use /exploit/linux/samba/is_known_pipename\"" + Environment.NewLine;
}
}
}
catch (Exception ex)
{
if (ex.Message.Contains("NT_STATUS_IO_TIMEOUT"))
{
smbClientItems = "-- Timeout - Try later :(" + Environment.NewLine;
}
else
{
Console.WriteLine($"SMB.cs - TestAnonymousAccess_Linux - Error: {ex.Message} - Invalid item: {item} - Bug Reelix!");
}
}
}
}
return smbClientItems.Trim(Environment.NewLine.ToCharArray());
}
else
{
return "- Error: Cannot find /usr/bin/smbclient - Please install it".Recolor(Color.Red);
}
}
public static void SMBBrute(string[] args)
{
// TODO: This still shows "Success" if:
// - The username doesn't exist
// - There is a space in the password
if (!RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
{
Console.WriteLine("SMB Brute only currently works in Linux - Heh :p");
return;
}
if (args.Length != 4)
{
Console.WriteLine("SMB Brute Usage: reecon -smb-brute IP Userfile Passfile");
return;
}
string ip = args[1];
string userFile = args[2];
string passFile = args[3];
if (!File.Exists(userFile))
{
Console.WriteLine("Unable to find UserFile: " + userFile);
return;
}
if (!File.Exists(passFile))
{
Console.WriteLine("Unable to find Passfile: " + passFile);
return;
}
List<string> userList = File.ReadAllLines(userFile).ToList();
List<string> passList = File.ReadAllLines(passFile).ToList();
foreach (string user in userList)
{
foreach (string pass in passList)
{
List<string> outputResult = General.GetProcessOutput("smbclient", @"-L \\\\" + ip + " -U" + user + "%" + pass); // Bug if pass contains a space?
outputResult.RemoveAll(x => x.Equals("Unable to initialize messaging context"));
string resultItem = outputResult[0];
if (resultItem.Contains("NT_STATUS_HOST_UNREACHABLE"))
{
Console.WriteLine("Error - Unable to contact \\\\" + ip);
return;
}
else if (resultItem.Contains("NT_STATUS_LOGON_FAILURE"))
{
Console.WriteLine(user + ":" + pass + " - Failed");
}
else if (resultItem.Contains("NT_STATUS_UNSUCCESSFUL"))
{
Console.WriteLine("Fatal Error: " + resultItem);
return;
}
else
{
Console.WriteLine(user + ":" + pass + " - Success!");
return;
}
}
}
// smbclient -L \\\\10.10.10.172 -USABatchJobs%SABatchJobs
}
// https://github.com/nixawk/labs/blob/master/MS17_010/smb_exploit.py
}
}