-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathdllmain.cpp
More file actions
166 lines (133 loc) · 5.17 KB
/
Copy pathdllmain.cpp
File metadata and controls
166 lines (133 loc) · 5.17 KB
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
#include "pch.h"
#include <Windows.h>
#include <winsock2.h>
#include <ws2tcpip.h>
#include "detours.h"
#include <iostream>
#include <iomanip>
#pragma comment(lib, "ws2_32.lib")
#pragma comment(lib, "detours.lib")
void Hexdump(const char* data, int length);
typedef int (WINAPI* ConnectFunction)(SOCKET s, const struct sockaddr* name, int namelen);
typedef int (WINAPI* SendFunction)(SOCKET s, const char* buf, int len, int flags);
typedef int (WINAPI* RecvFunction)(SOCKET s, char* buf, int len, int flags);
typedef int (WSAAPI* WSARecvFunction)(SOCKET s, LPWSABUF lpBuffers, DWORD dwBufferCount, LPDWORD lpNumberOfBytesRecvd, LPDWORD lpFlags, LPWSAOVERLAPPED lpOverlapped, LPWSAOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine);
ConnectFunction originalConnect = nullptr;
SendFunction originalSend = nullptr;
RecvFunction originalRecv = nullptr;
WSARecvFunction originalWSARecv = nullptr;
int WINAPI DetouredConnect(SOCKET s, const struct sockaddr* name, int namelen)
{
sockaddr_in* sa_in = (sockaddr_in*)name;
int port = ntohs(sa_in->sin_port);
const char* ipAddress = inet_ntoa(sa_in->sin_addr);
// Print the IP and port it's trying to connect to
printf("Connecting to IP: %s, Port: %d\n", ipAddress, port);
int result = originalConnect(s, name, namelen);
return result;
}
int WINAPI DetouredSend(SOCKET s, const char* buf, int len, int flags)
{
// Your custom logic for send here
Hexdump(buf, len);
int result = originalSend(s, buf, len, flags);
return result;
}
int WINAPI DetouredRecv(SOCKET s, char* buf, int len, int flags)
{
printf("RECV");
int result = originalRecv(s, buf, len, flags);
if (result > 0) {
// Hexdump the received data
Hexdump(buf, result);
}
return result;
}
int WSAAPI DetouredWSARecv(SOCKET s, LPWSABUF lpBuffers, DWORD dwBufferCount, LPDWORD lpNumberOfBytesRecvd, LPDWORD lpFlags, LPWSAOVERLAPPED lpOverlapped, LPWSAOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine)
{
printf("WSARecv - Buffer Count: %u\n", dwBufferCount);
// Iterate through the lpBuffers array and print the size of each buffer
for (DWORD i = 0; i < dwBufferCount; i++) {
printf("Buffer %u Size: %u\n", i, lpBuffers[i].len);
}
int result = originalWSARecv(s, lpBuffers, dwBufferCount, lpNumberOfBytesRecvd, lpFlags, lpOverlapped, lpCompletionRoutine);
Hexdump(lpBuffers->buf, 256); // You can adjust the size you want to dump here
return result;
}
extern "C" __declspec(dllexport) void Lain1337()
{
// Your custom code here
}
BOOL APIENTRY DllMain(HMODULE hModule, DWORD ul_reason_for_call, LPVOID lpReserved)
{
if (AllocConsole()) {
freopen("CONOUT$", "w", stdout);
SetConsoleTitle(L"Injected Console");
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_RED);
std::cout << "This is the injected console!" << std::endl;
}
if (ul_reason_for_call == DLL_PROCESS_ATTACH)
{
WSADATA wsaData;
if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0)
{
return FALSE;
}
DetourTransactionBegin();
DetourUpdateThread(GetCurrentThread());
originalConnect = (ConnectFunction)GetProcAddress(GetModuleHandle(L"ws2_32.dll"), "connect");
originalSend = (SendFunction)GetProcAddress(GetModuleHandle(L"ws2_32.dll"), "send");
originalRecv = (RecvFunction)GetProcAddress(GetModuleHandle(L"ws2_32.dll"), "recv");
originalWSARecv = (WSARecvFunction)GetProcAddress(GetModuleHandle(L"ws2_32.dll"), "WSARecv");
DetourAttach(&(PVOID&)originalConnect, DetouredConnect);
DetourAttach(&(PVOID&)originalSend, DetouredSend);
DetourAttach(&(PVOID&)originalRecv, DetouredRecv);
DetourAttach(&(PVOID&)originalWSARecv, DetouredWSARecv);
DetourTransactionCommit();
}
else if (ul_reason_for_call == DLL_PROCESS_DETACH)
{
DetourTransactionBegin();
DetourUpdateThread(GetCurrentThread());
DetourDetach(&(PVOID&)originalConnect, DetouredConnect);
DetourDetach(&(PVOID&)originalSend, DetouredSend);
DetourDetach(&(PVOID&)originalRecv, DetouredRecv);
DetourDetach(&(PVOID&)originalWSARecv, DetouredWSARecv);
DetourTransactionCommit();
WSACleanup();
}
return TRUE;
}
void Hexdump(const char* data, int length)
{
for (int i = 0; i < length; i += 16)
{
std::cout << std::hex << std::setw(8) << std::setfill('0') << i << ": ";
for (int j = 0; j < 16; ++j)
{
if (i + j < length)
std::cout << std::hex << std::setw(2) << std::setfill('0') << (unsigned int)(unsigned char)data[i + j] << " ";
else
std::cout << " ";
if (j == 7)
std::cout << " ";
}
std::cout << " ";
for (int j = 0; j < 16; ++j)
{
if (i + j < length)
{
char c = data[i + j];
if (c >= 32 && c <= 126)
std::cout << c;
else
std::cout << ".";
}
else
{
std::cout << " ";
}
}
std::cout << std::endl;
}
}