/* * Connections and datagrams: a destination that is the marker address * or outside the loopback network goes to 127.0.0.1: instead; when that * is impossible (no port configured, IPv6 destination) the call fails as unreachable. * * ConnectEx is not an export: programs fetch it with WSAIoctl(SIO_GET_EXTENSION_FUNCTION_POINTER), * so that call hands out a wrapper around the real one. */ #include "hooks.h" #include "hook_config.h" #include "redirect_rules.h" #include typedef int (WSAAPI *connect_fn)(SOCKET, const struct sockaddr *, int); typedef int (WSAAPI *WSAConnect_fn)(SOCKET, const struct sockaddr *, int, LPWSABUF, LPWSABUF, LPQOS, LPQOS); typedef int (WSAAPI *sendto_fn)(SOCKET, const char *, int, int, const struct sockaddr *, int); typedef int (WSAAPI *WSASendTo_fn)(SOCKET, LPWSABUF, DWORD, LPDWORD, DWORD, const struct sockaddr *, int, LPWSAOVERLAPPED, LPWSAOVERLAPPED_COMPLETION_ROUTINE); typedef int (WSAAPI *WSAIoctl_fn)(SOCKET, DWORD, LPVOID, DWORD, LPVOID, DWORD, LPDWORD, LPWSAOVERLAPPED, LPWSAOVERLAPPED_COMPLETION_ROUTINE); static connect_fn real_connect; static WSAConnect_fn real_WSAConnect; static sendto_fn real_sendto; static WSASendTo_fn real_WSASendTo; static WSAIoctl_fn real_WSAIoctl; static LPFN_CONNECTEX real_ConnectEx; /* * The destination to use: the original one, or the local server. Returns 0 when the call * must fail (WSAENETUNREACH is set). Connections are logged; datagrams only when refused or * the first time a destination is seen would be too noisy to track, so only refusals. */ static int choose_destination(const struct sockaddr **name, int *length, struct sockaddr_storage *buffer, const char *what, int log_change) { char original[64]; char replaced[64]; int new_length = 0; enum redirect_result result = redirect_destination(*name, *length, buffer, &new_length); if (result == REDIRECT_KEEP) return 1; describe_address(*name, *length, original, sizeof(original)); if (result == REDIRECT_REFUSE) { log_line("%s %s refused", what, original); WSASetLastError(WSAENETUNREACH); return 0; } if (log_change) { describe_address((const struct sockaddr *)buffer, new_length, replaced, sizeof(replaced)); log_line("%s %s -> %s", what, original, replaced); } *name = (const struct sockaddr *)buffer; *length = new_length; return 1; } static int WSAAPI hooked_connect(SOCKET s, const struct sockaddr *name, int length) { struct sockaddr_storage buffer; if (!choose_destination(&name, &length, &buffer, "connect", 1)) return SOCKET_ERROR; return real_connect(s, name, length); } static int WSAAPI hooked_WSAConnect(SOCKET s, const struct sockaddr *name, int length, LPWSABUF caller_data, LPWSABUF callee_data, LPQOS sqos, LPQOS gqos) { struct sockaddr_storage buffer; if (!choose_destination(&name, &length, &buffer, "WSAConnect", 1)) return SOCKET_ERROR; return real_WSAConnect(s, name, length, caller_data, callee_data, sqos, gqos); } static BOOL PASCAL hooked_ConnectEx(SOCKET s, const struct sockaddr *name, int length, PVOID send_buffer, DWORD send_length, LPDWORD sent, LPOVERLAPPED overlapped) { struct sockaddr_storage buffer; if (!choose_destination(&name, &length, &buffer, "ConnectEx", 1)) return FALSE; return real_ConnectEx(s, name, length, send_buffer, send_length, sent, overlapped); } static int WSAAPI hooked_sendto(SOCKET s, const char *data, int size, int flags, const struct sockaddr *to, int length) { struct sockaddr_storage buffer; if (!choose_destination(&to, &length, &buffer, "sendto", 0)) return SOCKET_ERROR; return real_sendto(s, data, size, flags, to, length); } static int WSAAPI hooked_WSASendTo(SOCKET s, LPWSABUF buffers, DWORD count, LPDWORD sent, DWORD flags, const struct sockaddr *to, int length, LPWSAOVERLAPPED overlapped, LPWSAOVERLAPPED_COMPLETION_ROUTINE completion) { struct sockaddr_storage buffer; if (!choose_destination(&to, &length, &buffer, "WSASendTo", 0)) return SOCKET_ERROR; return real_WSASendTo(s, buffers, count, sent, flags, to, length, overlapped, completion); } static int WSAAPI hooked_WSAIoctl(SOCKET s, DWORD code, LPVOID in, DWORD in_size, LPVOID out, DWORD out_size, LPDWORD returned, LPWSAOVERLAPPED overlapped, LPWSAOVERLAPPED_COMPLETION_ROUTINE completion) { static const GUID connect_ex_id = WSAID_CONNECTEX; int result = real_WSAIoctl(s, code, in, in_size, out, out_size, returned, overlapped, completion); if (result == 0 && code == SIO_GET_EXTENSION_FUNCTION_POINTER && in && in_size >= sizeof(GUID) && memcmp(in, &connect_ex_id, sizeof(GUID)) == 0 && out && out_size >= sizeof(LPFN_CONNECTEX)) { LPFN_CONNECTEX handed_out = *(LPFN_CONNECTEX *)out; if (handed_out != hooked_ConnectEx) { real_ConnectEx = handed_out; *(LPFN_CONNECTEX *)out = hooked_ConnectEx; } } return result; } int install_connection_hooks(void) { return hook_export("connect", (void *)hooked_connect, (void **)&real_connect) && hook_export("WSAConnect", (void *)hooked_WSAConnect, (void **)&real_WSAConnect) && hook_export("sendto", (void *)hooked_sendto, (void **)&real_sendto) && hook_export("WSASendTo", (void *)hooked_WSASendTo, (void **)&real_WSASendTo) && hook_export("WSAIoctl", (void *)hooked_WSAIoctl, (void **)&real_WSAIoctl); }