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