sysopfb

ramscraper

Aug 29th, 2018
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.82 KB | None | 0 0
  1. from time import sleep
  2. from ctypes import *
  3.  
  4. ### Setting the necessary vars and structs
  5. LPVOID = c_void_p
  6. HANDLE = LPVOID
  7. DWORD = c_uint32
  8. WORD = c_uint16
  9. UINT = c_uint
  10. INVALID_HANDLE_VALUE = c_void_p(-1).value
  11. LONG = c_long
  12.  
  13. TOKEN_ADJUST_PRIVILEGES = 0x00000020
  14. TOKEN_QUERY = 0x0008
  15.  
  16. SE_PRIVILEGE_ENABLED = 0x00000002
  17.  
  18. PROCESS_VM_READ = 0x0010
  19. PROCESS_VM_OPERATION = 0x0008
  20. PROCESS_QUERY_INFORMATION = 0x0400
  21.  
  22. MEM_PRIVATE = 0x20000
  23. MEM_COMMIT = 0x1000
  24.  
  25. PAGE_EXECUTE_READ = 0x20
  26. PAGE_EXECUTE_READWRITE = 0x40
  27. PAGE_READWRITE = 0x04
  28.  
  29. TH32CS_SNAPPROCESS = 0x00000002
  30.  
  31. class LUID(Structure):
  32.     _fields_ = [
  33.         ("LowPart",     DWORD),
  34.         ("HighPart",    LONG),
  35.     ]
  36.  
  37. class LUID_AND_ATTRIBUTES(Structure):
  38.     _fields_ = [
  39.         ("Luid",        LUID),
  40.         ("Attributes",  DWORD),
  41.     ]
  42.  
  43. class TOKEN_PRIVILEGES(Structure):
  44.     _fields_ = [
  45.         ("PrivilegeCount",  DWORD),
  46.         ("Privileges",      LUID_AND_ATTRIBUTES),
  47.     ]
  48.  
  49. class MEMORY_BASIC_INFORMATION(Structure):
  50.     _fields_ = [
  51.         ("BaseAddress", c_void_p),
  52.         ("AllocationBase", c_void_p),
  53.         ("AllocationProtect", DWORD),
  54.         ("RegionSize", UINT),
  55.         ("State", DWORD),
  56.         ("Protect", DWORD),
  57.         ("Type", DWORD)
  58.         ]
  59.  
  60. class PROCESSENTRY32(Structure):
  61.      _fields_ = [("dwSize", c_ulong),
  62.                  ("cntUsage", c_ulong),
  63.                  ("th32ProcessID", c_ulong),
  64.                  ("th32DefaultHeapID", c_ulong),
  65.                  ("th32ModuleID", c_ulong),
  66.                  ("cntThreads", c_ulong),
  67.                  ("th32ParentProcessID", c_ulong),
  68.                  ("pcPriClassBase", c_ulong),
  69.                  ("dwFlags", c_ulong),
  70.                  ("szExeFile", c_char * 260)]
  71.  
  72. def EnablePrivilege(privilegeStr, hToken = None):
  73.     """Enable Privilege on token, if no token is given the function gets the token of the current process."""
  74.     if hToken == None:
  75.         TOKEN_ADJUST_PRIVILEGES = 0x00000020
  76.         TOKEN_QUERY = 0x0008
  77.         hToken = HANDLE(INVALID_HANDLE_VALUE)
  78.         windll.advapi32.OpenProcessToken(windll.kernel32.GetCurrentProcess(), (TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY), byref(hToken) )
  79.  
  80.     privilege_id = LUID()
  81.     windll.advapi32.LookupPrivilegeValueA(None, privilegeStr, byref(privilege_id))
  82.  
  83.     SE_PRIVILEGE_ENABLED = 0x00000002
  84.     laa = LUID_AND_ATTRIBUTES(privilege_id, SE_PRIVILEGE_ENABLED)
  85.     tp  = TOKEN_PRIVILEGES(1, laa)
  86.  
  87.     windll.advapi32.AdjustTokenPrivileges(hToken, False, byref(tp), sizeof(tp), None, None)
  88.  
  89. def check_buffer( buffer ):
  90.     """Test function to test the buffer, this function could contain anything.
  91.       You could easily do something with regular expressions."""
  92.     blob_hdr = "08\x02\x00\x00\x10\x66\x00\x00\x20\x00\x00\x00"
  93.     if blob_hdr in buffer:
  94.         return buffer.index( blob_hdr )
  95.  
  96.     return "Not Found"
  97.  
  98. def Processis64( hProcess ):
  99.     """From MSDN:
  100.        [Returns] a value that is set to TRUE if the process is running under WOW64.
  101.        If the process is running under 32-bit Windows, the value is set to FALSE.
  102.        If the process is a 64-bit application running under 64-bit Windows, the value is also set to FALSE"""
  103.  
  104.     pis64 = c_bool()
  105.     windll.kernel32.IsWow64Process(hProcess, byref( pis64 ) )
  106.  
  107.     return pis64.value
  108.  
  109. def scan_memory( ):
  110.     """Scan the memory of every process except some predefined processes."""
  111.     print "START SCANNING"
  112.     readlimit = 100*4096
  113.  
  114.     skip = ("svchost.exe", "iexplore.exe", "explorer.exe", "System",
  115.             "smss.exe", "csrss.exe", "winlogon.exe", "lsass.exe",
  116.             "spoolsv.exe", "alg.exe", "wuauclt.exe", "wininit.exe",
  117.             "services.exe", "lsm.exe", "audiodg.exe", "dllhost.exe",
  118.             "conhost.exe", "igfxsrvc.exe", "SearchFilterHost.exe",
  119.             "SearchFilterHost.exe", "wmpnetwk.exe", "SearchIndexer.exe",
  120.             "SearchProtocolHost.exe", "WUDFHost.exe", "dwm.exe", "LogonUI.exe")
  121.  
  122.     hSnap = windll.kernel32.CreateToolhelp32Snapshot( TH32CS_SNAPPROCESS, 0 )
  123.  
  124.     pe32 = PROCESSENTRY32()
  125.     pe32.dwSize = sizeof( PROCESSENTRY32 )
  126.  
  127.     ## The first pid i == 0 (System)
  128.     windll.kernel32.Process32First( hSnap, byref( pe32 ) )
  129.  
  130.     ownpid= windll.kernel32.GetCurrentProcessId()
  131.     print "[+]PID current process: " + str( ownpid )
  132.  
  133.     print "[+]Scanning processes"
  134.     while True:
  135.         if windll.kernel32.Process32Next( hSnap, byref( pe32 ) ) == 0:
  136.             break
  137.  
  138.         name = pe32.szExeFile
  139.         pid = pe32.th32ProcessID
  140.  
  141.         if not name in skip and pid != ownpid:
  142.             print "\t[+]Name: " + str( name ) + " | PID: " + str( pid )
  143.             ## Open the process
  144.             hProcess = windll.kernel32.OpenProcess( PROCESS_VM_READ | PROCESS_VM_OPERATION | PROCESS_QUERY_INFORMATION, 0, pid )
  145.  
  146.             addr =  c_long(0)
  147.  
  148.             while True:
  149.                 MBI = MEMORY_BASIC_INFORMATION()
  150.                 windll.kernel32.VirtualQueryEx( hProcess, addr.value, byref( MBI ), sizeof( MBI ) )
  151.  
  152.                 ## If the VirtualQueryEx call returns nothing, the max address has been reached, break
  153.                 ## If the program is run in 32bit mode and scans a 64bit process it cant read some addresses so check AllocationBase if the address is readable.
  154.                 if ( addr.value != 0 and MBI.BaseAddress == None ) or (MBI.AllocationBase == None and not Processis64( hProcess ) ):
  155.                     break
  156.  
  157.                 ## The new addr that will be scanned
  158.                 addr.value += MBI.RegionSize
  159.  
  160.                 if MBI.Type == MEM_PRIVATE and MBI.State == MEM_COMMIT and MBI.Protect in ( PAGE_EXECUTE_READ, PAGE_EXECUTE_READWRITE, PAGE_READWRITE ):
  161.                     #print "\t\tFound good region: " + str( MBI.BaseAddress )
  162.                     ReadAddr = 0
  163.                     while MBI.RegionSize > 0:
  164.  
  165.                         if ReadAddr != 0:
  166.                             ReadAddr += readlimit
  167.  
  168.                         else:
  169.                             ReadAddr = MBI.BaseAddress
  170.  
  171.                         if MBI.RegionSize > readlimit:
  172.                             BuffSize = readlimit
  173.                             MBI.RegionSize -= readlimit
  174.  
  175.                         else:
  176.                             BuffSize = MBI.RegionSize
  177.                             MBI.RegionSize = 0
  178.  
  179.                         Buff = create_string_buffer( BuffSize )
  180.                         windll.kernel32.ReadProcessMemory( hProcess, ReadAddr, Buff, BuffSize, 0 )
  181.  
  182.                         found = check_buffer( Buff.raw )
  183.                         if found != "Not Found":
  184.                             print "\t\t[!]Found at address: " + str( ReadAddr + found )
  185.  
  186.             windll.kernel32.CloseHandle( hProcess )
  187.  
  188.     windll.kernel32.CloseHandle( hSnap )
  189.  
  190. if __name__ == "__main__":
  191.     EnablePrivilege( 'SeDebugPrivilege' )
  192.     scan_memory()
Advertisement
Add Comment
Please, Sign In to add comment