Skip to content

Commit

Permalink
reformat
Browse files Browse the repository at this point in the history
  • Loading branch information
radkesvat committed Aug 6, 2024
1 parent 36bb61a commit a931e72
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 112 deletions.
2 changes: 0 additions & 2 deletions tunnels/server/reality/reality_server.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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)
Expand Down
190 changes: 80 additions & 110 deletions ww/tundevice/tun_win32.c
Original file line number Diff line number Diff line change
@@ -1,38 +1,37 @@
#include <winsock2.h>
#include "wintun.h"
#include <Windows.h>
#include <ws2ipdef.h>
#include <ip2string.h>
#include <iphlpapi.h>
#include <mstcpip.h>
#include <ip2string.h>
#include <winternl.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include "wintun.h"
#include <winsock2.h>
#include <winternl.h>
#include <ws2ipdef.h>

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) ||
Expand All @@ -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)
{
Expand All @@ -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)
Expand All @@ -88,59 +77,45 @@ 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);
LocalFree(SystemMessage);
return Error;
}

static DWORD
LogLastError(_In_z_ const WCHAR *Prefix)
static DWORD LogLastError(_In_z_ const WCHAR *Prefix)
{
DWORD LastError = GetLastError();
LogError(Prefix, LastError);
SetLastError(LastError);
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);
va_end(args);
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)
{
Expand All @@ -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;
}
Expand All @@ -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;
}
Expand All @@ -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);
Expand All @@ -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)
Expand All @@ -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);
Expand All @@ -321,29 +291,29 @@ 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);
goto cleanupAdapter;
}

WINTUN_SESSION_HANDLE Session = WintunStartSession(Adapter, 0x400000);
if (!Session)
if (! Session)
{
LastError = LogLastError(L"Failed to create adapter");
goto cleanupAdapter;
}

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;
Expand Down
Loading

0 comments on commit a931e72

Please sign in to comment.