├── .gitignore ├── LICENSE ├── README.md └── sudo.c /.gitignore: -------------------------------------------------------------------------------- 1 | # Prerequisites 2 | *.d 3 | 4 | # Object files 5 | *.o 6 | *.ko 7 | *.obj 8 | *.elf 9 | 10 | # Linker output 11 | *.ilk 12 | *.map 13 | *.exp 14 | 15 | # Precompiled Headers 16 | *.gch 17 | *.pch 18 | 19 | # Libraries 20 | *.lib 21 | *.a 22 | *.la 23 | *.lo 24 | 25 | # Shared objects (inc. Windows DLLs) 26 | *.dll 27 | *.so 28 | *.so.* 29 | *.dylib 30 | 31 | # Executables 32 | *.exe 33 | *.out 34 | *.app 35 | *.i*86 36 | *.x86_64 37 | *.hex 38 | 39 | # Debug files 40 | *.dSYM/ 41 | *.su 42 | *.idb 43 | *.pdb 44 | 45 | # Kernel Module Compile Results 46 | *.mod* 47 | *.cmd 48 | .tmp_versions/ 49 | modules.order 50 | Module.symvers 51 | Mkfile.old 52 | dkms.conf 53 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | BSD 3-Clause License 2 | 3 | Copyright (c) 2017, LH_Mouse 4 | All rights reserved. 5 | 6 | Redistribution and use in source and binary forms, with or without 7 | modification, are permitted provided that the following conditions are met: 8 | 9 | * Redistributions of source code must retain the above copyright notice, this 10 | list of conditions and the following disclaimer. 11 | 12 | * Redistributions in binary form must reproduce the above copyright notice, 13 | this list of conditions and the following disclaimer in the documentation 14 | and/or other materials provided with the distribution. 15 | 16 | * Neither the name of the copyright holder nor the names of its 17 | contributors may be used to endorse or promote products derived from 18 | this software without specific prior written permission. 19 | 20 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 21 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 23 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 24 | FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 | DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 26 | SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 27 | CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 28 | OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 29 | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # sudo-win 2 | 3 | A utility for running arbitrary commands with administrator access on Windows 4 | 5 | **Use the following command to compile:** 6 | 7 | ```sh 8 | gcc -std=c99 -Wall -Wextra -Werror -Wconversion -pedantic -pedantic-errors \ 9 | sudo.c -o sudo.exe -O3 -nostdlib -lshlwapi -lshell32 -lkernel32 \ 10 | -Wl,--entry,@SudoEntryPoint,--subsystem,windows,--strip-all 11 | ``` 12 | -------------------------------------------------------------------------------- /sudo.c: -------------------------------------------------------------------------------- 1 | /****************************************************************************** 2 | 3 | A utility for running arbitrary commands with administrator access on Windows 4 | 5 | Use the following command to compile: 6 | 7 | gcc -std=c99 -Wall -Wextra -Werror -Wconversion -pedantic -pedantic-errors \ 8 | sudo.c -o sudo.exe -O3 -nostdlib -lshlwapi -lshell32 -lkernel32 \ 9 | -Wl,--entry,@SudoEntryPoint,--subsystem,windows,--strip-all 10 | 11 | Copyright (c) 2017, LH_Mouse 12 | All rights reserved. 13 | 14 | Redistribution and use in source and binary forms, with or without 15 | modification, are permitted provided that the following conditions are met: 16 | 17 | * Redistributions of source code must retain the above copyright notice, this 18 | list of conditions and the following disclaimer. 19 | 20 | * Redistributions in binary form must reproduce the above copyright notice, 21 | this list of conditions and the following disclaimer in the documentation 22 | and/or other materials provided with the distribution. 23 | 24 | * Neither the name of the copyright holder nor the names of its 25 | contributors may be used to endorse or promote products derived from 26 | this software without specific prior written permission. 27 | 28 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 29 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 30 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 31 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 32 | FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 33 | DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 34 | SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 35 | CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 36 | OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 37 | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 38 | 39 | ******************************************************************************/ 40 | 41 | #include 42 | #include 43 | 44 | DWORD SudoEntryPoint(void *pUnknown) __asm__("@SudoEntryPoint"); 45 | 46 | static STARTUPINFOW vStartupInfo; 47 | static LPCWSTR pwszCmdLine; 48 | static WCHAR awchSomeBuffer[32768 + 256]; 49 | 50 | DWORD SudoEntryPoint(void *pUnknown){ 51 | LPWSTR pwszFile, pwszArgs; 52 | SHELLEXECUTEINFOW vShellExecInfo; 53 | DWORD dwExitCode; 54 | (void)pUnknown; 55 | 56 | /* Perform global initialization. */ 57 | GetStartupInfoW(&vStartupInfo); 58 | pwszCmdLine = GetCommandLineW(); 59 | 60 | /* Obtain the command and arguments to run. */ 61 | pwszFile = PathGetArgsW(pwszCmdLine); 62 | if(pwszFile[0] == 0){ 63 | /* Run CMD if no filename is given, passing the working directory 64 | to it. By default an elevated CMD has its working directory 65 | set to `%SystemRoot%\System32\`, which confuses end users. 66 | We tell CMD to switch to our working directory upon launch. */ 67 | pwszArgs = awchSomeBuffer; 68 | CopyMemory(pwszArgs, L"/s /k pushd \"", 13 * sizeof(WCHAR)); 69 | dwExitCode = GetCurrentDirectoryW(32768, pwszArgs + 13); 70 | CopyMemory(pwszArgs + 13 + dwExitCode, L"\"", 2 * sizeof(WCHAR)); 71 | pwszFile = L"CMD.EXE"; 72 | } else { 73 | /* Use the filename provided if any. */ 74 | pwszArgs = PathGetArgsW(pwszFile); 75 | /* Trim the filename, leaving the arguments alone. */ 76 | if(pwszArgs[0] != 0){ 77 | pwszArgs[-1] = 0; 78 | } 79 | StrTrimW(pwszFile, L" \t"); 80 | } 81 | /* Launch it with elevated access now. */ 82 | vShellExecInfo.cbSize = sizeof(vShellExecInfo); 83 | vShellExecInfo.fMask = SEE_MASK_NOCLOSEPROCESS | SEE_MASK_UNICODE; 84 | vShellExecInfo.hwnd = NULL; 85 | vShellExecInfo.lpVerb = L"runas"; 86 | vShellExecInfo.lpFile = pwszFile; 87 | vShellExecInfo.lpParameters = pwszArgs; 88 | vShellExecInfo.lpDirectory = NULL; 89 | vShellExecInfo.nShow = vStartupInfo.wShowWindow; 90 | if(!ShellExecuteExW(&vShellExecInfo)){ 91 | /* Return its error code in case of failure. */ 92 | dwExitCode = GetLastError(); 93 | } else if(!vShellExecInfo.hProcess){ 94 | /* Assume success, should no process handle be returned. */ 95 | dwExitCode = 0; 96 | } else { 97 | /* Retrieve the exit code of the process created. */ 98 | WaitForSingleObject(vShellExecInfo.hProcess, INFINITE); 99 | GetExitCodeProcess(vShellExecInfo.hProcess, &dwExitCode); 100 | CloseHandle(vShellExecInfo.hProcess); 101 | } 102 | /* Forward the exit code. */ 103 | ExitProcess(dwExitCode); 104 | } 105 | --------------------------------------------------------------------------------