MSNRedirector/MSNRedirector/dllmain.cpp
2024-06-15 01:25:34 -04:00

164 lines
No EOL
6.6 KiB
C++

// dllmain.cpp : Defines the entry point for the DLL application.
#include "dllmain.h"
#include "pch.h"
#pragma comment (lib, "wininet.lib")
#pragma comment (lib, "Ws2_32.lib")
// Used as a fallback if server cannot be read from the registry
const char* MESSENGERSERVICE_DEFAULT = "vm9.computernewb.com";
char* MESSENGERSERVICE = NULL;
HINTERNET(WINAPI * Real_InternetConnectA) (HINTERNET hInternet, LPCSTR lpszServerName, INTERNET_PORT nServerPort, LPCSTR lpszUserName, LPCSTR lpszPassword, DWORD dwService, DWORD dwFlags, DWORD_PTR dwContext) = InternetConnectA;
HINTERNET(WINAPI * Real_InternetConnectW) (HINTERNET hInternet, LPCWSTR lpszServerName, INTERNET_PORT nServerPort, LPCWSTR lpszUserName, LPCWSTR lpszPassword, DWORD dwService, DWORD dwFlags, DWORD_PTR dwContect) = InternetConnectW;
hostent * (WSAAPI * Real_gethostbyname) (const char *name) = gethostbyname;
INT(WSAAPI * Real_getaddrinfo) (PCSTR pNodeName, PCSTR pServiceName, const ADDRINFOA *pHints, PADDRINFOA *ppResult) = getaddrinfo;
BOOL ServerNeedsPatching(const char* server) {
return (
/* msn live domains, commonly used */
strcmp(server, "nexus.passport.com") == 0 ||
strcmp(server, "messenger.hotmail.com") == 0 ||
strcmp(server, "loginnet.passport.com") == 0 ||
strcmp(server, "config.messenger.msn.com") == 0 ||
strcmp(server, "gateway.messenger.hotmail.com") == 0 ||
strcmp(server, "muser.gateway.messenger.hotmail.com") == 0 ||
/* msn internal domains, rarely used, common in betas */
strcmp(server, "nexus.passport-int.com") == 0 ||
strcmp(server, "messenger.hotmail-int.com") == 0 ||
strcmp(server, "config.messenger.msn-int.com") == 0 /* || */
/* msn pre-production environment domains, very rarely used */
/*strcmp(server, "nexus.passport-ppe.com") == 0 ||
strcmp(server, "nexus.passporttest.com") == 0 ||
strcmp(server, "messenger.hotmail-ppe.com") == 0 ||
strcmp(server, "config.messenger.msn-ppe.com") == 0*/
);
}
HINTERNET WINAPI Patched_InternetConnectA(HINTERNET hInternet, LPCSTR lpszServerName, INTERNET_PORT nServerPort, LPCSTR lpszUserName, LPCSTR lpszPassword, DWORD dwService, DWORD dwFlags, DWORD_PTR dwContext) {
if (ServerNeedsPatching(lpszServerName)) {
return Real_InternetConnectA(hInternet, MESSENGERSERVICE, nServerPort, lpszUserName, lpszPassword, dwService, dwFlags, dwContext);
}
return Real_InternetConnectA(hInternet, lpszServerName, nServerPort, lpszUserName, lpszPassword, dwService, dwFlags, dwContext);
}
HINTERNET WINAPI Patched_InternetConnectW(HINTERNET hInternet, LPCWSTR lpszServerName, INTERNET_PORT nServerPort, LPCWSTR lpszUserName, LPCWSTR lpszPassword, DWORD dwService, DWORD dwFlags, DWORD_PTR dwContext) {
if (ServerNeedsPatching(CT2CA(lpszServerName))) {
return Real_InternetConnectW(hInternet, CA2CT(MESSENGERSERVICE), nServerPort, lpszUserName, lpszPassword, dwService, dwFlags, dwContext);
}
return Real_InternetConnectW(hInternet, lpszServerName, nServerPort, lpszUserName, lpszPassword, dwService, dwFlags, dwContext);
}
hostent *WSAAPI Patched_gethostbyname(const char* name) {
// somehow things (winmsgr6) can call this with a null pointer and ws2 is fine with it
if (name != NULL && ServerNeedsPatching(name)) {
name = MESSENGERSERVICE;
}
return Real_gethostbyname(name);
}
INT WSAAPI Patched_getaddrinfo(PCSTR pNodeName, PCSTR pServiceName, const ADDRINFOA *pHints, PADDRINFOA *ppResult) {
if (pNodeName != NULL && ServerNeedsPatching(pNodeName)) {
pNodeName = MESSENGERSERVICE;
}
return Real_getaddrinfo(pNodeName, pServiceName, pHints, ppResult);
}
VOID SetFallbackService() {
int size = strlen(MESSENGERSERVICE_DEFAULT) + 1;
MESSENGERSERVICE = (char*)calloc(size, sizeof(char));
strcpy_s(MESSENGERSERVICE, size, MESSENGERSERVICE_DEFAULT);
}
VOID LoadURL() {
HKEY key;
LSTATUS status = RegCreateKeyExA(HKEY_CURRENT_USER, "SOFTWARE\\Computernewb\\MSNRedirector", 0, NULL, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &key, NULL);
if (status != ERROR_SUCCESS) {
// Fall back
char c[128];
sprintf_s(c, "Opening MSNRedirector registry key failed with system error %d\nFalling back to default", status);
MessageBoxA(NULL, c, "Computernewb MSNRedirector", MB_OK | MB_ICONERROR);
SetFallbackService();
return;
}
DWORD type;
DWORD size;
// Get size
status = RegQueryValueExA(key, "Server", NULL, &type, NULL, &size);
if (status == ERROR_FILE_NOT_FOUND) {
SetFallbackService();
RegSetValueExA(key, "Server", 0, REG_SZ, (byte*)MESSENGERSERVICE, strlen(MESSENGERSERVICE));
RegCloseKey(key);
return;
}
if (status != ERROR_SUCCESS) {
char c[128];
sprintf_s(c, "Reading MSNRedirector Server from registry failed with system error %d\nFalling back to default", status);
MessageBoxA(NULL, c, "Computernewb MSNRedirector", MB_OK | MB_ICONERROR);
RegCloseKey(key);
SetFallbackService();
return;
}
if (type != REG_SZ) {
MessageBoxA(NULL, "Reading MSNRedirector Server from registry failed because the value is not a string.\nFalling back to default", "Computernewb MSNRedirector", MB_OK | MB_ICONERROR);
RegCloseKey(key);
SetFallbackService();
return;
}
// Read key
byte* data = (byte*)malloc(size);
status = RegQueryValueExA(key, "Server", NULL, NULL, data, &size);
if (status != ERROR_SUCCESS) {
char c[128];
sprintf_s(c, "Reading MSNRedirector Server from registry failed with system error %d\nFalling back to default", status);
MessageBoxA(NULL, c, "Computernewb MSNRedirector", MB_OK | MB_ICONERROR);
free(data);
RegCloseKey(key);
SetFallbackService();
return;
}
RegCloseKey(key);
MESSENGERSERVICE = (char*)data;
return;
}
BOOL APIENTRY DllMain( HMODULE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved
)
{
switch (ul_reason_for_call)
{
case DLL_PROCESS_ATTACH:
{
LoadURL();
DetourTransactionBegin();
DetourUpdateThread(GetCurrentThread());
DetourAttach(&(PVOID&)Real_InternetConnectA, Patched_InternetConnectA);
DetourAttach(&(PVOID&)Real_InternetConnectW, Patched_InternetConnectW);
// we dont need this yet
//DetourAttach(&(PVOID&)Real_gethostbyname, Patched_gethostbyname);
DetourAttach(&(PVOID&)Real_getaddrinfo, Patched_getaddrinfo);
DetourTransactionCommit();
break;
}
case DLL_PROCESS_DETACH: {
DetourTransactionBegin();
DetourUpdateThread(GetCurrentThread());
DetourDetach(&(PVOID&)Real_InternetConnectA, Patched_InternetConnectA);
DetourDetach(&(PVOID&)Real_InternetConnectW, Patched_InternetConnectW);
//DetourDetach(&(PVOID&)Real_gethostbyname, Patched_gethostbyname);
DetourDetach(&(PVOID&)Real_getaddrinfo, Patched_getaddrinfo);
DetourTransactionCommit();
break;
}
}
return TRUE;
}
__declspec(dllexport) void WINAPI ImportThis() {}