diff --git a/tunnels/server/reality/reality_server.c b/tunnels/server/reality/reality_server.c index 23e862f0..3d721382 100644 --- a/tunnels/server/reality/reality_server.c +++ b/tunnels/server/reality/reality_server.c @@ -28,7 +28,6 @@ typedef struct reality_server_state_s // settings uint8_t hashes[EVP_MAX_MD_SIZE]; char context_password[kSignPasswordLen]; - uint32_t max_delta_time; uint32_t counter_threshold; char *password; unsigned int password_length; @@ -318,7 +317,6 @@ tunnel_t *newRealityServer(node_instance_context_t *instance_info) return NULL; } getIntFromJsonObjectOrDefault((int *) &(state->counter_threshold), settings, "sniffing-counter", 7); - getIntFromJsonObjectOrDefault((int *) &(state->max_delta_time), settings, "max-delta-time", 10); state->password_length = (int) strlen(state->password); if (state->password_length < 3) diff --git a/ww/tundevice/tun_win32.c b/ww/tundevice/tun_win32.c index 6f3e1357..15ea5f84 100644 --- a/ww/tundevice/tun_win32.c +++ b/ww/tundevice/tun_win32.c @@ -1,38 +1,37 @@ -#include +#include "wintun.h" #include -#include +#include #include #include -#include -#include #include #include #include -#include "wintun.h" +#include +#include +#include -static WINTUN_CREATE_ADAPTER_FUNC *WintunCreateAdapter; -static WINTUN_CLOSE_ADAPTER_FUNC *WintunCloseAdapter; -static WINTUN_OPEN_ADAPTER_FUNC *WintunOpenAdapter; -static WINTUN_GET_ADAPTER_LUID_FUNC *WintunGetAdapterLUID; +static WINTUN_CREATE_ADAPTER_FUNC *WintunCreateAdapter; +static WINTUN_CLOSE_ADAPTER_FUNC *WintunCloseAdapter; +static WINTUN_OPEN_ADAPTER_FUNC *WintunOpenAdapter; +static WINTUN_GET_ADAPTER_LUID_FUNC *WintunGetAdapterLUID; static WINTUN_GET_RUNNING_DRIVER_VERSION_FUNC *WintunGetRunningDriverVersion; -static WINTUN_DELETE_DRIVER_FUNC *WintunDeleteDriver; -static WINTUN_SET_LOGGER_FUNC *WintunSetLogger; -static WINTUN_START_SESSION_FUNC *WintunStartSession; -static WINTUN_END_SESSION_FUNC *WintunEndSession; -static WINTUN_GET_READ_WAIT_EVENT_FUNC *WintunGetReadWaitEvent; -static WINTUN_RECEIVE_PACKET_FUNC *WintunReceivePacket; -static WINTUN_RELEASE_RECEIVE_PACKET_FUNC *WintunReleaseReceivePacket; -static WINTUN_ALLOCATE_SEND_PACKET_FUNC *WintunAllocateSendPacket; -static WINTUN_SEND_PACKET_FUNC *WintunSendPacket; +static WINTUN_DELETE_DRIVER_FUNC *WintunDeleteDriver; +static WINTUN_SET_LOGGER_FUNC *WintunSetLogger; +static WINTUN_START_SESSION_FUNC *WintunStartSession; +static WINTUN_END_SESSION_FUNC *WintunEndSession; +static WINTUN_GET_READ_WAIT_EVENT_FUNC *WintunGetReadWaitEvent; +static WINTUN_RECEIVE_PACKET_FUNC *WintunReceivePacket; +static WINTUN_RELEASE_RECEIVE_PACKET_FUNC *WintunReleaseReceivePacket; +static WINTUN_ALLOCATE_SEND_PACKET_FUNC *WintunAllocateSendPacket; +static WINTUN_SEND_PACKET_FUNC *WintunSendPacket; -static HMODULE -InitializeWintun(void) +static HMODULE InitializeWintun(void) { HMODULE Wintun = LoadLibraryExW(L"wintun.dll", NULL, LOAD_LIBRARY_SEARCH_APPLICATION_DIR | LOAD_LIBRARY_SEARCH_SYSTEM32); - if (!Wintun) + if (! Wintun) return NULL; -#define X(Name) ((*(FARPROC *)&Name = GetProcAddress(Wintun, #Name)) == NULL) +#define X(Name) ((*(FARPROC *) &Name = GetProcAddress(Wintun, #Name)) == NULL) if (X(WintunCreateAdapter) || X(WintunCloseAdapter) || X(WintunOpenAdapter) || X(WintunGetAdapterLUID) || X(WintunGetRunningDriverVersion) || X(WintunDeleteDriver) || X(WintunSetLogger) || X(WintunStartSession) || X(WintunEndSession) || X(WintunGetReadWaitEvent) || X(WintunReceivePacket) || X(WintunReleaseReceivePacket) || @@ -47,11 +46,10 @@ InitializeWintun(void) return Wintun; } -static void CALLBACK -ConsoleLogger(_In_ WINTUN_LOGGER_LEVEL Level, _In_ DWORD64 Timestamp, _In_z_ const WCHAR *LogLine) +static void CALLBACK ConsoleLogger(_In_ WINTUN_LOGGER_LEVEL Level, _In_ DWORD64 Timestamp, _In_z_ const WCHAR *LogLine) { SYSTEMTIME SystemTime; - FileTimeToSystemTime((FILETIME *)&Timestamp, &SystemTime); + FileTimeToSystemTime((FILETIME *) &Timestamp, &SystemTime); WCHAR LevelMarker; switch (Level) { @@ -67,18 +65,9 @@ ConsoleLogger(_In_ WINTUN_LOGGER_LEVEL Level, _In_ DWORD64 Timestamp, _In_z_ con default: return; } - fwprintf( - stderr, - L"%04u-%02u-%02u %02u:%02u:%02u.%04u [%c] %s\n", - SystemTime.wYear, - SystemTime.wMonth, - SystemTime.wDay, - SystemTime.wHour, - SystemTime.wMinute, - SystemTime.wSecond, - SystemTime.wMilliseconds, - LevelMarker, - LogLine); + fwprintf(stderr, L"%04u-%02u-%02u %02u:%02u:%02u.%04u [%c] %s\n", SystemTime.wYear, SystemTime.wMonth, + SystemTime.wDay, SystemTime.wHour, SystemTime.wMinute, SystemTime.wSecond, SystemTime.wMilliseconds, + LevelMarker, LogLine); } static DWORD64 Now(VOID) @@ -88,27 +77,16 @@ static DWORD64 Now(VOID) return Timestamp.QuadPart; } -static DWORD -LogError(_In_z_ const WCHAR *Prefix, _In_ DWORD Error) +static DWORD LogError(_In_z_ const WCHAR *Prefix, _In_ DWORD Error) { WCHAR *SystemMessage = NULL, *FormattedMessage = NULL; - FormatMessageW( - FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_MAX_WIDTH_MASK, - NULL, - HRESULT_FROM_SETUPAPI(Error), - MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), - (void *)&SystemMessage, - 0, - NULL); - FormatMessageW( - FORMAT_MESSAGE_FROM_STRING | FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_ARGUMENT_ARRAY | - FORMAT_MESSAGE_MAX_WIDTH_MASK, - SystemMessage ? L"%1: %3(Code 0x%2!08X!)" : L"%1: Code 0x%2!08X!", - 0, - 0, - (void *)&FormattedMessage, - 0, - (va_list *)(DWORD_PTR[]){ (DWORD_PTR)Prefix, (DWORD_PTR)Error, (DWORD_PTR)SystemMessage }); + FormatMessageW(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_MAX_WIDTH_MASK, NULL, + HRESULT_FROM_SETUPAPI(Error), MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (void *) &SystemMessage, 0, + NULL); + FormatMessageW(FORMAT_MESSAGE_FROM_STRING | FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_ARGUMENT_ARRAY | + FORMAT_MESSAGE_MAX_WIDTH_MASK, + SystemMessage ? L"%1: %3(Code 0x%2!08X!)" : L"%1: Code 0x%2!08X!", 0, 0, (void *) &FormattedMessage, + 0, (va_list *) (DWORD_PTR[]) {(DWORD_PTR) Prefix, (DWORD_PTR) Error, (DWORD_PTR) SystemMessage}); if (FormattedMessage) ConsoleLogger(WINTUN_LOG_ERR, Now(), FormattedMessage); LocalFree(FormattedMessage); @@ -116,8 +94,7 @@ LogError(_In_z_ const WCHAR *Prefix, _In_ DWORD Error) return Error; } -static DWORD -LogLastError(_In_z_ const WCHAR *Prefix) +static DWORD LogLastError(_In_z_ const WCHAR *Prefix) { DWORD LastError = GetLastError(); LogError(Prefix, LastError); @@ -125,10 +102,9 @@ LogLastError(_In_z_ const WCHAR *Prefix) return LastError; } -static void -Log(_In_ WINTUN_LOGGER_LEVEL Level, _In_z_ const WCHAR *Format, ...) +static void Log(_In_ WINTUN_LOGGER_LEVEL Level, _In_z_ const WCHAR *Format, ...) { - WCHAR LogLine[0x200]; + WCHAR LogLine[0x200]; va_list args; va_start(args, Format); _vsnwprintf_s(LogLine, _countof(LogLine), _TRUNCATE, Format, args); @@ -136,11 +112,10 @@ Log(_In_ WINTUN_LOGGER_LEVEL Level, _In_z_ const WCHAR *Format, ...) ConsoleLogger(Level, Now(), LogLine); } -static HANDLE QuitEvent; +static HANDLE QuitEvent; static volatile BOOL HaveQuit; -static BOOL WINAPI -CtrlHandler(_In_ DWORD CtrlType) +static BOOL WINAPI CtrlHandler(_In_ DWORD CtrlType) { switch (CtrlType) { @@ -157,20 +132,19 @@ CtrlHandler(_In_ DWORD CtrlType) return FALSE; } -static void -PrintPacket(_In_ const BYTE *Packet, _In_ DWORD PacketSize) +static void PrintPacket(_In_ const BYTE *Packet, _In_ DWORD PacketSize) { if (PacketSize < 20) { Log(WINTUN_LOG_INFO, L"Received packet without room for an IP header"); return; } - BYTE IpVersion = Packet[0] >> 4, Proto; + BYTE IpVersion = Packet[0] >> 4, Proto; WCHAR Src[46], Dst[46]; if (IpVersion == 4) { - RtlIpv4AddressToStringW((struct in_addr *)&Packet[12], Src); - RtlIpv4AddressToStringW((struct in_addr *)&Packet[16], Dst); + RtlIpv4AddressToStringW((struct in_addr *) &Packet[12], Src); + RtlIpv4AddressToStringW((struct in_addr *) &Packet[16], Dst); Proto = Packet[9]; Packet += 20, PacketSize -= 20; } @@ -181,8 +155,8 @@ PrintPacket(_In_ const BYTE *Packet, _In_ DWORD PacketSize) } else if (IpVersion == 6) { - RtlIpv6AddressToStringW((struct in6_addr *)&Packet[8], Src); - RtlIpv6AddressToStringW((struct in6_addr *)&Packet[24], Dst); + RtlIpv6AddressToStringW((struct in6_addr *) &Packet[8], Src); + RtlIpv6AddressToStringW((struct in6_addr *) &Packet[24], Dst); Proto = Packet[6]; Packet += 40, PacketSize -= 40; } @@ -197,42 +171,39 @@ PrintPacket(_In_ const BYTE *Packet, _In_ DWORD PacketSize) Log(WINTUN_LOG_INFO, L"Received IPv%d proto 0x%x packet from %s to %s", IpVersion, Proto, Src, Dst); } -static USHORT -IPChecksum(_In_reads_bytes_(Len) BYTE *Buffer, _In_ DWORD Len) +static USHORT IPChecksum(_In_reads_bytes_(Len) BYTE *Buffer, _In_ DWORD Len) { ULONG Sum = 0; for (; Len > 1; Len -= 2, Buffer += 2) - Sum += *(USHORT *)Buffer; + Sum += *(USHORT *) Buffer; if (Len) Sum += *Buffer; Sum = (Sum >> 16) + (Sum & 0xffff); Sum += (Sum >> 16); - return (USHORT)(~Sum); + return (USHORT) (~Sum); } -static void -MakeICMP(_Out_writes_bytes_all_(28) BYTE Packet[28]) +static void MakeICMP(_Out_writes_bytes_all_(28) BYTE Packet[28]) { memset(Packet, 0, 28); - Packet[0] = 0x45; - *(USHORT *)&Packet[2] = htons(28); - Packet[8] = 255; - Packet[9] = 1; - *(ULONG *)&Packet[12] = htonl((10 << 24) | (6 << 16) | (7 << 8) | (8 << 0)); /* 10.6.7.8 */ - *(ULONG *)&Packet[16] = htonl((10 << 24) | (6 << 16) | (7 << 8) | (7 << 0)); /* 10.6.7.7 */ - *(USHORT *)&Packet[10] = IPChecksum(Packet, 20); - Packet[20] = 8; - *(USHORT *)&Packet[22] = IPChecksum(&Packet[20], 8); + Packet[0] = 0x45; + *(USHORT *) &Packet[2] = htons(28); + Packet[8] = 255; + Packet[9] = 1; + *(ULONG *) &Packet[12] = htonl((10 << 24) | (6 << 16) | (7 << 8) | (8 << 0)); /* 10.6.7.8 */ + *(ULONG *) &Packet[16] = htonl((10 << 24) | (6 << 16) | (7 << 8) | (7 << 0)); /* 10.6.7.7 */ + *(USHORT *) &Packet[10] = IPChecksum(Packet, 20); + Packet[20] = 8; + *(USHORT *) &Packet[22] = IPChecksum(&Packet[20], 8); Log(WINTUN_LOG_INFO, L"Sending IPv4 ICMP echo request to 10.6.7.8 from 10.6.7.7"); } -static DWORD WINAPI -ReceivePackets(_Inout_ DWORD_PTR SessionPtr) +static DWORD WINAPI ReceivePackets(_Inout_ DWORD_PTR SessionPtr) { - WINTUN_SESSION_HANDLE Session = (WINTUN_SESSION_HANDLE)SessionPtr; - HANDLE WaitHandles[] = { WintunGetReadWaitEvent(Session), QuitEvent }; + WINTUN_SESSION_HANDLE Session = (WINTUN_SESSION_HANDLE) SessionPtr; + HANDLE WaitHandles[] = {WintunGetReadWaitEvent(Session), QuitEvent}; - while (!HaveQuit) + while (! HaveQuit) { DWORD PacketSize; BYTE *Packet = WintunReceivePacket(Session, &PacketSize); @@ -259,11 +230,10 @@ ReceivePackets(_Inout_ DWORD_PTR SessionPtr) return ERROR_SUCCESS; } -static DWORD WINAPI -SendPackets(_Inout_ DWORD_PTR SessionPtr) +static DWORD WINAPI SendPackets(_Inout_ DWORD_PTR SessionPtr) { - WINTUN_SESSION_HANDLE Session = (WINTUN_SESSION_HANDLE)SessionPtr; - while (!HaveQuit) + WINTUN_SESSION_HANDLE Session = (WINTUN_SESSION_HANDLE) SessionPtr; + while (! HaveQuit) { BYTE *Packet = WintunAllocateSendPacket(Session, 28); if (Packet) @@ -287,28 +257,28 @@ SendPackets(_Inout_ DWORD_PTR SessionPtr) int __cdecl main(void) { HMODULE Wintun = InitializeWintun(); - if (!Wintun) + if (! Wintun) return LogError(L"Failed to initialize Wintun", GetLastError()); WintunSetLogger(ConsoleLogger); Log(WINTUN_LOG_INFO, L"Wintun library loaded"); DWORD LastError; - HaveQuit = FALSE; + HaveQuit = FALSE; QuitEvent = CreateEventW(NULL, TRUE, FALSE, NULL); - if (!QuitEvent) + if (! QuitEvent) { LastError = LogError(L"Failed to create event", GetLastError()); goto cleanupWintun; } - if (!SetConsoleCtrlHandler(CtrlHandler, TRUE)) + if (! SetConsoleCtrlHandler(CtrlHandler, TRUE)) { LastError = LogError(L"Failed to set console handler", GetLastError()); goto cleanupQuit; } - GUID ExampleGuid = { 0xdeadbabe, 0xcafe, 0xbeef, { 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef } }; - WINTUN_ADAPTER_HANDLE Adapter = WintunCreateAdapter(L"Demo", L"Example", &ExampleGuid); - if (!Adapter) + GUID ExampleGuid = {0xdeadbabe, 0xcafe, 0xbeef, {0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef}}; + WINTUN_ADAPTER_HANDLE Adapter = WintunCreateAdapter(L"Demo", L"Example", &ExampleGuid); + if (! Adapter) { LastError = GetLastError(); LogError(L"Failed to create adapter", LastError); @@ -321,11 +291,11 @@ int __cdecl main(void) MIB_UNICASTIPADDRESS_ROW AddressRow; InitializeUnicastIpAddressEntry(&AddressRow); WintunGetAdapterLUID(Adapter, &AddressRow.InterfaceLuid); - AddressRow.Address.Ipv4.sin_family = AF_INET; + AddressRow.Address.Ipv4.sin_family = AF_INET; AddressRow.Address.Ipv4.sin_addr.S_un.S_addr = htonl((10 << 24) | (6 << 16) | (7 << 8) | (7 << 0)); /* 10.6.7.7 */ - AddressRow.OnLinkPrefixLength = 24; /* This is a /24 network */ - AddressRow.DadState = IpDadStatePreferred; - LastError = CreateUnicastIpAddressEntry(&AddressRow); + AddressRow.OnLinkPrefixLength = 24; /* This is a /24 network */ + AddressRow.DadState = IpDadStatePreferred; + LastError = CreateUnicastIpAddressEntry(&AddressRow); if (LastError != ERROR_SUCCESS && LastError != ERROR_OBJECT_ALREADY_EXISTS) { LogError(L"Failed to set IP address", LastError); @@ -333,7 +303,7 @@ int __cdecl main(void) } WINTUN_SESSION_HANDLE Session = WintunStartSession(Adapter, 0x400000); - if (!Session) + if (! Session) { LastError = LogLastError(L"Failed to create adapter"); goto cleanupAdapter; @@ -341,9 +311,9 @@ int __cdecl main(void) Log(WINTUN_LOG_INFO, L"Launching threads and mangling packets..."); - HANDLE Workers[] = { CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)ReceivePackets, (LPVOID)Session, 0, NULL), - CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)SendPackets, (LPVOID)Session, 0, NULL) }; - if (!Workers[0] || !Workers[1]) + HANDLE Workers[] = {CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE) ReceivePackets, (LPVOID) Session, 0, NULL), + CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE) SendPackets, (LPVOID) Session, 0, NULL)}; + if (! Workers[0] || ! Workers[1]) { LastError = LogError(L"Failed to create threads", GetLastError()); goto cleanupWorkers; diff --git a/ww/utils/utils.c b/ww/utils/utils.c index 233e7045..a948cdba 100644 --- a/ww/utils/utils.c +++ b/ww/utils/utils.c @@ -71,6 +71,7 @@ char *concat(const char *s1, const char *s2) strcat(result, s2); return result; } + void toUpperCase(char *str) { int i = 0; @@ -80,6 +81,7 @@ void toUpperCase(char *str) i++; } } + void toLowerCase(char *str) { int i = 0; @@ -152,6 +154,7 @@ bool getStringFromJson(char **dest, const cJSON *json_str_node) } return false; } + bool getStringFromJsonObject(char **dest, const cJSON *json_obj, const char *key) { @@ -193,6 +196,7 @@ bool sockAddrCmpIPV4(const sockaddr_u *restrict addr1, const sockaddr_u *restric { return (addr1->sin.sin_addr.s_addr == addr2->sin.sin_addr.s_addr); } + bool sockAddrCmpIPV6(const sockaddr_u *restrict addr1, const sockaddr_u *restrict addr2) { int r = memcmp(addr1->sin6.sin6_addr.s6_addr, addr2->sin6.sin6_addr.s6_addr, sizeof(addr1->sin6.sin6_addr.s6_addr)); @@ -319,6 +323,7 @@ void socketContextDomainSet(socket_context_t *restrict scontext, const char *res scontext->domain[len] = 0x0; scontext->domain_len = len; } + void socketContextDomainSetConstMem(socket_context_t *restrict scontext, const char *restrict domain, uint8_t len) { if (scontext->domain != NULL && ! scontext->domain_constant) @@ -330,6 +335,7 @@ void socketContextDomainSetConstMem(socket_context_t *restrict scontext, const c scontext->domain_len = len; assert(scontext->domain[len] == 0x0); } + hash_t sockAddrCalcHashNoPort(const sockaddr_u *saddr) { hash_t result; @@ -515,6 +521,7 @@ dynamic_value_t parseDynamicStrValueFromJsonObject(const cJSON *json_obj, const } return result; } + dynamic_value_t parseDynamicNumericValueFromJsonObject(const cJSON *json_obj, const char *key, size_t matchers, ...) {