-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi.c
More file actions
142 lines (123 loc) · 3.48 KB
/
api.c
File metadata and controls
142 lines (123 loc) · 3.48 KB
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
135
136
137
138
139
140
141
#include "api.h"
#include "interpreter_states.h"
#ifdef _WIN32 //windows only apis
#include <Windows.h>
#endif
#include <stdio.h>
#include "libc.h"
#ifdef _WIN32
extern int dprintf(int stream, const char *format, ...);
#endif
void api_put() {
int mode = (int)state->STACK[state->STACK_IDX--]; // 1 for char, 2 for num
if (mode != 1 && mode != 2) return;
int f = fstream;
#ifdef _WIN32
if (f == 2) //stderr (could use _fileno(stderr) but it uses the stdlib)
f = 1; //stdout
#else
if (f == fileno(stderr))
f = fileno(stdout);
#endif
if (mode == 1) {
char c = (char)state->STACK[state->STACK_IDX--];
if (c == '\0') c = ' ';
dprintf(f, "%c", c); //using printf and not write because of the buffer
}
else {
#ifdef _WIN32
dprintf(f, "%ld", state->STACK[state->STACK_IDX--]);
#else
dprintf(f, "%lld", state->STACK[state->STACK_IDX--]);
#endif
}
}
void api_print() {
char *address = (char *)state->STACK[state->STACK_IDX--];
int f = fstream;
#ifdef _WIN32
if (f == 2) //stderr (could use _fileno(stderr) but it uses the stdlib)
f = 1; //stdout
#else
if (f == fileno(stderr))
f = fileno(stdout);
#endif
dprintf(f, "%s", address);
}
void api_free() {
void *addr = (void *)state->STACK[state->STACK_IDX--];
free_(addr);
}
void api_malloc() {
long size = state->STACK[state->STACK_IDX--];
#ifdef _WIN32
state->registers->eax = 0;
return;
#else
state->registers->eax = (long long)malloc(size);
#endif
}
void api_callrawaddr() {
long long address = state->STACK[state->STACK_IDX--];
((void (*)())address)();
}
// generated APIs here
#ifdef _WIN32
typedef LPVOID(WINAPI *fVirtualAlloc)(LPVOID, DWORD, DWORD, DWORD);
#endif
void api_VirtualAlloc(void) {
#ifdef _WIN32
fVirtualAlloc pVirtualAlloc = GetApi(L"kernel32.dll", "[nwyzfqFqqth");
if (pVirtualAlloc == NULL) {
state->STACK_IDX -= 4;
state->registers->eax = 1;
return;
}
long long arg0 = state->STACK[state->STACK_IDX--];
long long arg1 = state->STACK[state->STACK_IDX--];
long long arg2 = state->STACK[state->STACK_IDX--];
long long arg3 = state->STACK[state->STACK_IDX--];
state->registers->eax = (long long)pVirtualAlloc((LPVOID)arg0, (DWORD)arg1, (DWORD)arg2, (DWORD)arg3);
#else
state->STACK_IDX -= 4;
state->registers->eax = 1;
#endif
}
#ifdef _WIN32
typedef BOOL(WINAPI *fVirtualFree)(LPVOID, DWORD, DWORD);
#endif
void api_VirtualFree(void) {
#ifdef _WIN32
fVirtualFree pVirtualFree = GetApi(L"kernel32.dll", "[nwyzfqKwjj");
if (pVirtualFree == NULL) {
state->STACK_IDX -= 3;
state->registers->eax = 1;
return;
}
long long arg0 = state->STACK[state->STACK_IDX--];
long long arg1 = state->STACK[state->STACK_IDX--];
long long arg2 = state->STACK[state->STACK_IDX--];
state->registers->eax = (long long)pVirtualFree((LPVOID)arg0, (DWORD)arg1, (DWORD)arg2);
#else
state->STACK_IDX -= 3;
state->registers->eax = 1;
#endif
}
#ifdef _WIN32
typedef SHORT(WINAPI *fGetAsyncKeyState)(int);
#endif
void api_GetAsyncKeyState(void) {
#ifdef _WIN32
fGetAsyncKeyState pGetAsyncKeyState = GetApi(L"user32.dll", "LjyFx~shPj~Xyfyj");
if (pGetAsyncKeyState == NULL) {
state->STACK_IDX -= 1;
state->registers->eax = 1;
return;
}
long long arg0 = state->STACK[state->STACK_IDX--];
state->registers->eax = (long long)pGetAsyncKeyState((int)arg0);
#else
state->STACK_IDX -= 1;
state->registers->eax = 1;
#endif
}