diff options
author | Jason A. Donenfeld <Jason@zx2c4.com> | 2020-10-31 15:09:47 +0100 |
---|---|---|
committer | Simon Rozman <simon@rozman.si> | 2020-10-31 19:11:56 +0100 |
commit | 6c40f24498d448f3c5b52530835f367306cb0ff7 (patch) | |
tree | 3191d14a860e3134734123708df2fda4f4a8b2a3 /api | |
parent | api: add missing header for rundll32 mode (diff) | |
download | wintun-6c40f24498d448f3c5b52530835f367306cb0ff7.tar.xz wintun-6c40f24498d448f3c5b52530835f367306cb0ff7.zip |
api: add debugging rundll32 entry point
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
Diffstat (limited to 'api')
-rw-r--r-- | api/api.vcxproj | 8 | ||||
-rw-r--r-- | api/api.vcxproj.filters | 3 | ||||
-rw-r--r-- | api/rundll32.c | 66 | ||||
-rw-r--r-- | api/session.c | 1 | ||||
-rw-r--r-- | api/session.h | 54 |
5 files changed, 117 insertions, 15 deletions
diff --git a/api/api.vcxproj b/api/api.vcxproj index b93990d..af28a41 100644 --- a/api/api.vcxproj +++ b/api/api.vcxproj @@ -100,6 +100,11 @@ <CodeAnalysisRuleSet>NativeRecommendedRules.ruleset</CodeAnalysisRuleSet> <RunCodeAnalysis>true</RunCodeAnalysis> </PropertyGroup> + <PropertyGroup Condition="'$(Configuration)'=='Debug'" Label="Configuration"> + <LocalDebuggerCommand>rundll32.exe</LocalDebuggerCommand> + <LocalDebuggerCommandArguments>$(OutDir)$(TargetName)$(TargetExt),DoThingsForDebugging</LocalDebuggerCommandArguments> + <DebuggerFlavor>WindowsLocalDebugger</DebuggerFlavor> + </PropertyGroup> <ItemDefinitionGroup> <ClCompile> <PreprocessorDefinitions>_WINDOWS;_USRDLL;%(PreprocessorDefinitions)</PreprocessorDefinitions> @@ -164,6 +169,7 @@ <ClInclude Include="ntldr.h" /> <ClInclude Include="registry.h" /> <ClInclude Include="resource.h" /> + <ClInclude Include="session.h" /> <ClInclude Include="wintun.h" /> </ItemGroup> <ItemGroup> @@ -194,4 +200,4 @@ <Target Name="CleanSignTarget"> <Delete Files="$(IntermediateOutputPath)$(TargetName).sign" /> </Target> -</Project>
\ No newline at end of file +</Project> diff --git a/api/api.vcxproj.filters b/api/api.vcxproj.filters index c1db346..7d29920 100644 --- a/api/api.vcxproj.filters +++ b/api/api.vcxproj.filters @@ -58,6 +58,9 @@ <ClInclude Include="entry.h"> <Filter>Header Files</Filter> </ClInclude> + <ClInclude Include="session.h"> + <Filter>Header Files</Filter> + </ClInclude> </ItemGroup> <ItemGroup> <ClCompile Include="namespace.c"> diff --git a/api/rundll32.c b/api/rundll32.c index 2b2f11d..d594723 100644 --- a/api/rundll32.c +++ b/api/rundll32.c @@ -5,15 +5,23 @@ #include "adapter.h" #include "logger.h" +#include "session.h" #include "wintun.h" #include <Windows.h> #include <cfgmgr32.h> #include <objbase.h> +#include <assert.h> #define EXPORT comment(linker, "/EXPORT:" __FUNCTION__ "=" __FUNCDNAME__) -#ifdef ACCEPT_WOW64 +#if defined(ACCEPT_WOW64) || defined(_DEBUG) + +static +#ifndef _DEBUG +const +#endif +BOOL WriteToConsole = FALSE; static DWORD WriteFormatted(_In_ DWORD StdHandle, _In_z_ const WCHAR *Template, ...) @@ -22,19 +30,18 @@ WriteFormatted(_In_ DWORD StdHandle, _In_z_ const WCHAR *Template, ...) DWORD SizeWritten; va_list Arguments; va_start(Arguments, Template); - WriteFile( - GetStdHandle(StdHandle), - FormattedMessage, - sizeof(WCHAR) * FormatMessageW( - FORMAT_MESSAGE_FROM_STRING | FORMAT_MESSAGE_ALLOCATE_BUFFER, - Template, - 0, - 0, - (void *)&FormattedMessage, - 0, - &Arguments), - &SizeWritten, - NULL); + DWORD Len = sizeof(WCHAR) * FormatMessageW( + FORMAT_MESSAGE_FROM_STRING | FORMAT_MESSAGE_ALLOCATE_BUFFER, + Template, + 0, + 0, + (void *)&FormattedMessage, + 0, + &Arguments); + if (WriteToConsole) + WriteConsoleW(GetStdHandle(StdHandle), FormattedMessage, Len, &SizeWritten, NULL); + else + WriteFile(GetStdHandle(StdHandle), FormattedMessage, Len, &SizeWritten, NULL); LocalFree(FormattedMessage); va_end(Arguments); return SizeWritten / sizeof(WCHAR); @@ -141,4 +148,35 @@ cleanup: Done(); } +#ifdef _DEBUG +VOID __stdcall DoThingsForDebugging(HWND hwnd, HINSTANCE hinst, LPSTR lpszCmdLine, int nCmdShow) +{ +# pragma EXPORT + UNREFERENCED_PARAMETER(hwnd); + UNREFERENCED_PARAMETER(hinst); + UNREFERENCED_PARAMETER(lpszCmdLine); + UNREFERENCED_PARAMETER(nCmdShow); + + AllocConsole(); + WriteToConsole = TRUE; + Init(); + GUID TestGuid = { + 0xdeadbabe, + 0xcafe, + 0xbeef, + { 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef } + }; + WINTUN_ADAPTER *Adapter; + BOOL RebootRequired = FALSE; + assert(WintunCreateAdapter(L"Wintun", L"Test", &TestGuid, &Adapter, &RebootRequired) == ERROR_SUCCESS); + assert(!RebootRequired); + TUN_SESSION *Session; + HANDLE ReadWait; + assert(WintunStartSession(Adapter, WINTUN_MIN_RING_CAPACITY, &Session, &ReadWait) == ERROR_SUCCESS); + WintunEndSession(Session); + assert(WintunDeleteAdapter(Adapter, TRUE, &RebootRequired) == ERROR_SUCCESS); + assert(!RebootRequired); +} +#endif + #endif diff --git a/api/session.c b/api/session.c index 67772f0..940a9ff 100644 --- a/api/session.c +++ b/api/session.c @@ -7,6 +7,7 @@ #include "elevate.h" #include "entry.h" #include "logger.h" +#include "session.h" #include "wintun.h" #include <Windows.h> diff --git a/api/session.h b/api/session.h new file mode 100644 index 0000000..735b927 --- /dev/null +++ b/api/session.h @@ -0,0 +1,54 @@ +/* SPDX-License-Identifier: GPL-2.0 + * + * Copyright (C) 2018-2020 WireGuard LLC. All Rights Reserved. + */ + +#pragma once + +#include "wintun.h" +#include <Windows.h> + +typedef struct _TUN_SESSION TUN_SESSION; + +/** + * @copydoc WINTUN_START_SESSION_FUNC + */ +WINTUN_STATUS WINAPI +WintunStartSession( + _In_ const WINTUN_ADAPTER *Adapter, + _In_ DWORD Capacity, + _Out_ TUN_SESSION **Session, + _Out_ HANDLE *ReadWait); + +/** + * @copydoc WINTUN_END_SESSION_FUNC + */ +void WINAPI +WintunEndSession(_In_ TUN_SESSION *Session); + +/** + * @copydoc WINTUN_RECEIVE_PACKET_FUNC + */ +WINTUN_STATUS WINAPI +WintunReceivePacket(_In_ TUN_SESSION *Session, _Out_bytecapcount_(*PacketSize) BYTE **Packet, _Out_ DWORD *PacketSize); + +/** + * @copydoc WINTUN_RECEIVE_RELEASE_FUNC + */ +void WINAPI +WintunReceiveRelease(_In_ TUN_SESSION *Session, _In_ const BYTE *Packet); + +/** + * @copydoc WINTUN_ALLOCATE_SEND_PACKET + */ +WINTUN_STATUS WINAPI +WintunAllocateSendPacket( + _In_ TUN_SESSION *Session, + _In_ DWORD PacketSize, + _Out_bytecapcount_(PacketSize) BYTE **Packet); + +/** + * @copydoc WINTUN_SEND_PACKET + */ +void WINAPI +WintunSendPacket(_In_ TUN_SESSION *Session, _In_ const BYTE *Packet); |