micetools/src/micetools/dll/hooks/network.c

202 lines
7.8 KiB
C
Raw Normal View History

2022-10-30 17:33:02 +00:00
#include "network.h"
int WINAPI Fake_connect(SOCKET s, const SOCKADDR* name, int namelen) {
ULONG addr = _byteswap_ulong(((SOCKADDR_IN*)name)->sin_addr.S_un.S_addr);
USHORT port = _byteswap_ushort(((SOCKADDR_IN*)name)->sin_port);
2023-03-28 20:08:02 +00:00
// Poorly exclude nxauth and mxgcatcher.
// TODO: better
2023-04-10 17:29:59 +00:00
if (port != 40190 && port != 40110 && port != 40102 && port != 40103) {
2023-03-13 21:49:07 +00:00
log_info(plfNetwork, "connect(%hhu.%hhu.%hhu.%hhu:%hu)", (addr >> 24) & 0xff,
(addr >> 16) & 0xff, (addr >> 8) & 0xff, addr & 0xff, port);
2023-02-14 07:46:24 +00:00
}
2022-10-30 17:33:02 +00:00
return True_connect(s, name, namelen);
}
2023-02-14 07:09:08 +00:00
int WINAPI Fake_bind(SOCKET s, const SOCKADDR* name, int namelen) {
ULONG addr = _byteswap_ulong(((SOCKADDR_IN*)name)->sin_addr.S_un.S_addr);
USHORT port = _byteswap_ushort(((SOCKADDR_IN*)name)->sin_port);
2023-03-13 21:49:07 +00:00
log_info(plfNetwork, "bind(%hhu.%hhu.%hhu.%hhu:%hu)", (addr >> 24) & 0xff, (addr >> 16) & 0xff,
2023-02-14 07:09:08 +00:00
(addr >> 8) & 0xff, addr & 0xff, port);
return True_bind(s, name, namelen);
}
2022-10-30 17:33:02 +00:00
#define IF_INDEX 1
2023-02-10 04:22:16 +00:00
// Sega prefix
#define MAC_PREFIX_0 0xD8
#define MAC_PREFIX_1 0xBB
#define MAC_PREFIX_2 0xC1
2022-10-30 17:33:02 +00:00
DWORD WINAPI FakeGetIfTable(PMIB_IFTABLE pIfTable, PULONG pdwSize, BOOL bOrder) {
2023-04-10 17:29:59 +00:00
log_misc(plfNetwork, "Injecting fake IfTable");
2022-10-30 17:33:02 +00:00
MIB_IFROW* row;
uint32_t nbytes;
if (pdwSize == NULL) return ERROR_INVALID_PARAMETER;
nbytes = *pdwSize;
*pdwSize = sizeof(*row) + sizeof(DWORD);
if (pIfTable == NULL || nbytes < sizeof(*row) + sizeof(DWORD)) {
return ERROR_INSUFFICIENT_BUFFER;
}
pIfTable->dwNumEntries = 1;
row = pIfTable->table;
memset(row, 0, sizeof(*row));
wcscpy_s(row->wszName, _countof(row->wszName), L"RING2 Ethernet");
row->dwIndex = IF_INDEX;
row->dwType = IF_TYPE_ETHERNET_CSMACD;
row->dwMtu = 4200;
row->dwSpeed = 1000000000;
row->dwPhysAddrLen = 6;
row->bPhysAddr[0] = MAC_PREFIX_0;
row->bPhysAddr[1] = MAC_PREFIX_1;
row->bPhysAddr[2] = MAC_PREFIX_2;
row->bPhysAddr[3] = (MiceConfig.network.mac >> 16) & 0xff;
row->bPhysAddr[4] = (MiceConfig.network.mac >> 8) & 0xff;
row->bPhysAddr[5] = MiceConfig.network.mac & 0xff;
2022-10-30 17:33:02 +00:00
row->dwAdminStatus = 1;
row->dwOperStatus = IF_OPER_STATUS_OPERATIONAL;
return ERROR_SUCCESS;
// DWORD ret = TrueGetIfTable(pIfTable, pdwSize, bOrder);
// if (ret == NO_ERROR) {
// for (size_t i = 0; i < pIfTable->dwNumEntries; i++) {
// pIfTable->table[i].bPhysAddr[0] = 0x00;
// pIfTable->table[i].bPhysAddr[1] = 0xD0;
// pIfTable->table[i].bPhysAddr[2] = 0xF1;
// }
// }
// return ret;
}
typedef struct {
char* name;
unsigned int* address;
2022-10-30 17:33:02 +00:00
} dns;
2022-10-30 17:33:02 +00:00
dns INTERCEPT_DNS[] = {
// Startup
{ "naominet.jp", &(MiceConfig.network.naominet_jp) },
2022-10-30 17:33:02 +00:00
// Billing
{ "ib.naominet.jp", &(MiceConfig.network.ib_naominet_jp) },
2022-10-30 17:33:02 +00:00
// Aime
{ "aime.naominet.jp", &(MiceConfig.network.aime_naominet_jp) },
2022-10-30 17:33:02 +00:00
// Routers (ping targets)
{ "tenporouter.loc", &(MiceConfig.network.tenporouter_loc) },
{ "bbrouter.loc", &(MiceConfig.network.bbrouter_loc) }, // Must match tenporouter
{ "mobirouter.loc", &(MiceConfig.network.mobirouter_loc) },
{ "dslrouter.loc", &(MiceConfig.network.dslrouter_loc) },
2022-10-30 17:33:02 +00:00
};
DNS_RECORDA dummy_record;
DNS_STATUS WINAPI FakeDnsQuery_A(PCSTR pszName, WORD wType, DWORD Options, PVOID pExtra,
PDNS_RECORDA* ppQueryResults, PVOID* pReserved) {
if (ppQueryResults) {
for (size_t i = 0; i < sizeof INTERCEPT_DNS / sizeof INTERCEPT_DNS[0]; i++) {
if (strcmp(pszName, INTERCEPT_DNS[i].name) == 0) {
2023-02-10 04:22:16 +00:00
printf("%08x\n", MiceConfig.network.naominet_jp);
2023-03-13 21:49:07 +00:00
log_info(plfNetwork, "DNS Replacing %s with %08x", pszName,
*INTERCEPT_DNS[i].address);
2022-10-30 17:33:02 +00:00
// We only support replacing at most one address, but that's all we'll ever need to!
(*ppQueryResults) = &dummy_record;
(*ppQueryResults)->pNext = NULL;
(*ppQueryResults)->wType = DNS_TYPE_A;
(*ppQueryResults)->Data.A.IpAddress = _byteswap_ulong(*INTERCEPT_DNS[i].address);
2022-10-30 17:33:02 +00:00
return ERROR_SUCCESS;
}
}
}
2023-03-13 21:49:07 +00:00
log_warning(plfNetwork, "DNS passthrough for %s", pszName);
2022-10-30 17:33:02 +00:00
return TrueDnsQuery_A(pszName, wType, Options, pExtra, ppQueryResults, pReserved);
};
INT WSAAPI FakeWSAStringToAddressA(LPSTR AddressString, INT AddressFamily,
LPWSAPROTOCOL_INFOA lpProtocolInfo, LPSOCKADDR lpAddress,
LPINT lpAddressLength) {
2023-03-13 21:49:07 +00:00
log_misc(plfNetwork, "WSA DNS lookup for %s", AddressString);
2022-10-30 17:33:02 +00:00
for (size_t i = 0; i < sizeof INTERCEPT_DNS / sizeof INTERCEPT_DNS[0]; i++) {
if (strcmp(AddressString, INTERCEPT_DNS[i].name) == 0) {
2023-03-13 21:49:07 +00:00
log_info(plfNetwork, "WSA DNS Replacing %s with %08x", AddressString,
2023-02-10 04:22:16 +00:00
*INTERCEPT_DNS[i].address);
2022-10-30 17:33:02 +00:00
lpAddress->sa_family = AF_INET;
2023-02-10 04:22:16 +00:00
PULONG addr = &(((SOCKADDR_IN*)lpAddress)->sin_addr.S_un.S_addr);
*addr = _byteswap_ulong(
*INTERCEPT_DNS[i]
.address); //(192UL) | (168UL << 8) | (103UL << 16) | (200UL << 24);
2022-10-30 17:33:02 +00:00
return ERROR_SUCCESS;
}
}
2023-03-13 21:49:07 +00:00
log_warning(plfNetwork, "WSA DNS passthrough for %s", AddressString);
2022-10-30 17:33:02 +00:00
return TrueWSAStringToAddressA(AddressString, AddressFamily, lpProtocolInfo, lpAddress,
lpAddressLength);
}
2023-03-13 21:49:07 +00:00
int __stdcall Fake_socket(int domain, int type, int protocol) {
int sock = True_socket(domain, type, protocol);
log_trace(plfNetwork, "Creating new socket: %d/%s/%d -> %d", domain,
type == 1 ? "SOCK_STREAM"
: type == 2 ? "SOCK_DGRAM"
: type == 3 ? "SOCK_RAW"
: type == 4 ? "SOCK_RDM"
: type == 5 ? "SOCK_SEQPACKET"
: "Unknown",
protocol, sock);
return sock;
}
2023-04-11 02:27:25 +00:00
static SOCKADDR pingSentTo;
2023-03-13 21:49:07 +00:00
static unsigned char pingInfo[4];
2023-04-11 02:27:25 +00:00
static SOCKADDR_IN toLocalhost = {
.sin_addr.S_un.S_addr = 0x0100007f,
2023-03-13 21:49:07 +00:00
.sin_family = AF_INET,
.sin_port = 24,
.sin_zero = 0,
};
2023-04-11 02:27:25 +00:00
int __stdcall Fake_sendto(SOCKET s, const char* buf, int len, int flags, const PSOCKADDR to,
2023-03-13 21:49:07 +00:00
int tolen) {
// Hardcoded ICMP4 ping "detection"
// TODO: Only do this if the socket is using the ICMP protocol
if (len == 8 && buf[0] == 0x08 && buf[1] == 0x00) {
2023-04-11 02:27:25 +00:00
uint32_t addr = ((PSOCKADDR_IN)to)->sin_addr.S_un.S_addr;
2023-03-13 21:49:07 +00:00
memcpy(&pingSentTo, to, sizeof pingSentTo);
uint16_t seq = _byteswap_ushort(((uint16_t*)buf)[3]);
memcpy(pingInfo, buf + 4, 4);
memcpy(&toLocalhost, to, sizeof toLocalhost);
toLocalhost.sin_addr.S_un.S_addr = 0x0100007f; // 127.0.0.1
log_warning(plfNetwork, "(probable) Ping to: %d.%d.%d.%d (%d). Redirecting to localhost",
addr & 0xff, (addr >> 8) & 0xff, (addr >> 16) & 0xff, addr >> 24,
2023-04-11 02:27:25 +00:00
((PSOCKADDR_IN)to)->sin_port, seq);
return True_sendto(s, buf, len, flags, (PSOCKADDR)&toLocalhost, tolen);
2023-03-13 21:49:07 +00:00
}
return True_sendto(s, buf, len, flags, to, tolen);
}
2022-10-30 17:33:02 +00:00
void hook_network() {
2023-02-10 04:22:16 +00:00
hook("Ws2_32.dll", "connect", Fake_connect, (void**)&True_connect);
2023-03-13 21:49:07 +00:00
hook("Ws2_32.dll", "socket", Fake_socket, (void**)&True_socket);
2023-02-14 07:09:08 +00:00
hook("Ws2_32.dll", "bind", Fake_bind, (void**)&True_bind);
2023-03-13 21:49:07 +00:00
hook("Ws2_32.dll", "sendto", Fake_sendto, (void**)&True_sendto);
2022-10-30 17:33:02 +00:00
hook("Ws2_32.dll", "WSAStringToAddressA", FakeWSAStringToAddressA,
2023-02-10 04:22:16 +00:00
(void**)&TrueWSAStringToAddressA);
hook("Iphlpapi.dll", "GetIfTable", FakeGetIfTable, (void**)&TrueGetIfTable);
hook("Dnsapi.dll", "DnsQuery_A", FakeDnsQuery_A, (void**)&TrueDnsQuery_A);
2022-10-30 17:33:02 +00:00
}