-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhelpers.h
94 lines (87 loc) · 2.49 KB
/
helpers.h
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
#include <windows.h>
#include <stdio.h>
#define push(x) pushm(x, thd)
unsigned int pshc; //push edx; call eax
unsigned int jmps; //jmp $
unsigned int ret; //ret
//gadget finder
unsigned int findr(const unsigned char* pattern, int sz, const char* name){
void* base = GetModuleHandleA(name);
unsigned char* ptr = (unsigned char*)base;
ptr+=((PIMAGE_SECTION_HEADER)(base + ((PIMAGE_DOS_HEADER)base)->e_lfanew + 248))->VirtualAddress;
unsigned int virtsize = ((PIMAGE_SECTION_HEADER)(base + ((PIMAGE_DOS_HEADER)base)->e_lfanew + 248))->SizeOfRawData;
unsigned int c=0;
while(memcmp(pattern, ptr+c, sz)!=0){
c++;
if(c>=virtsize) return 0;
}
return (unsigned int)(ptr+c);
}
//wait for user time to increase, signify kernel exit, thread can be manipulated
void waitunblock(HANDLE thd){
FILETIME a, b, c, d;
GetThreadTimes(thd, &a, &b, &c, &d);
DWORD pt = d.dwLowDateTime;
while(1){
Sleep(1);
GetThreadTimes(thd, &a, &b, &c, &d);
if(d.dwLowDateTime - pt > 9) break; //when user time is >90% of total time, we're probably done
pt = d.dwLowDateTime;
}
return;
}
//push val to stack, returns address of pushed data
unsigned int pushm(unsigned int data, HANDLE thd){
CONTEXT ctx;
ctx.ContextFlags = CONTEXT_FULL;
GetThreadContext(thd, &ctx);
ctx.Esp += 4;
ctx.Eip = pshc;
ctx.Edx = data;
ctx.Eax = jmps;
SetThreadContext(thd, &ctx);
ResumeThread(thd);
Sleep(1);
SuspendThread(thd);
return ctx.Esp-4;
}
//push val to stack, but returns return val of previous fn called (in eax)
unsigned int getretpush(unsigned int data, HANDLE thd){
CONTEXT ctx2;
SuspendThread(thd);
ctx2.ContextFlags = CONTEXT_FULL;
GetThreadContext(thd, &ctx2);
ctx2.Eip = pshc;
unsigned int addr = ctx2.Eax;
ctx2.Edx = data;
ctx2.Eax = jmps;
SetThreadContext(thd, &ctx2);
ResumeThread(thd);
Sleep(1);
SuspendThread(thd);
return addr;
}
//push junk to stack
void opening(HANDLE thd){
CONTEXT ctx;
SuspendThread(thd);
ctx.ContextFlags = CONTEXT_FULL;
GetThreadContext(thd, &ctx);
ctx.Edx = 0;
ctx.Eip = pshc;
ctx.Eax = jmps;
SetThreadContext(thd, &ctx);
ResumeThread(thd);
Sleep(1);
SuspendThread(thd);
}
//execute the prepared rop sled
void slay(HANDLE thd){
CONTEXT ctx;
ctx.ContextFlags = CONTEXT_FULL;
GetThreadContext(thd, &ctx);
ctx.Esp += 4;
ctx.Eip = ret;
SetThreadContext(thd, &ctx);
ResumeThread(thd);
}