-
Notifications
You must be signed in to change notification settings - Fork 121
/
Copy pathGetSyscallStub.nim
84 lines (72 loc) · 3.63 KB
/
GetSyscallStub.nim
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
#[
S3cur3Th1sSh1t, Twitter: @Shitsecure
License: BSD 3-Clause
]#
import winim
import strutils
import ptr_math
type
PS_ATTR_UNION* {.pure, union.} = object
Value*: ULONG
ValuePtr*: PVOID
PS_ATTRIBUTE* {.pure.} = object
Attribute*: ULONG
Size*: SIZE_T
u1*: PS_ATTR_UNION
ReturnLength*: PSIZE_T
PPS_ATTRIBUTE* = ptr PS_ATTRIBUTE
PS_ATTRIBUTE_LIST* {.pure.} = object
TotalLength*: SIZE_T
Attributes*: array[2, PS_ATTRIBUTE]
PPS_ATTRIBUTE_LIST* = ptr PS_ATTRIBUTE_LIST
KNORMAL_ROUTINE* {.pure.} = object
NormalContext*: PVOID
SystemArgument1*: PVOID
SystemArgument2*: PVOID
PKNORMAL_ROUTINE* = ptr KNORMAL_ROUTINE
var SYSCALL_STUB_SIZE: int = 23
proc RVAtoRawOffset(RVA: DWORD_PTR, section: PIMAGE_SECTION_HEADER): PVOID =
return cast[PVOID](RVA - section.VirtualAddress + section.PointerToRawData)
proc toString(bytes: openarray[byte]): string =
result = newString(bytes.len)
copyMem(result[0].addr, bytes[0].unsafeAddr, bytes.len)
proc GetSyscallStub*(functionName: LPCSTR, syscallStub: LPVOID): BOOL =
var
file: HANDLE
fileSize: DWORD
bytesRead: DWORD
fileData: LPVOID
ntdllString: LPCSTR = "C:\\windows\\system32\\ntdll.dll"
nullHandle: HANDLE
file = CreateFileA(ntdllString, cast[DWORD](GENERIC_READ), cast[DWORD](FILE_SHARE_READ), cast[LPSECURITY_ATTRIBUTES](NULL), cast[DWORD](OPEN_EXISTING), cast[DWORD](FILE_ATTRIBUTE_NORMAL), nullHandle)
fileSize = GetFileSize(file, nil)
fileData = HeapAlloc(GetProcessHeap(), 0, fileSize)
ReadFile(file, fileData, fileSize, addr bytesRead, nil)
var
dosHeader: PIMAGE_DOS_HEADER = cast[PIMAGE_DOS_HEADER](fileData)
imageNTHeaders: PIMAGE_NT_HEADERS = cast[PIMAGE_NT_HEADERS](cast[DWORD_PTR](fileData) + dosHeader.e_lfanew)
exportDirRVA: DWORD = imageNTHeaders.OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_EXPORT].VirtualAddress
section: PIMAGE_SECTION_HEADER = IMAGE_FIRST_SECTION(imageNTHeaders)
textSection: PIMAGE_SECTION_HEADER = section
rdataSection: PIMAGE_SECTION_HEADER = section
let low: uint16 = 0
for Section in low ..< imageNTHeaders.FileHeader.NumberOfSections:
var ntdllSectionHeader = cast[PIMAGE_SECTION_HEADER](cast[DWORD_PTR](IMAGE_FIRST_SECTION(imageNTHeaders)) + cast[DWORD_PTR](IMAGE_SIZEOF_SECTION_HEADER * Section))
if ".rdata" in toString(ntdllSectionHeader.Name):
rdataSection = ntdllSectionHeader
var exportDirectory: PIMAGE_EXPORT_DIRECTORY = cast[PIMAGE_EXPORT_DIRECTORY](RVAtoRawOffset(cast[DWORD_PTR](fileData) + exportDirRVA, rdataSection))
var addressOfNames: PDWORD = cast[PDWORD](RVAtoRawOffset(cast[DWORD_PTR](fileData) + cast[DWORD_PTR](exportDirectory.AddressOfNames), rdataSection))
var addressOfFunctions: PDWORD = cast[PDWORD](RVAtoRawOffset(cast[DWORD_PTR](fileData) + cast[DWORD_PTR](exportDirectory.AddressOfFunctions), rdataSection))
var stubFound: BOOL = 0
var oldProtection: DWORD = 0
let low2: int = 0
for low2 in 0 ..< exportDirectory.NumberOfNames:
var functionNameVA: DWORD_PTR = cast[DWORD_PTR](RVAtoRawOffset(cast[DWORD_PTR](fileData) + addressOfNames[low2], rdataSection))
var functionVA: DWORD_PTR = cast[DWORD_PTR](RVAtoRawOffset(cast[DWORD_PTR](fileData) + addressOfFunctions[low2 + 1], textSection))
var functionNameResolved: LPCSTR = cast[LPCSTR](functionNameVA)
var compare: int = lstrcmpA(functionNameResolved,functionName)
if (compare == 0):
copyMem(syscallStub, cast[LPVOID](functionVA), SYSCALL_STUB_SIZE)
stubFound = 1
return stubFound
return stubFound