-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathshellcodeTester.cs
More file actions
205 lines (177 loc) · 7.71 KB
/
shellcodeTester.cs
File metadata and controls
205 lines (177 loc) · 7.71 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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
namespace shellcodeTester
{
public class shellcodeTester
{
#region dll imports
[DllImport("kernel32.dll")]
static extern bool CreateProcess(string lpApplicationName,
string lpCommandLine, ref SECURITY_ATTRIBUTES lpProcessAttributes,
ref SECURITY_ATTRIBUTES lpThreadAttributes, bool bInheritHandles,
uint dwCreationFlags, IntPtr lpEnvironment, string lpCurrentDirectory,
[In] ref STARTUPINFO lpStartupInfo,
out PROCESS_INFORMATION lpProcessInformation);
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
struct STARTUPINFO
{
public Int32 cb;
public string lpReserved;
public string lpDesktop;
public string lpTitle;
public Int32 dwX;
public Int32 dwY;
public Int32 dwXSize;
public Int32 dwYSize;
public Int32 dwXCountChars;
public Int32 dwYCountChars;
public Int32 dwFillAttribute;
public Int32 dwFlags;
public Int16 wShowWindow;
public Int16 cbReserved2;
public IntPtr lpReserved2;
public IntPtr hStdInput;
public IntPtr hStdOutput;
public IntPtr hStdError;
}
[StructLayout(LayoutKind.Sequential)]
internal struct PROCESS_INFORMATION
{
public IntPtr hProcess;
public IntPtr hThread;
public int dwProcessId;
public int dwThreadId;
}
[StructLayout(LayoutKind.Sequential)]
public struct SECURITY_ATTRIBUTES
{
public int nLength;
public unsafe byte* lpSecurityDescriptor;
public int bInheritHandle;
}
[DllImport("kernel32.dll")]
public static extern IntPtr VirtualAllocEx(IntPtr hProcess, IntPtr lpAddress, uint dwSize, AllocationType flAllocationType, MemoryProtection flProtect);
[Flags]
public enum AllocationType
{
COMMIT = 0x00001000
}
[Flags]
public enum MemoryProtection
{
EXECUTE_READWRITE = 0x0040,
}
[DllImport("kernel32.dll")]
public static extern void WriteProcessMemory(IntPtr hProcess, IntPtr lpBaseAddress, byte[] lpBuffer, uint nSize, out UIntPtr lpNumberOfBytesWritten);
[DllImport("kernel32.dll")]
public static extern IntPtr CreateRemoteThread(IntPtr hProcess, IntPtr lpThreadAttributes, uint dwStackSize, IntPtr lpStartAddress, IntPtr lpParameter, uint dwCreationFlags, out uint lpThreadId);
[DllImport("kernel32.dll")]
static extern bool CloseHandle(IntPtr hObject);
[DllImport("kernel32.dll")]
static extern bool Wow64DisableWow64FsRedirection(out IntPtr oldValue);
[DllImport("kernel32.dll")]
static extern bool Wow64RevertWow64FsRedirection(IntPtr oldValue);
[System.Runtime.InteropServices.DllImport("kernel32.dll", SetLastError = true)]
public static extern IntPtr VirtualAlloc(IntPtr lpAddress, UIntPtr dwSize, AllocationType flAllocationType, MemoryProtection flProtect);
[System.Runtime.InteropServices.DllImport("kernel32")]
public static extern bool VirtualFree(IntPtr lpAddress, UInt32 dwSize, UInt32 dwFreeType);
#endregion
#region fire shellcode
public static void fireShellcode(uint architecture, byte[] shellcode, bool isCalcTarget)
{
if (isCalcTarget)
{
fireAgainstCalc(architecture, shellcode);
}
else
{
IntPtr virtualMemory = VirtualAlloc(shellcode);
callATrueIntPtr(virtualMemory);
VirtualFree(virtualMemory, 0, 0x8000);
}
}
public static void fireAgainstCalc(uint architecture, byte[] shellcode)
{
IntPtr oldValue = IntPtr.Zero;
bool disabledWOW = false;
string file = string.Empty;
if (architecture == 32)
{
file = "C:\\Windows\\SysWOW64\\calc.exe";
}
else if (architecture == 64)
{
disabledWOW = Wow64DisableWow64FsRedirection(out oldValue);
file = "C:\\Windows\\System32\\calc.exe";
}
PROCESS_INFORMATION pInfo = new PROCESS_INFORMATION();
STARTUPINFO sInfo = new STARTUPINFO();
SECURITY_ATTRIBUTES pSec = new SECURITY_ATTRIBUTES();
SECURITY_ATTRIBUTES tSec = new SECURITY_ATTRIBUTES();
pSec.nLength = Marshal.SizeOf(pSec);
tSec.nLength = Marshal.SizeOf(tSec);
if (!CreateProcess(file, null, ref pSec, ref tSec, false, 0x0020, IntPtr.Zero, null, ref sInfo, out pInfo))
{
System.Windows.Forms.MessageBox.Show("Couldn't create process");
return;
}
if (disabledWOW == true)
{
Wow64RevertWow64FsRedirection(oldValue);
}
System.Threading.Thread.Sleep(2000);
IntPtr hHandle = pInfo.hProcess;
IntPtr hAlloc = VirtualAllocEx(hHandle, IntPtr.Zero, (uint)shellcode.Length, AllocationType.COMMIT, MemoryProtection.EXECUTE_READWRITE);
UIntPtr bytesWritten = UIntPtr.Zero;
WriteProcessMemory(hHandle, hAlloc, shellcode, (uint)shellcode.Length, out bytesWritten);
uint iThreadId = 0;
IntPtr hThread = CreateRemoteThread(hHandle, IntPtr.Zero, 0, hAlloc, IntPtr.Zero, 0, out iThreadId);
System.Threading.Thread.Sleep(1000);
CloseHandle(hThread);
CloseHandle(hHandle);
}
#endregion fire shellcode
#region target against self
public delegate void launchShellCodeIntPtr(IntPtr target);
//Virtual alloc and marshal copy shellcode to an IntPtr.
public static IntPtr VirtualAlloc(byte[] shellcodeIN)
{
IntPtr virtualMemory = VirtualAlloc(IntPtr.Zero, new UIntPtr((uint)shellcodeIN.Length), AllocationType.COMMIT, MemoryProtection.EXECUTE_READWRITE);
System.Runtime.InteropServices.Marshal.Copy(shellcodeIN, 0, virtualMemory, shellcodeIN.Length);
return virtualMemory;
}
public static void callATrueIntPtr(IntPtr intPtrToFire)
{
IntPtr p = VirtualAlloc(call_a_fun_ptr);
launchShellCodeIntPtr fireShellcode = (launchShellCodeIntPtr)System.Runtime.InteropServices.Marshal.GetDelegateForFunctionPointer(p, typeof(launchShellCodeIntPtr));
try
{
fireShellcode(intPtrToFire);
}
catch (System.Exception ex)
{
System.Windows.Forms.MessageBox.Show("Failed in callATrueIntPtr because of " + ex.Message);
}
VirtualFree(p, 0, 0x8000);
}
/// <summary>
/// Takes an IntPtr as an argument and will call it.
/// </summary>
static public byte[] call_a_fun_ptr = new byte[]
{
0x60, //pushad
0x55,//push ebp
0x89, 0xe5, //mov ebp, esp
0x8b, 0x44, 0x24, 0x28, //mov eax, [esp + 28]
0xff, 0xd0, //call eax
0x89, 0xec,//mov esp, ebp
0x5d, //pop ebp
0x61, //popad
0xc3//ret
};
#endregion target against self
}
}