Skip to content

Commit 0532914

Browse files
author
tobii-dev
committed
WinAPI32 -- GetUserNameA() hooked
1 parent 77aa790 commit 0532914

File tree

8 files changed

+118
-48
lines changed

8 files changed

+118
-48
lines changed

source/blur.cpp

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,28 +22,25 @@ gameConfig::gameConfig(char cfg_name[]) {
2222

2323
gameAPI::gameAPI(uintptr_t p) : config("cfg.ini") {
2424
moduleBase = p;
25-
};
25+
}
2626

2727

28-
//TODO: lets have all the init code here so we dont have to touch d3d9.cpp
28+
//TODO: lets have all the init code here
2929
void gameAPI::load() {
3030
console.start();
31-
if (install_menu_hook()) {
32-
blurAPI->console.print("!!");
33-
} else {
34-
blurAPI->console.print("ERROR in gameAPI::load() [install_menu_hook() returned false]");
35-
}
31+
if (install_username_hook()) blurAPI->console.print("set_usr_hook() -> true");
32+
//if (!install_menu_hook()) blurAPI->console.print("ERROR in gameAPI::load() [install_menu_hook() returned false]");
3633
}
3734

3835

3936
void gameAPI::unload() {
40-
//TODO: are we leaking mem rn?
37+
//TODO: unhooks
4138
console.close();
4239
}
4340

4441

4542
//TODO: move this somewhere sane
46-
bool gameAPI::toggle_SP_drifter() {
43+
bool gameAPI::toggle_drifter_mod_SP() {
4744
uintptr_t modAdr = moduleBase + ADDY_SP_MOD;
4845
int* modPtr = (int*) modAdr;
4946
int curMod = *modPtr;
@@ -60,13 +57,13 @@ bool gameAPI::toggle_SP_drifter() {
6057
}
6158

6259

63-
bool gameAPI::set_LAN_name(std::string szName) {
60+
bool gameAPI::set_name_LAN(std::string szName) {
6461
bool set = false;
6562
int len = szName.length();
66-
if (len && len <= LEN_LAN_NAME) {
63+
if (len && (len <= LEN_LAN_NAME)) {
6764
uintptr_t nameAdr = moduleBase + ADDY_LAN_NAME;
6865
short* ptr = (short*) nameAdr;
69-
for (int i = 0; i < len; i++) ptr[i] = szName[i];
66+
for (int i=0; i<len; i++) ptr[i] = szName[i];
7067
ptr[len] = NULL;
7168
set = true;
7269
}

source/blur.h

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,18 @@
1414
#define ADDY_SP_MOD 0xE14240
1515

1616
#define ADDY_LAN_NAME 0xCE5898
17-
#define LEN_LAN_NAME 16
17+
#define LEN_LAN_NAME 32
18+
19+
20+
#define ADDY_UNLOCK_INPUT 0xCC221C
21+
#define OFFSETS_UNLOCK_INPUT {0x14, 0x35C, 0xC, 0x4B0}
1822

1923

2024
struct gameConfig {
2125
std::string user_name;
2226
float fps;
2327
bool bFPSLimit;
24-
gameConfig(char cfg_name[]);
28+
gameConfig(char ini[]);
2529
};
2630

2731

@@ -30,12 +34,11 @@ struct gameAPI {
3034
gameHooks hooks;
3135
gameConfig config;
3236
gameConsole console;
33-
gameAPI(uintptr_t p);
37+
bool toggle_drifter_mod_SP();
38+
bool set_name_LAN(std::string szName);
3439
void load();
3540
void unload();
36-
37-
bool toggle_SP_drifter();
38-
bool set_LAN_name(std::string szName);
41+
gameAPI(uintptr_t p);
3942
};
4043

4144

source/blur_console.cpp

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
1-
#include "blur_console.h"
2-
#include "blur.h"
3-
#include <iostream>
1+
#include <iostream>
42
#include <io.h>
53
#include <fcntl.h>
64
#include <string>
75
#include <vector>
86
#include <conio.h>
97

108

9+
#include "blur_console.h"
10+
#include "blur.h"
11+
1112

1213
//TODO: buttons, better screen,
1314
DWORD WINAPI input_thread(void* arg) {
@@ -19,8 +20,6 @@ DWORD WINAPI input_thread(void* arg) {
1920
char c = NULL;
2021
char prompt = '+';
2122
while (true) {
22-
//UP1 \x1b^[[1A
23-
//DOWN1 \x1b^[[1B
2423
std::cout << "\r" << prompt << " " << *input;
2524
c = _getch();
2625
if ((c == EOF) || (c == '\n') || (c == '\r')) { //parse the stuff
@@ -79,8 +78,8 @@ void gameConsole::close() {
7978
}
8079

8180

81+
//TODO: mutex with input thread, custom prompt status, colours, allow open/close
8282
void gameConsole::print(std::string text) {
83-
//TODO: mutex with input thread, make nicer, make usable, etc...
8483
std::cout << "\r] " << text << std::endl;
8584
if (input->empty()) {
8685
std::cout << "\r> ";
@@ -102,7 +101,7 @@ bool gameConsole::cmd_handler(std::string cmd) {
102101
if ((cmd_args[1].front() == '"') && (cmd_args.back().back() == '"')) {
103102
std::vector<std::string> tmp = split(cmd, "\"");
104103
if (tmp.size() == 2) {
105-
if (blurAPI->set_LAN_name(tmp[1])) {
104+
if (blurAPI->set_name_LAN(tmp[1])) {
106105
print("Name changed to \""+tmp[1]+"\", exit multiplayer menu to use it.");
107106
blurAPI->config.user_name = tmp[1];
108107
} else {

source/blur_hooks.cpp

Lines changed: 33 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -10,48 +10,64 @@ bool install_menu_hook() {
1010
}
1111

1212

13-
//fn_ptr_t tmp_global_i_hate_this_variable = nullptr; //gone?
1413
bool install_menu_hook(fn_ptr_t fn) {
1514
bool hooked = false;
1615
uintptr_t src = blurAPI->moduleBase + HOOK_MENU_FUNC_ADDY;
17-
fn_ptr_t t = install_void_hook((void*) src, menu_hook_func, HOOK_MENU_FUNC_INS_LEN);
16+
fn_ptr_t t = install_void_hook((void*) src, hook_menu_leave, HOOK_MENU_FUNC_INS_LEN);
1817
if (t) {
19-
blurAPI->hooks.fn = fn;
20-
blurAPI->hooks.fn_trampoline = t;
21-
//tmp_global_i_hate_this_variable = t; //PLEASE I DONT LIKE YOU SO JUST WORK (FIXED?)
18+
blurAPI->hooks.fn_menu_callback = fn;
19+
blurAPI->hooks.fn_menu_trampoline = t;
2220
hooked = true;
2321
}
2422
return hooked;
2523
}
2624

27-
void __declspec(naked) menu_hook_func() {
25+
26+
//https://www.agner.org/optimize/calling_conventions.pdf
27+
//its a __thiscall, pointer to __THIS @ ECX register
28+
void __declspec(naked) hook_menu_leave() {
29+
/* no direct innits in __declspec(naked) funcs */
2830
void* f;
29-
f = blurAPI->hooks.fn_trampoline; //like this it works...
31+
f = blurAPI->hooks.fn_menu_trampoline;
3032
__asm PUSHAD;
3133
__asm PUSHFD;
3234
__asm nop;
3335
__asm nop;
34-
(blurAPI->hooks.fn)();
36+
(blurAPI->hooks.fn_menu_callback)();
3537
__asm nop;
3638
__asm nop;
3739
__asm POPFD;
3840
__asm POPAD;
3941
__asm jmp [f];
40-
//__asm jmp [tmp_global_i_hate_this_variable];
41-
//__asm jmp [blurAPI->hooks.fn_trampoline];
42-
//(blurAPI->hooks.fn_trampoline)();
4342
}
4443

4544

4645
void fn_hello_world() {
47-
if (blurAPI->set_LAN_name(blurAPI->config.user_name)) {
48-
blurAPI->console.print("SET NAME TO: [" + blurAPI->config.user_name + "]");
49-
} else {
50-
blurAPI->console.print("FAILED TO SET NAME TO: [" + blurAPI->config.user_name + "]");
51-
}
46+
blurAPI->console.print("Hello world -- fn_hello_world()!");
5247
//aux_print_registers();
5348
}
5449

50+
51+
bool install_username_hook() {
52+
return set_call_func((void*)(blurAPI->moduleBase + HOOK_NAME_FUNC_ADDY), (fn_ptr_t) hook_GetUserNameA);
53+
}
54+
55+
56+
57+
bool __stdcall hook_GetUserNameA(char* buff, unsigned long * len) {
58+
//bool r = GetUserNameA(buff, len); //original func
59+
bool r = true;
60+
std::string name = blurAPI->config.user_name;
61+
int n = name.length();
62+
for (int i=0; i<n; i++) buff[i] = name[i];
63+
buff[n] = NULL;
64+
*len = n;
65+
blurAPI->console.print("Name set to: " + name);
66+
return r;
67+
}
68+
69+
70+
5571
//TODO: debug stuff
5672
//https://en.wikibooks.org/wiki/X86_Assembly/X86_Architecture
5773
void aux_print_registers() {
@@ -80,3 +96,4 @@ void aux_print_registers() {
8096
__asm {mov [reg_dst], edi};
8197
std::printf("%#010x [reg_dst], edi\n", reg_dst);
8298
}
99+

source/blur_hooks.h

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,29 @@
11
#pragma once
22
#include "mem.h"
33

4+
//TODO: clean menu hook
45
#define HOOK_MENU_FUNC_ADDY 0x34B8A7
56
#define HOOK_MENU_FUNC_INS_LEN 6
67

8+
//winapi32 call to GetUserNameA() hook here
9+
#define HOOK_NAME_FUNC_ADDY 0x95CD2A
10+
#define HOOK_NAME_FUNC_INS_LEN 6
711

812
struct gameHooks {
9-
fn_ptr_t fn_trampoline = nullptr;
10-
fn_ptr_t fn = nullptr;
13+
fn_ptr_t fn_menu_trampoline = nullptr;
14+
fn_ptr_t fn_menu_callback = nullptr;
1115
};
1216

1317

1418
bool install_menu_hook();
1519
bool install_menu_hook(fn_ptr_t fn);
20+
void hook_menu_leave(); //its a __thiscall in Blur.exe?
1621

22+
void fn_hello_world();
1723

18-
void menu_hook_func();
1924

25+
bool install_username_hook();
26+
bool __stdcall hook_GetUserNameA(char* buff, unsigned long *len);
2027

21-
void fn_hello_world();
22-
void aux_print_registers();
28+
29+
//void aux_print_registers();

source/d3d9.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ HRESULT f_IDirect3DDevice9::EndScene() {
190190
} else if ((GetKeyState(VK_DIVIDE) < 0)) {
191191
if (!bPressed) {
192192
blurAPI->console.print("PRESSED: VK_DIVIDE");
193-
blurAPI->toggle_SP_drifter();
193+
blurAPI->toggle_drifter_mod_SP();
194194
}
195195
bPressed = true;
196196
} else if ((GetKeyState(VK_NUMPAD0) < 0)) {

source/mem.cpp

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include <Windows.h>
22
#include <iostream>
3+
#include <string>
34

45
#include "mem.h"
56

@@ -34,3 +35,41 @@ fn_ptr_t install_void_hook(void* src, fn_ptr_t f, int len) {
3435
}
3536
return (fn_ptr_t) trampolineAddr;
3637
}
38+
39+
40+
//uintptr_t follow_offsets(uintptr_t ptr, std::vector<unsigned int> offsets) {
41+
uintptr_t follow_offsets(uintptr_t ptr, std::vector<uintptr_t> offsets) {
42+
uintptr_t addr = ptr;
43+
//uintptr_t tmp;
44+
//DWORD srcProtection, _;
45+
for (unsigned int i = 0; i<offsets.size(); i++) {
46+
//VirtualProtect((void*)addr, sizeof(addr), PAGE_EXECUTE_READWRITE, &srcProtection);
47+
//tmp = addr;
48+
addr = *(uintptr_t*) addr;
49+
if (addr != NULL) {
50+
addr += offsets[i];
51+
//restore stuffs
52+
//VirtualProtect((void*)tmp, sizeof(tmp), srcProtection, &_);
53+
} else {
54+
//VirtualProtect((void*)tmp, sizeof(tmp), srcProtection, &_);
55+
break;
56+
}
57+
}
58+
return addr;
59+
}
60+
61+
62+
63+
bool __stdcall set_call_func(void* src, fn_ptr_t f) {
64+
bool r = false;
65+
void* callArg = VirtualAlloc(0, sizeof(uintptr_t), MEM_COMMIT | MEM_RESERVE, PAGE_EXECUTE_READWRITE);
66+
if (callArg) {
67+
*(uintptr_t*)callArg = (uintptr_t) f;
68+
DWORD srcProtection, _;
69+
VirtualProtect(src, OP_CALL_LEN, PAGE_EXECUTE_READWRITE, &srcProtection);
70+
*(uintptr_t*)(((uint8_t*)src) + 2) = (uintptr_t)callArg;
71+
VirtualProtect(src, OP_CALL_LEN, srcProtection, &_);
72+
r = true;
73+
}
74+
return r;
75+
}

source/mem.h

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,17 @@
11
#pragma once
22

3+
#include <vector>
4+
35
#define OP_NOP 0x90
46
#define OP_JMP 0xE9
57
#define OP_JMP_LEN 5
68

7-
typedef void (*fn_ptr_t)(); //pointer to a function
9+
#define OP_CALL_LEN 6
10+
11+
typedef void (*fn_ptr_t)();
812

913
fn_ptr_t install_void_hook(void* src, fn_ptr_t f, int len);
14+
15+
uintptr_t follow_offsets(uintptr_t ptr, std::vector<uintptr_t> offsets);
16+
17+
bool __stdcall set_call_func(void* src, fn_ptr_t f);

0 commit comments

Comments
 (0)