dns: add port overriding support

This commit is contained in:
r0x5a
2024-12-12 02:28:02 +08:00
parent 6d8ffb46ef
commit d8202e1df4
7 changed files with 117 additions and 7 deletions

View File

@ -88,6 +88,11 @@ static DWORD WINAPI hook_send(
int len,
int flags);
static int WINAPI hook_connect(
SOCKET s,
const struct sockaddr *name,
int namelen);
/* Link pointers */
static DNS_STATUS (WINAPI *next_DnsQuery_A)(
@ -135,6 +140,11 @@ static DWORD (WINAPI *next_send)(
int len,
int flags);
static int (__stdcall *next_connect)(
SOCKET s,
const struct sockaddr *name,
int namelen);
static const struct hook_symbol dns_hook_syms_dnsapi[] = {
{
.name = "DnsQuery_A",
@ -180,11 +190,22 @@ static struct hook_symbol http_hook_syms_ws2[] = {
},
};
static struct hook_symbol port_hook_syms_ws2[] = {
{
.name = "connect",
.patch = hook_connect,
.link = (void **) &next_connect
},
};
static bool dns_hook_initted;
static CRITICAL_SECTION dns_hook_lock;
static struct dns_hook_entry *dns_hook_entries;
static size_t dns_hook_nentries;
static char received_title_url[255];
static unsigned short startup_port;
static unsigned short billing_port;
static unsigned short aimedb_port;
static void dns_hook_init(void)
{
@ -226,6 +247,21 @@ void http_hook_init(){
_countof(http_hook_syms_ws2));
}
void port_hook_init(unsigned short _startup_port, unsigned short _billing_port, unsigned short _aimedb_port){
startup_port = _startup_port;
billing_port = _billing_port;
aimedb_port = _aimedb_port;
for (size_t i = 0; i < _countof(port_hook_syms_ws2); ++i) {
port_hook_syms_ws2[i].ordinal = get_function_ordinal("ws2_32.dll", port_hook_syms_ws2[i].name);
}
hook_table_apply(
NULL,
"ws2_32.dll",
port_hook_syms_ws2,
_countof(port_hook_syms_ws2));
}
// This function match domain and subdomains like *.naominet.jp.
bool match_domain(const wchar_t* target, const wchar_t* pattern) {
if (_wcsicmp(pattern, target) == 0) {
@ -624,7 +660,7 @@ static bool WINAPI hook_WinHttpCrackUrl(
if (match_domain(pwszUrl, pos->from)) {
wchar_t* toAddr = pos->to;
wchar_t titleBuffer[255];
if(wcscmp(toAddr, L"title") == 0) {
size_t wstr_c;
mbstowcs_s(&wstr_c, titleBuffer, 255, received_title_url, strlen(received_title_url));
@ -651,6 +687,49 @@ static bool WINAPI hook_WinHttpCrackUrl(
);
}
int WINAPI hook_connect(SOCKET s, const struct sockaddr *name, int namelen) {
const struct sockaddr_in *n;
struct sockaddr_in new_name;
unsigned ip;
unsigned short port, new_port;
EnterCriticalSection(&dns_hook_lock);
n = (const struct sockaddr_in *)name;
ip = n->sin_addr.S_un.S_addr;
if (WSANtohs(s, n->sin_port, &port)) return SOCKET_ERROR;
if (port == 80 && startup_port) {
new_port = startup_port;
} else if (port == 8443 && billing_port) {
new_port = billing_port;
} else if (port == 22345 && aimedb_port) {
new_port = aimedb_port;
} else { // No match
dprintf("TCP Connect: %u.%u.%u.%u:%hu\n", ip & 0xff, (ip >> 8) & 0xff, (ip >> 16) & 0xff, (ip >> 24) & 0xff, port);
LeaveCriticalSection(&dns_hook_lock);
return next_connect(
s,
name,
namelen
);
}
// matched
new_name = *n;
if (WSAHtons(s, new_port, &new_name.sin_port)) return SOCKET_ERROR;
dprintf("TCP Connect: %u.%u.%u.%u:%hu, mapped to port %hu\n", ip & 0xff, (ip >> 8) & 0xff, (ip >> 16) & 0xff, (ip >> 24) & 0xff, port, new_port);
LeaveCriticalSection(&dns_hook_lock);
return next_connect(
s,
(const struct sockaddr *)&new_name,
sizeof(new_name)
);
}
DWORD WINAPI hook_send(SOCKET s, const char* buf, int len, int flags) {
if (strstr(buf, "HTTP/") != NULL) {
char *new_buf = malloc(len + 1);