~ruther/NosSmooth

ref: fa8f04cd1a67693b7463e20bac640e434d262de5 NosSmooth/Local/NosSmooth.LocalCore/NetworkUnmanaged.cpp -rw-r--r-- 3.5 KiB
fa8f04cd — František Boháček chore: remove debug test projects 3 years ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#include "NetworkUnmanaged.h"
#include <detours.h>
#include <windows.h>

using namespace NosSmoothCore;

const BYTE SEND_PATTERN[] = { 0x53, 0x56, 0x8B, 0xF2, 0x8B, 0xD8, 0xEB, 0x04 };
const BYTE RECV_PATTERN[] = { 0x55, 0x8B, 0xEC, 0x83, 0xC4, 0xF4, 0x53, 0x56, 0x57, 0x33, 0xC9, 0x89, 0x4D, 0xF4, 0x89, 0x55, 0xFC, 0x8B, 0xD8, 0x8B, 0x45, 0xFC };
const BYTE PACKET_CALLER_PATTERN[] = { 0xA1, 0x00, 0x00, 0x00, 0x00, 0x8B, 0x00, 0xE8, 0x00, 0x00, 0x00, 0x00, 0xA1, 0x00, 0x00, 0x00, 0x00, 0x8B, 0x00, 0x33, 0xD2, 0x89, 0x10 };

LPCSTR SEND_MASK = "xxxxxxxx";
LPCSTR RECV_MASK = "xxxxxxxxxxxxxxxxxxxxxx";
LPCSTR PACKET_CALLER_MASK = "x????xxx????x????xxxxxx";

void PacketSendDetour()
{
    const char* packet = nullptr;

    __asm
    {
        pushad
        pushfd

        mov packet, edx
    }

    NetworkUnmanaged::GetInstance()->ExecuteSendCallback(packet);

    __asm
    {
        popfd
        popad
    }
}

void PacketReceiveDetour()
{
    const char* packet = nullptr;

    __asm
    {
        pushad
        pushfd

        mov packet, edx
    }

    NetworkUnmanaged::GetInstance()->ExecuteReceiveCallback(packet);

    __asm
    {
        popfd
        popad
    }
}

void NetworkUnmanaged::Setup(ModuleHook moduleHook)
{
    auto sendFunction = moduleHook.FindPattern(SEND_PATTERN, SEND_MASK);
    auto receiveFunction = moduleHook.FindPattern(RECV_PATTERN, RECV_MASK);
    auto callerObject = *reinterpret_cast<unsigned int*>((DWORD_PTR)moduleHook.FindPattern(PACKET_CALLER_PATTERN, PACKET_CALLER_MASK) + 1);

    if (sendFunction == 0)
    {
        throw "Could not find send packet function.";
    }

    if (receiveFunction == 0)
    {
        throw "Could not find receive packet function.";
    }

    if (callerObject == 0)
    {
        throw "Could not find packet caller object.";
    }

    _sendPacketAddress = sendFunction;
    _receivePacketAddress = receiveFunction;
    _callerObject = callerObject;

    DetourTransactionBegin();
    DetourUpdateThread(GetCurrentThread());
    DetourAttach(&reinterpret_cast<void*&>(sendFunction), PacketSendDetour);
    DetourAttach(&reinterpret_cast<void*&>(receiveFunction), PacketReceiveDetour);
    DetourTransactionCommit();
}

void NetworkUnmanaged::SendPacket(const char *packet)
{
    __asm
    {
        mov esi, this
        mov eax, [esi]._callerObject
        mov eax, dword ptr ds : [eax]
        mov eax, dword ptr ds : [eax]
        mov edx, packet
        call[esi]._sendPacketAddress
    }
}

void NetworkUnmanaged::ReceivePacket(const char* packet)
{
    __asm
    {
        mov esi, this
        mov eax, [esi]._callerObject
        mov eax, dword ptr ds : [eax]
        mov eax, dword ptr ds : [eax]
        mov eax, dword ptr ds : [eax + 34h]
        mov edx, packet
        call[esi]._receivePacketAddress
    }
}

void NetworkUnmanaged::ResetHooks()
{
    DetourTransactionBegin();
    DetourUpdateThread(GetCurrentThread());
    DetourDetach(&reinterpret_cast<void*&>(_sendPacketAddress), PacketSendDetour);
    DetourDetach(&reinterpret_cast<void*&>(_receivePacketAddress), PacketReceiveDetour);
    DetourTransactionCommit();
}

void NetworkUnmanaged::SetReceiveCallback(PacketCallback callback)
{
    _receiveCallback = callback;
}

void NetworkUnmanaged::SetSendCallback(PacketCallback callback)
{
    _sendCallback = callback;
}

bool NetworkUnmanaged::ExecuteReceiveCallback(const char *packet)
{
    return _receiveCallback(packet);
}

bool NetworkUnmanaged::ExecuteSendCallback(const char* packet)
{
    return _sendCallback(packet);
}
Do not follow this link