Skip to content

Commit e560311

Browse files
committed
add: dynamic Windows APIs, blank IAT fix: misc & linux build
1 parent 1c451dc commit e560311

21 files changed

+894
-251
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ x64/
44
Debug/
55
Release/
66
build/
7+
bin
8+
obj
79
*.obj
810
*.a
911
*.o

Makefile

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,36 @@
1-
SRC = src/pasm.c \
2-
src/file_utils.c \
3-
src/interpreter_states.c \
4-
src/instructions.c \
5-
src/api.c \
6-
src/debug.c
1+
SRC = src/pasm.c \
2+
src/file_utils.c \
3+
src/interpreter_states.c \
4+
src/instructions.c \
5+
src/api.c \
6+
src/debug.c \
7+
src/libc.c
78
OBJ = $(SRC:.c=.o)
89
NAME = pasm
910
CC = gcc
1011
CFLAGS = -Wall -Wextra -Wpedantic -Iinclude -s -Os -fno-ident -fno-asynchronous-unwind-tables
1112
CLIBS = -lm
1213

13-
all: $(NAME)
14+
all: $(NAME)
1415

1516
lib: $(OBJ)
16-
@-mkdir build
17+
@mkdir -p build
1718
ar rc build/lib$(NAME).a $(OBJ)
1819

19-
$(NAME): fclean
2020
$(NAME): lib
21-
$(NAME): CLIBS += build/lib$(NAME).a
22-
$(NAME):
23-
$(CC) tests/interpreter.c $(CFLAGS) $(CLIBS) -o build/$(NAME)
21+
$(CC) $(CFLAGS) -o build/$(NAME) tests/interpreter.c build/lib$(NAME).a $(CLIBS)
2422

2523
interpreter: $(NAME)
2624

2725
clean:
28-
@-rm -f $(OBJ)
29-
@-cd tests && $(MAKE) clean
26+
@rm -f $(OBJ)
27+
@cd tests && $(MAKE) clean
3028

3129
fclean: clean
32-
@-rm -rf build/
33-
@-cd tests && $(MAKE) fclean
30+
@rm -rf build/
31+
@cd tests && $(MAKE) fclean
3432

3533
re: fclean
3634
re: $(NAME)
3735

38-
.PHONY : all $(NAME) clean fclean re interpreter lib
36+
.PHONY: all $(NAME) clean fclean re interpreter lib

examples/array.pasm

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ jmp loop2
2121

2222
main:
2323
mov a1, msg ; msg is a char *
24-
2524
loop:
2625
cmp *a1, 0
2726
jne 1

msvc/interpreter.vcxproj

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,19 +72,19 @@
7272
<PropertyGroup Label="UserMacros" />
7373
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
7474
<LinkIncremental>true</LinkIncremental>
75-
<LibraryPath>$(SolutionDir)$(Configuration);$(LibraryPath)</LibraryPath>
75+
<LibraryPath>..\bin;$(LibraryPath)</LibraryPath>
7676
</PropertyGroup>
7777
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
7878
<LinkIncremental>false</LinkIncremental>
79-
<LibraryPath>$(SolutionDir)$(Configuration);$(LibraryPath)</LibraryPath>
79+
<LibraryPath>..\bin;$(LibraryPath)</LibraryPath>
8080
</PropertyGroup>
8181
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
8282
<LinkIncremental>true</LinkIncremental>
83-
<LibraryPath>$(SolutionDir)$(Platform)\$(Configuration);$(LibraryPath)</LibraryPath>
83+
<LibraryPath>..\bin;$(LibraryPath)</LibraryPath>
8484
</PropertyGroup>
8585
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
8686
<LinkIncremental>false</LinkIncremental>
87-
<LibraryPath>$(SolutionDir)$(Platform)\$(Configuration);$(LibraryPath)</LibraryPath>
87+
<LibraryPath>..\bin;$(LibraryPath)</LibraryPath>
8888
</PropertyGroup>
8989
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
9090
<ClCompile>

msvc/interpreter.vcxproj.user

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<Project ToolsVersion="Current" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
33
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
4-
<LocalDebuggerCommandArguments>../examples/shellcode.pasm</LocalDebuggerCommandArguments>
4+
<LocalDebuggerCommandArguments>../examples/array.pasm</LocalDebuggerCommandArguments>
55
<DebuggerFlavor>WindowsLocalDebugger</DebuggerFlavor>
66
</PropertyGroup>
77
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">

msvc/pasm.vcxproj

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,15 +72,23 @@
7272
<PropertyGroup Label="UserMacros" />
7373
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
7474
<LinkIncremental>true</LinkIncremental>
75+
<OutDir>..\bin\</OutDir>
76+
<IntDir>..\obj\</IntDir>
7577
</PropertyGroup>
7678
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
7779
<LinkIncremental>false</LinkIncremental>
80+
<OutDir>..\bin\</OutDir>
81+
<IntDir>..\obj\</IntDir>
7882
</PropertyGroup>
7983
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
8084
<LinkIncremental>true</LinkIncremental>
85+
<OutDir>..\bin\</OutDir>
86+
<IntDir>..\obj\</IntDir>
8187
</PropertyGroup>
8288
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
8389
<LinkIncremental>false</LinkIncremental>
90+
<OutDir>..\bin\</OutDir>
91+
<IntDir>..\obj\</IntDir>
8492
</PropertyGroup>
8593
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
8694
<ClCompile>
@@ -89,6 +97,9 @@
8997
<PreprocessorDefinitions>WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions);_CRT_SECURE_NO_WARNINGS</PreprocessorDefinitions>
9098
<ConformanceMode>true</ConformanceMode>
9199
<LanguageStandard_C>stdc17</LanguageStandard_C>
100+
<BasicRuntimeChecks>Default</BasicRuntimeChecks>
101+
<BufferSecurityCheck>false</BufferSecurityCheck>
102+
<ExceptionHandling>false</ExceptionHandling>
92103
</ClCompile>
93104
<Link>
94105
<SubSystem>Console</SubSystem>
@@ -98,6 +109,11 @@
98109
<ExportNamedFunctions>pasm_run_script</ExportNamedFunctions>
99110
</Lib>
100111
</ItemDefinitionGroup>
112+
<ItemDefinitionGroup Condition="'$(LaikaBuild)' != ''">
113+
<ClCompile>
114+
<AdditionalOptions>/DLAIKA %(AdditionalOptions)</AdditionalOptions>
115+
</ClCompile>
116+
</ItemDefinitionGroup>
101117
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
102118
<ClCompile>
103119
<WarningLevel>Level3</WarningLevel>
@@ -111,6 +127,8 @@
111127
<FavorSizeOrSpeed>Size</FavorSizeOrSpeed>
112128
<DebugInformationFormat>None</DebugInformationFormat>
113129
<RuntimeLibrary>MultiThreaded</RuntimeLibrary>
130+
<BufferSecurityCheck>false</BufferSecurityCheck>
131+
<ExceptionHandling>false</ExceptionHandling>
114132
</ClCompile>
115133
<Link>
116134
<SubSystem>Console</SubSystem>
@@ -129,6 +147,9 @@
129147
<PreprocessorDefinitions>_DEBUG;_CONSOLE;%(PreprocessorDefinitions);_CRT_SECURE_NO_WARNINGS</PreprocessorDefinitions>
130148
<ConformanceMode>true</ConformanceMode>
131149
<LanguageStandard_C>stdc17</LanguageStandard_C>
150+
<BasicRuntimeChecks>Default</BasicRuntimeChecks>
151+
<BufferSecurityCheck>false</BufferSecurityCheck>
152+
<ExceptionHandling>false</ExceptionHandling>
132153
</ClCompile>
133154
<Link>
134155
<SubSystem>Console</SubSystem>
@@ -151,6 +172,8 @@
151172
<FavorSizeOrSpeed>Size</FavorSizeOrSpeed>
152173
<DebugInformationFormat>None</DebugInformationFormat>
153174
<RuntimeLibrary>MultiThreaded</RuntimeLibrary>
175+
<BufferSecurityCheck>false</BufferSecurityCheck>
176+
<ExceptionHandling>false</ExceptionHandling>
154177
</ClCompile>
155178
<Link>
156179
<SubSystem>Console</SubSystem>
@@ -168,6 +191,7 @@
168191
<ClCompile Include="..\src\file_utils.c" />
169192
<ClCompile Include="..\src\instructions.c" />
170193
<ClCompile Include="..\src\interpreter_states.c" />
194+
<ClCompile Include="..\src\libc.c" />
171195
<ClCompile Include="..\src\pasm.c" />
172196
</ItemGroup>
173197
<ItemGroup>
@@ -176,6 +200,7 @@
176200
<ClInclude Include="..\src\file_utils.h" />
177201
<ClInclude Include="..\src\instructions.h" />
178202
<ClInclude Include="..\src\interpreter_states.h" />
203+
<ClInclude Include="..\src\libc.h" />
179204
</ItemGroup>
180205
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
181206
<ImportGroup Label="ExtensionTargets">

msvc/pasm.vcxproj.filters

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@
2525
<ClCompile Include="..\src\pasm.c">
2626
<Filter>pasm</Filter>
2727
</ClCompile>
28+
<ClCompile Include="..\src\libc.c">
29+
<Filter>pasm</Filter>
30+
</ClCompile>
2831
</ItemGroup>
2932
<ItemGroup>
3033
<ClInclude Include="..\src\api.h">
@@ -42,5 +45,8 @@
4245
<ClInclude Include="..\src\interpreter_states.h">
4346
<Filter>pasm</Filter>
4447
</ClInclude>
48+
<ClInclude Include="..\src\libc.h">
49+
<Filter>pasm</Filter>
50+
</ClInclude>
4551
</ItemGroup>
4652
</Project>

msvc/socket_usage.vcxproj

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -72,19 +72,19 @@
7272
<PropertyGroup Label="UserMacros" />
7373
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
7474
<LinkIncremental>true</LinkIncremental>
75-
<LibraryPath>$(SolutionDir)$(Configuration);$(LibraryPath)</LibraryPath>
75+
<LibraryPath>..\bin;$(LibraryPath);..\bin</LibraryPath>
7676
</PropertyGroup>
7777
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
7878
<LinkIncremental>false</LinkIncremental>
79-
<LibraryPath>$(SolutionDir)$(Configuration);$(LibraryPath)</LibraryPath>
79+
<LibraryPath>..\bin;$(LibraryPath);..\bin</LibraryPath>
8080
</PropertyGroup>
8181
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
8282
<LinkIncremental>true</LinkIncremental>
83-
<LibraryPath>$(SolutionDir)$(Platform)\$(Configuration);$(LibraryPath)</LibraryPath>
83+
<LibraryPath>$(SolutionDir)$(Platform)\$(Configuration);$(LibraryPath);..\bin</LibraryPath>
8484
</PropertyGroup>
8585
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
8686
<LinkIncremental>false</LinkIncremental>
87-
<LibraryPath>$(SolutionDir)$(Platform)\$(Configuration);$(LibraryPath)</LibraryPath>
87+
<LibraryPath>$(SolutionDir)$(Platform)\$(Configuration);$(LibraryPath);..\bin</LibraryPath>
8888
</PropertyGroup>
8989
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
9090
<ClCompile>
@@ -95,7 +95,7 @@
9595
<LanguageStandard_C>stdc17</LanguageStandard_C>
9696
</ClCompile>
9797
<Link>
98-
<SubSystem>Windows</SubSystem>
98+
<SubSystem>Console</SubSystem>
9999
<GenerateDebugInformation>true</GenerateDebugInformation>
100100
<AdditionalDependencies>pasm.lib;%(AdditionalDependencies)</AdditionalDependencies>
101101
</Link>
@@ -111,7 +111,7 @@
111111
<LanguageStandard_C>stdc17</LanguageStandard_C>
112112
</ClCompile>
113113
<Link>
114-
<SubSystem>Windows</SubSystem>
114+
<SubSystem>Console</SubSystem>
115115
<EnableCOMDATFolding>true</EnableCOMDATFolding>
116116
<OptimizeReferences>true</OptimizeReferences>
117117
<GenerateDebugInformation>true</GenerateDebugInformation>
@@ -127,7 +127,7 @@
127127
<LanguageStandard_C>stdc17</LanguageStandard_C>
128128
</ClCompile>
129129
<Link>
130-
<SubSystem>Windows</SubSystem>
130+
<SubSystem>Console</SubSystem>
131131
<GenerateDebugInformation>true</GenerateDebugInformation>
132132
<AdditionalDependencies>pasm.lib;%(AdditionalDependencies)</AdditionalDependencies>
133133
</Link>
@@ -143,7 +143,7 @@
143143
<LanguageStandard_C>stdc17</LanguageStandard_C>
144144
</ClCompile>
145145
<Link>
146-
<SubSystem>Windows</SubSystem>
146+
<SubSystem>Console</SubSystem>
147147
<EnableCOMDATFolding>true</EnableCOMDATFolding>
148148
<OptimizeReferences>true</OptimizeReferences>
149149
<GenerateDebugInformation>true</GenerateDebugInformation>

src/api.c

Lines changed: 47 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#endif
77

88
#include <stdio.h>
9+
#include "libc.h"
910

1011
#ifdef _WIN32
1112
extern int dprintf(int stream, const char *format, ...);
@@ -17,8 +18,8 @@ void api_put() {
1718

1819
int f = fstream;
1920
#ifdef _WIN32
20-
if (f == _fileno(stderr))
21-
f = _fileno(stdout);
21+
if (f == 2) //stderr (could use _fileno(stderr) but it uses the stdlib)
22+
f = 1; //stdout
2223
#else
2324
if (f == fileno(stderr))
2425
f = fileno(stdout);
@@ -31,46 +32,68 @@ void api_put() {
3132
dprintf(f, "%c", c); //using printf and not write because of the buffer
3233
}
3334
else {
34-
dprintf(f, "%lld", state->STACK[state->STACK_IDX--]);
35+
#ifdef _WIN32
36+
dprintf(f, "%ld", state->STACK[state->STACK_IDX--]);
37+
#else
38+
dprintf(f, "%lld", state->STACK[state->STACK_IDX--]);
39+
#endif
3540
}
3641
}
3742

38-
void api_getasynckeystate() {
39-
#ifdef _WIN32
40-
state->registers->eax = GetAsyncKeyState((int)state->STACK[state->STACK_IDX--]);
41-
#else
42-
state->STACK_IDX--;
43-
state->registers->eax = 1;
44-
#endif
43+
void api_callrawaddr() {
44+
long long address = state->STACK[state->STACK_IDX--];
45+
46+
((void (*)())address)();
4547
}
4648

47-
void api_virtualalloc() {
49+
// generated APIs here
50+
4851
#ifdef _WIN32
49-
long long address = state->STACK[state->STACK_IDX--];
50-
long long size = state->STACK[state->STACK_IDX--];
51-
long long alloctype = state->STACK[state->STACK_IDX--];
52-
long long flprotect = state->STACK[state->STACK_IDX--];
53-
state->registers->eax = (long long)VirtualAlloc((LPVOID)address, (SIZE_T)size, (DWORD)alloctype, (DWORD)flprotect);
52+
typedef LPVOID(WINAPI *fVirtualAlloc)(LPVOID, DWORD, DWORD, DWORD);
53+
#endif
54+
void api_VirtualAlloc(void) {
55+
#ifdef _WIN32
56+
fVirtualAlloc pVirtualAlloc = GetApi(L"kernel32.dll", "VirtualAlloc");
57+
long long arg0 = state->STACK[state->STACK_IDX--];
58+
long long arg1 = state->STACK[state->STACK_IDX--];
59+
long long arg2 = state->STACK[state->STACK_IDX--];
60+
long long arg3 = state->STACK[state->STACK_IDX--];
61+
state->registers->eax = (long long)pVirtualAlloc((LPVOID)arg0, (DWORD)arg1, (DWORD)arg2, (DWORD)arg3);
5462
#else
5563
state->STACK_IDX -= 4;
5664
state->registers->eax = 1;
5765
#endif
5866
}
5967

60-
void api_virtualfree() {
68+
6169
#ifdef _WIN32
62-
long long address = state->STACK[state->STACK_IDX--];
63-
long long size = state->STACK[state->STACK_IDX--];
64-
long long freetype = state->STACK[state->STACK_IDX--];
65-
state->registers->eax = VirtualFree((LPVOID)address, (SIZE_T)size, (DWORD)freetype);
70+
typedef BOOL(WINAPI *fVirtualFree)(LPVOID, DWORD, DWORD);
71+
#endif
72+
void api_VirtualFree(void) {
73+
#ifdef _WIN32
74+
fVirtualFree pVirtualFree = GetApi(L"kernel32.dll", "VirtualFree");
75+
long long arg0 = state->STACK[state->STACK_IDX--];
76+
long long arg1 = state->STACK[state->STACK_IDX--];
77+
long long arg2 = state->STACK[state->STACK_IDX--];
78+
state->registers->eax = (long long)pVirtualFree((LPVOID)arg0, (DWORD)arg1, (DWORD)arg2);
6679
#else
6780
state->STACK_IDX -= 3;
6881
state->registers->eax = 1;
6982
#endif
7083
}
7184

72-
void api_callrawaddr() {
73-
long long address = state->STACK[state->STACK_IDX--];
7485

75-
((void (*)())address)();
86+
#ifdef _WIN32
87+
typedef SHORT(WINAPI *fGetAsyncKeyState)(int);
88+
#endif
89+
void api_GetAsyncKeyState(void) {
90+
#ifdef _WIN32
91+
fGetAsyncKeyState pGetAsyncKeyState = GetApi(L"user32.dll", "GetAsyncKeyState");
92+
long long arg0 = state->STACK[state->STACK_IDX--];
93+
state->registers->eax = (long long)pGetAsyncKeyState((int)arg0);
94+
#else
95+
state->STACK_IDX -= 1;
96+
state->registers->eax = 1;
97+
#endif
7698
}
99+

0 commit comments

Comments
 (0)