first commit of the code

This commit is contained in:
2023-04-27 21:44:22 +02:00
parent 156d04530f
commit a6e64150e8
14 changed files with 556 additions and 0 deletions

3
.gitignore vendored Normal file
View File

@@ -0,0 +1,3 @@
.vs
.git
x64/

2
examples/README.md Normal file
View File

@@ -0,0 +1,2 @@
# keylogger
This is a POC, i don't even know if it works as the interpreter and scripts are in very early stages

60
examples/keylogger.pasm Normal file
View File

@@ -0,0 +1,60 @@
; Simple PASM keylogger POC
; by patate
; //https://learn.microsoft.com/en-us/windows/win32/inputdev/virtual-key-codes
; -----------------------------------
; Keys 0 to 9 and A to Z
; -----------------------------------
; for (int i = 48; i < 91; i++) {
; if (i >= 58 && i <= 64) {
; continue;
; }
; if (GetAsyncKeyState(i)) {
; printf("%c", i);
; }
; }
loop:
mov a1, 47 ;set a1 to 47
numbers:
cmp a1, 90
je loop ; if ==
call check
cmp eax, 1
je inc_and_numbers
add a1, 1 ; i++
push a1 ; arg 1 (vKey)
call GetAsyncKeyState
cmp eax, 1
jne numbers ; if GetAsyncKeyState was false, jump to numbers
mov a2, a1 ; necessary ?
push "%c" ; push format
push a2 ; push char
call printf
jmp numbers
; https://stackoverflow.com/a/18670716
check:
cmp a1, 58
jb return ; if <
cmp a1, 64
ja return ; if >
mov eax, 1
ret
inc_and_numbers:
add a1, 1
call numbers
return:
ret

31
pasm.sln Normal file
View File

@@ -0,0 +1,31 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.1.32407.343
MinimumVisualStudioVersion = 10.0.40219.1
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "pasm", "src/pasm.vcxproj", "{6D8EE35D-C813-4209-A185-D7198811E00C}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|x64 = Debug|x64
Debug|x86 = Debug|x86
Release|x64 = Release|x64
Release|x86 = Release|x86
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{6D8EE35D-C813-4209-A185-D7198811E00C}.Debug|x64.ActiveCfg = Debug|x64
{6D8EE35D-C813-4209-A185-D7198811E00C}.Debug|x64.Build.0 = Debug|x64
{6D8EE35D-C813-4209-A185-D7198811E00C}.Debug|x86.ActiveCfg = Debug|Win32
{6D8EE35D-C813-4209-A185-D7198811E00C}.Debug|x86.Build.0 = Debug|Win32
{6D8EE35D-C813-4209-A185-D7198811E00C}.Release|x64.ActiveCfg = Release|x64
{6D8EE35D-C813-4209-A185-D7198811E00C}.Release|x64.Build.0 = Release|x64
{6D8EE35D-C813-4209-A185-D7198811E00C}.Release|x86.ActiveCfg = Release|Win32
{6D8EE35D-C813-4209-A185-D7198811E00C}.Release|x86.Build.0 = Release|Win32
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {AA519A40-CDCD-4CB3-9E0B-F8704E40D1F4}
EndGlobalSection
EndGlobal

74
src/get_instruction.c Normal file
View File

@@ -0,0 +1,74 @@
#include "get_instruction.h"
#include "main.h"
#include <stdio.h>
#include <string.h>
const char* instructions_char[] = {"add", "sub", "mov", "cmp", "je", "jne", "jmp", "jb", "jbn", "ja", "jna", "ret", "pop", "push", "call"};
INSTRUCTION get_instruction(char* line, int* args_start_pos, int line_number) {
char ins[20]; //20 should be enough
memset(ins, 0, sizeof(ins));
for (int i = 0; i < (int)strlen(line); i++) {
if (line[i] == ' ' || line[i] == '\n' || line[i] == '\0') {
ins[i] = '\0';
*args_start_pos = i + 1;
break;
}
if (line[i] == ':') {
ins[i] = '\0';
*args_start_pos = -1;
labels[num_labels] = ins;
labels_lines[num_labels] = line_number;
++num_labels;
return LABEL;
}
ins[i] = line[i];
}
for (int j = 0; j < sizeof(instructions_char)/sizeof(instructions_char[0]); j++) {
if (strcmp(ins, instructions_char[j]) == 0) {
return (INSTRUCTION)j;
}
}
*args_start_pos = -1;
return ERROR_INSTRUCTION;
}
arguments get_args(char* line, int args_start_pos) {
char first_arg[256];
char second_arg[256];
int write_to_first = 1; //ugly hack but whatever
int j = 0;
for (int i = args_start_pos; i < (int)strlen(line); i++) {
if (line[i] == '\n' || line[i] == '\0' || line[i] == ';') {
second_arg[j] = '\0';
break;
}
if (line[i] == ',') {
first_arg[j] = '\0';
write_to_first = 0;
j = 0;
continue;
}
if (line[i] == ' ') {
continue;
}
if (write_to_first) {
first_arg[j++] = line[i];
}
else {
second_arg[j++] = line[i];
}
}
arguments args = {first_arg, second_arg};
return args;
}

10
src/get_instruction.h Normal file
View File

@@ -0,0 +1,10 @@
#pragma once
#include "instruction_set.h"
typedef struct arguments {
char* arg1;
char* arg2;
} arguments;
INSTRUCTION get_instruction(char* line, int* args_start_pos, int line_number);
arguments get_args(char* line, int args_start_pos);

21
src/instruction_set.h Normal file
View File

@@ -0,0 +1,21 @@
#pragma once
typedef enum INSTRUCTIONS {
ERROR_INSTRUCTION = -2,
LABEL = -1,
ADD,
SUB,
MOV,
CMP,
JE,
JNE,
JMP,
JB,
JNB,
JA,
JNA,
RET,
POP,
PUSH,
CALL
} INSTRUCTION;

22
src/instructions.c Normal file
View File

@@ -0,0 +1,22 @@
#include "instructions.h"
#include "main.h"
#include <stdio.h>
#include <string.h>
bool check_args(arguments args, int expected_num) {
if (strcmp(args.arg1, "a1") == 0) {
printf("aa\n");
}
return true;
}
void add(arguments args) {
if (!check_args(args, 2)) {
return;
}
}
void mov(arguments args) {
}

34
src/instructions.h Normal file
View File

@@ -0,0 +1,34 @@
#pragma once
#include "get_instruction.h"
#include <stdbool.h>
typedef enum cmp_return_codes {
EQUAL,
BELOW,
ABOVE,
} cmp_return_codes;
typedef enum check_args_codes {
WRONG_NUMBER,
NOT_VALID,
OK
} check_args_codes ;
static check_args_codes last_check_args_code = OK;
bool check_args(arguments args, int expected_num);
void add(arguments args);
void sub(arguments args);
void mov(arguments args);
cmp_return_codes cmp(arguments args);
bool je(arguments args);
bool jne(arguments args);
bool jb(arguments args);
bool jnb(arguments args);
bool ja(arguments args);
bool jna(arguments args);
void jmp(arguments args);
void ret(arguments args);
void pop(arguments args);
void push(arguments args);
void call(arguments args);

86
src/main.c Normal file
View File

@@ -0,0 +1,86 @@
#include <stdio.h>
#include <string.h>
#include "main.h"
#include "get_instruction.h"
#include "instructions.h"
void show_help() {
printf("usage : pasm.exe [filename]");
}
int main(int argc, char** argv) {
if (argc != 2) {
printf("Bad arguments.\n");
show_help();
return 1;
}
if (strcmp(argv[1], "help") == 0) {
show_help();
return 1;
}
FILE* fptr;
fopen_s(&fptr, argv[1], "r");
if (fptr == NULL) {
printf("File %s does not exist.", argv[1]);
return 1;
}
memset(&stack, 0, sizeof(stack)); //init stack
char line[256];
int line_number = 1;
while (fgets(line, sizeof(line), fptr)) {
if (line[0] == ';' || line[0] == '\n') {
++line_number;
continue;
}
int args_pos;
INSTRUCTION ins = get_instruction(line, &args_pos, line_number);
arguments args;
if (args_pos != -1) {
args = get_args(line, args_pos);
}
switch (ins) {
case SUB :
break;
case MOV :
//mov(args);
break;
case ADD :
add(args);
break;
case ERROR_INSTRUCTION:
printf("%s ^\n |\ninvalid operand on line %d", line, line_number);
fclose(fptr);
return 1;
}
if (last_check_args_code != OK) {
printf("%s", line);
printf("%*c ^\n", args_pos, ' ');
printf("%*c |\n", args_pos, ' ');
switch (last_check_args_code) {
case WRONG_NUMBER :
printf("%*c wrong number of arguments on line %d", args_pos, ' ', line_number);
case NOT_VALID :
printf("%*c invalid number/register on line %d", args_pos, ' ', line_number);
}
fclose(fptr);
return 1;
}
++line_number;
}
fclose(fptr);
return 0;
}

7
src/main.h Normal file
View File

@@ -0,0 +1,7 @@
#pragma once
static int stack[9];
static int a1, a2, a3, a4, a5, a6, a7, a8, a9, eax; //registers
static char* labels[256]; //max 256 labels
static int labels_lines[256]; //line numbers for the labels
static int num_labels = 0; //number of labels already in use

155
src/pasm.vcxproj Normal file
View File

@@ -0,0 +1,155 @@
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup Label="ProjectConfigurations">
<ProjectConfiguration Include="Debug|Win32">
<Configuration>Debug</Configuration>
<Platform>Win32</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Release|Win32">
<Configuration>Release</Configuration>
<Platform>Win32</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Debug|x64">
<Configuration>Debug</Configuration>
<Platform>x64</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Release|x64">
<Configuration>Release</Configuration>
<Platform>x64</Platform>
</ProjectConfiguration>
</ItemGroup>
<PropertyGroup Label="Globals">
<VCProjectVersion>16.0</VCProjectVersion>
<Keyword>Win32Proj</Keyword>
<ProjectGuid>{6d8ee35d-c813-4209-a185-d7198811e00c}</ProjectGuid>
<RootNamespace>pasm</RootNamespace>
<WindowsTargetPlatformVersion>10.0</WindowsTargetPlatformVersion>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v143</PlatformToolset>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v143</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v143</PlatformToolset>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v143</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
<ImportGroup Label="ExtensionSettings">
</ImportGroup>
<ImportGroup Label="Shared">
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<PropertyGroup Label="UserMacros" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<LinkIncremental>true</LinkIncremental>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<LinkIncremental>false</LinkIncremental>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<LinkIncremental>true</LinkIncremental>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<LinkIncremental>false</LinkIncremental>
</PropertyGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
<GenerateDebugInformation>true</GenerateDebugInformation>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
<GenerateDebugInformation>true</GenerateDebugInformation>
</Link>
</ItemDefinitionGroup>
<ItemGroup>
<ClCompile Include="get_instruction.c" />
<ClCompile Include="instructions.c" />
<ClCompile Include="main.c" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="get_instruction.h" />
<ClInclude Include="instructions.h" />
<ClInclude Include="instruction_set.h" />
<ClInclude Include="main.h" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
</ImportGroup>
</Project>

41
src/pasm.vcxproj.filters Normal file
View File

@@ -0,0 +1,41 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup>
<Filter Include="Fichiers sources">
<UniqueIdentifier>{4FC737F1-C7A5-4376-A066-2A32D752A2FF}</UniqueIdentifier>
<Extensions>cpp;c;cc;cxx;c++;cppm;ixx;def;odl;idl;hpj;bat;asm;asmx</Extensions>
</Filter>
<Filter Include="Fichiers d%27en-tête">
<UniqueIdentifier>{93995380-89BD-4b04-88EB-625FBE52EBFB}</UniqueIdentifier>
<Extensions>h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd</Extensions>
</Filter>
<Filter Include="instructions">
<UniqueIdentifier>{2a1becaf-56e9-47d6-9175-5b0e4bceae63}</UniqueIdentifier>
</Filter>
</ItemGroup>
<ItemGroup>
<ClCompile Include="main.c">
<Filter>Fichiers sources</Filter>
</ClCompile>
<ClCompile Include="get_instruction.c">
<Filter>Fichiers sources</Filter>
</ClCompile>
<ClCompile Include="instructions.c">
<Filter>instructions</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="instruction_set.h">
<Filter>Fichiers d%27en-tête</Filter>
</ClInclude>
<ClInclude Include="main.h">
<Filter>Fichiers sources</Filter>
</ClInclude>
<ClInclude Include="get_instruction.h">
<Filter>Fichiers sources</Filter>
</ClInclude>
<ClInclude Include="instructions.h">
<Filter>instructions</Filter>
</ClInclude>
</ItemGroup>
</Project>

10
src/pasm.vcxproj.user Normal file
View File

@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="Current" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<LocalDebuggerCommandArguments>../examples/keylogger.pasm</LocalDebuggerCommandArguments>
<DebuggerFlavor>WindowsLocalDebugger</DebuggerFlavor>
</PropertyGroup>
<PropertyGroup>
<ShowAllFiles>false</ShowAllFiles>
</PropertyGroup>
</Project>