-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathloader.nim
More file actions
29 lines (22 loc) · 741 Bytes
/
loader.nim
File metadata and controls
29 lines (22 loc) · 741 Bytes
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
import posix
when defined(macosx) or defined(bsd):
const MAP_ANONYMOUS = 0x1000
elif defined(solaris):
const MAP_ANONYMOUS = 0x100
else:
var
MAP_ANONYMOUS {.importc: "MAP_ANONYMOUS", header: "<sys/mman.h>".}: cint
proc test(a, b: cint): cint =
# mov EAX, [ESP+4]
# add EAX, [ESP+8]
var code = [0x8B'u8, 0x44, 0x24, 0x4, 0x3, 0x44, 0x24, 0x8, 0xC3]
# create executable buffer
var buf = mmap(nil, sizeof(code), PROT_READ or PROT_WRITE or PROT_EXEC,
MAP_PRIVATE or MAP_ANONYMOUS, -1, 0)
# copy code to buffer
copyMem(addr buf, addr code[0], sizeof(code))
# run code
{.emit: "`result` = ((int (*) (int, int))&`buf`)(`a`,`b`);".}
# free buffer
discard munmap(buf, sizeof(code))
echo test(7, 12)