-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscratch.py
106 lines (74 loc) · 2.59 KB
/
scratch.py
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
from cffi import FFI
ffi = FFI()
ffi.cdef(r"""
typedef struct _IMAGE_EXPORT_DIRECTORY {
DWORD Characteristics;
DWORD TimeDateStamp;
WORD MajorVersion;
WORD MinorVersion;
DWORD Name;
DWORD Base;
DWORD NumberOfFunctions;
DWORD NumberOfNames;
DWORD AddressOfFunctions;
DWORD AddressOfNames;
DWORD AddressOfNameOrdinals;
} IMAGE_EXPORT_DIRECTORY,*PIMAGE_EXPORT_DIRECTORY;
DWORD pRVABase;
IMAGE_EXPORT_DIRECTORY* pExportDirectory;
void* PointerFromAddress(DWORD Address);
DWORD AddressFromPointer(void* Pointer);
""")
ffi.set_source("scratch", r"""
#include <assert.h>
#include <WINDOWS.h>
extern DWORD pRVABase = 0;
IMAGE_EXPORT_DIRECTORY* pExportDirectory = 0;
extern CFFI_DLLEXPORT __declspec(naked) _0() { ExitProcess(0x00FF0000 + 0); }
extern CFFI_DLLEXPORT __declspec(naked) _1() { ExitProcess(0x00FF0000 + 1); }
extern CFFI_DLLEXPORT __declspec(naked) _3() { ExitProcess(0x00FF0000 + 2); }
extern CFFI_DLLEXPORT __declspec(naked) _02() { ExitProcess(0x00FF0000 + 02); }
extern void* PointerFromAddress(DWORD Address)
{
return (void*)(Address + pRVABase);
}
extern DWORD AddressFromPointer(void* Pointer)
{
return (DWORD)(Pointer) - pRVABase;
}
// initialize this from the
extern void** exports= 0;
BOOL WINAPI DllMain(HINSTANCE hInstDll, DWORD fdwReason, LPVOID lpvReason)
{
if (pRVABase)
return TRUE;
// figure out the location of the export able, this is easyier than
// using pythongf
pRVABase = (DWORD) hInstDll;
IMAGE_DOS_HEADER* pDosHeader =
(IMAGE_DOS_HEADER*) hInstDll;
assert(pDosHeader->e_magic == 0x4550);
IMAGE_NT_HEADERS32* pNtHeaders32 =
(IMAGE_NT_HEADERS32*) (pDosHeader->e_lfanew + pRVABase);
assert(pNtHeaders32->Signature == 0x4550);
assert(pNtHeaders32->OptionalHeader.DataDirectory[0].VirtualAddress != 0);
assert(pNtHeaders32->OptionalHeader.DataDirectory[0].Size != 0);
pExportDirectory =
(IMAGE_EXPORT_DIRECTORY*)
(pNtHeaders32->OptionalHeader.DataDirectory[0].VirtualAddress + pRVABase);
_cffi_initialize_python();
return TRUE;
}
""")
ffi.embedding_init_code(open("scratch-init.py", "r").read())
ffi.compile(target="scratch.dll", verbose=True)
def test_it():
ffi = FFI()
ffi.cdef("""
int _0(int, int);
""")
api = ffi.dlopen('scratch.dll')
# why no api call here?
print(api._0(1, 10))
# assert 3 == api._0(1, 2)
test_it()