unlocker/unlocker.py

402 lines
13 KiB
Python
Raw Permalink Normal View History

#!/usr/bin/env python
2015-01-31 19:56:43 +01:00
"""
The MIT License (MIT)
2018-09-29 10:21:28 +02:00
Copyright (c) 2014-2018 Dave Parsons & Sam Bingner
2015-01-31 19:56:43 +01:00
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the 'Software'), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
vSMC Header Structure
Offset Length Struct Type Description
2015-01-31 19:56:43 +01:00
----------------------------------------
0x00/00 0x08/08 Q ptr Offset to key table
0x08/08 0x04/4 I int Number of private keys
0x0C/12 0x04/4 I int Number of public keys
vSMC Key Data Structure
Offset Length Struct Type Description
2015-01-31 19:56:43 +01:00
----------------------------------------
0x00/00 0x04/04 4s int Key name (byte reversed e.g. #KEY is YEK#)
0x04/04 0x01/01 B byte Length of returned data
0x05/05 0x04/04 4s int Data type (byte reversed e.g. ui32 is 23iu)
0x09/09 0x01/01 B byte Flag R/W
0x0A/10 0x06/06 6x byte Padding
0x10/16 0x08/08 Q ptr Internal VMware routine
0x18/24 0x30/48 48B byte Data
"""
2016-12-15 11:59:03 +01:00
from __future__ import print_function
2017-09-27 09:53:51 +02:00
import codecs
2015-01-31 19:56:43 +01:00
import os
import re
2015-01-31 19:56:43 +01:00
import struct
2017-12-07 16:31:16 +01:00
import sys
2015-01-31 19:56:43 +01:00
if sys.version_info < (2, 7):
sys.stderr.write('You need Python 2.7 or later\n')
sys.exit(1)
# Setup imports depending on whether IronPython or CPython
2015-10-14 20:09:10 +02:00
if sys.platform == 'win32' \
or sys.platform == 'cli':
2018-09-24 14:49:47 +02:00
# noinspection PyUnresolvedReferences
if sys.version_info > (3, 0):
from winreg import *
else:
from _winreg import *
2015-10-14 20:09:10 +02:00
2015-01-31 19:56:43 +01:00
2016-12-15 11:59:03 +01:00
def bytetohex(data):
if sys.version_info > (3, 0):
# Python 3 code in this block
return "".join("{:02X} ".format(c) for c in data)
else:
# Python 2 code in this block
return "".join("{:02X} ".format(ord(c)) for c in data)
2017-10-05 09:26:04 +02:00
def joinpath(folder, filename):
return os.path.join(folder, filename)
2015-01-31 19:56:43 +01:00
2015-06-16 13:45:15 +02:00
def printkey(i, offset, smc_key, smc_data):
2016-12-15 11:59:03 +01:00
print(str(i + 1).zfill(3)
+ ' ' + hex(offset)
+ ' ' + smc_key[0][::-1].decode('UTF-8')
+ ' ' + str(smc_key[1]).zfill(2)
+ ' ' + smc_key[2][::-1].replace(b'\x00', b' ').decode('UTF-8')
+ ' ' + '{0:#0{1}x}'.format(smc_key[3], 4)
+ ' ' + hex(smc_key[4])
+ ' ' + bytetohex(smc_data))
2017-10-05 09:26:04 +02:00
def set_bit(value, bit):
return value | (1 << bit)
def clear_bit(value, bit):
return value & ~(1 << bit)
def test_bit(value, bit):
return value & bit
2016-12-15 11:59:03 +01:00
E_CLASS64 = 2
E_SHT_RELA = 4
2015-10-10 07:36:51 +02:00
2016-07-17 14:33:26 +02:00
def patchelf(f, oldoffset, newoffset):
2015-06-16 13:45:15 +02:00
f.seek(0)
magic = f.read(4)
if not magic == b'\x7fELF':
raise Exception('Magic number does not match')
ei_class = struct.unpack('=B', f.read(1))[0]
if ei_class != E_CLASS64:
2015-10-10 07:36:51 +02:00
raise Exception('Not 64bit elf header: ' + ei_class)
2015-06-16 13:45:15 +02:00
f.seek(40)
e_shoff = struct.unpack('=Q', f.read(8))[0]
f.seek(58)
e_shentsize = struct.unpack('=H', f.read(2))[0]
e_shnum = struct.unpack('=H', f.read(2))[0]
e_shstrndx = struct.unpack('=H', f.read(2))[0]
2016-12-15 11:59:03 +01:00
print('e_shoff: 0x{:x} e_shentsize: 0x{:x} e_shnum:0x{:x} e_shstrndx:0x{:x}'.format(e_shoff, e_shentsize,
e_shnum, e_shstrndx))
2015-06-16 13:45:15 +02:00
for i in range(0, e_shnum):
f.seek(e_shoff + i * e_shentsize)
e_sh = struct.unpack('=LLQQQQLLQQ', f.read(e_shentsize))
2017-10-05 09:26:04 +02:00
# e_sh_name = e_sh[0]
2015-06-16 13:45:15 +02:00
e_sh_type = e_sh[1]
e_sh_offset = e_sh[4]
e_sh_size = e_sh[5]
e_sh_entsize = e_sh[9]
if e_sh_type == E_SHT_RELA:
2016-12-15 11:59:03 +01:00
e_sh_nument = int(e_sh_size / e_sh_entsize)
# print 'RELA at 0x{:x} with {:d} entries'.format(e_sh_offset, e_sh_nument)
2015-06-16 13:45:15 +02:00
for j in range(0, e_sh_nument):
f.seek(e_sh_offset + e_sh_entsize * j)
rela = struct.unpack('=QQq', f.read(e_sh_entsize))
r_offset = rela[0]
r_info = rela[1]
r_addend = rela[2]
2016-07-17 14:33:26 +02:00
if r_addend == oldoffset:
r_addend = newoffset
2015-06-16 13:45:15 +02:00
f.seek(e_sh_offset + e_sh_entsize * j)
f.write(struct.pack('=QQq', r_offset, r_info, r_addend))
2016-12-15 11:59:03 +01:00
print('Relocation modified at: ' + hex(e_sh_offset + e_sh_entsize * j))
2015-06-16 13:45:15 +02:00
2016-07-17 14:33:26 +02:00
def patchkeys(f, key):
2015-01-31 19:56:43 +01:00
# Setup struct pack string
key_pack = '=4sB4sB6xQ'
2017-10-05 09:26:04 +02:00
# smc_old_memptr = 0
2015-10-10 07:36:51 +02:00
smc_new_memptr = 0
2015-01-31 19:56:43 +01:00
# Do Until OSK1 read
i = 0
while True:
# Read key into struct str and data byte str
offset = key + (i * 72)
f.seek(offset)
smc_key = struct.unpack(key_pack, f.read(24))
smc_data = f.read(smc_key[1])
# Reset pointer to beginning of key entry
f.seek(offset)
2016-12-15 11:59:03 +01:00
if smc_key[0] == b'SKL+':
2015-01-31 19:56:43 +01:00
# Use the +LKS data routine for OSK0/1
smc_new_memptr = smc_key[4]
2016-12-15 11:59:03 +01:00
print('+LKS Key: ')
2015-06-16 13:45:15 +02:00
printkey(i, offset, smc_key, smc_data)
2015-01-31 19:56:43 +01:00
2016-12-15 11:59:03 +01:00
elif smc_key[0] == b'0KSO':
2015-01-31 19:56:43 +01:00
# Write new data routine pointer from +LKS
2016-12-15 11:59:03 +01:00
print('OSK0 Key Before:')
2015-06-16 13:45:15 +02:00
printkey(i, offset, smc_key, smc_data)
2017-10-05 09:26:04 +02:00
# smc_old_memptr = smc_key[4]
2015-01-31 19:56:43 +01:00
f.seek(offset)
f.write(struct.pack(key_pack, smc_key[0], smc_key[1], smc_key[2], smc_key[3], smc_new_memptr))
f.flush()
# Write new data for key
f.seek(offset + 24)
2017-09-27 09:53:51 +02:00
smc_new_data = codecs.encode('bheuneqjbexolgurfrjbeqfthneqrqcy', 'rot_13')
2016-12-15 11:59:03 +01:00
f.write(smc_new_data.encode('UTF-8'))
2015-01-31 19:56:43 +01:00
f.flush()
# Re-read and print key
f.seek(offset)
smc_key = struct.unpack(key_pack, f.read(24))
smc_data = f.read(smc_key[1])
2016-12-15 11:59:03 +01:00
print('OSK0 Key After:')
2015-06-16 13:45:15 +02:00
printkey(i, offset, smc_key, smc_data)
2015-01-31 19:56:43 +01:00
2016-12-15 11:59:03 +01:00
elif smc_key[0] == b'1KSO':
2015-01-31 19:56:43 +01:00
# Write new data routine pointer from +LKS
2016-12-15 11:59:03 +01:00
print('OSK1 Key Before:')
2015-06-16 13:45:15 +02:00
printkey(i, offset, smc_key, smc_data)
smc_old_memptr = smc_key[4]
2015-01-31 19:56:43 +01:00
f.seek(offset)
f.write(struct.pack(key_pack, smc_key[0], smc_key[1], smc_key[2], smc_key[3], smc_new_memptr))
f.flush()
# Write new data for key
f.seek(offset + 24)
2017-09-27 09:53:51 +02:00
smc_new_data = codecs.encode('rnfrqbagfgrny(p)NccyrPbzchgreVap', 'rot_13')
2016-12-15 11:59:03 +01:00
f.write(smc_new_data.encode('UTF-8'))
2015-01-31 19:56:43 +01:00
f.flush()
# Re-read and print key
f.seek(offset)
smc_key = struct.unpack(key_pack, f.read(24))
smc_data = f.read(smc_key[1])
2016-12-15 11:59:03 +01:00
print('OSK1 Key After:')
2015-06-16 13:45:15 +02:00
printkey(i, offset, smc_key, smc_data)
2015-01-31 19:56:43 +01:00
# Finished so get out of loop
break
else:
pass
i += 1
2015-06-16 13:45:15 +02:00
return smc_old_memptr, smc_new_memptr
2015-01-31 19:56:43 +01:00
2015-10-10 07:36:51 +02:00
2016-07-17 14:33:26 +02:00
def patchsmc(name, sharedobj):
2015-01-31 19:56:43 +01:00
with open(name, 'r+b') as f:
2015-10-10 07:36:51 +02:00
smc_old_memptr = 0
smc_new_memptr = 0
2015-01-31 19:56:43 +01:00
# Read file into string variable
vmx = f.read()
2018-08-19 17:56:28 +02:00
print('File: ' + name + '\n')
2015-01-31 19:56:43 +01:00
# Setup hex string for vSMC headers
# These are the private and public key counts
2016-12-15 11:59:03 +01:00
smc_header_v0 = b'\xF2\x00\x00\x00\xF0\x00\x00\x00'
smc_header_v1 = b'\xB4\x01\x00\x00\xB0\x01\x00\x00'
2015-01-31 19:56:43 +01:00
# Setup hex string for #KEY key
2016-12-15 11:59:03 +01:00
key_key = b'\x59\x45\x4B\x23\x04\x32\x33\x69\x75'
2015-01-31 19:56:43 +01:00
# Setup hex string for $Adr key
2016-12-15 11:59:03 +01:00
adr_key = b'\x72\x64\x41\x24\x04\x32\x33\x69\x75'
2015-01-31 19:56:43 +01:00
# Find the vSMC headers
smc_header_v0_offset = vmx.find(smc_header_v0) - 8
smc_header_v1_offset = vmx.find(smc_header_v1) - 8
# Find '#KEY' keys
smc_key0 = vmx.find(key_key)
smc_key1 = vmx.rfind(key_key)
# Find '$Adr' key only V1 table
smc_adr = vmx.find(adr_key)
# Print vSMC0 tables and keys
2016-12-15 11:59:03 +01:00
print('appleSMCTableV0 (smc.version = "0")')
print('appleSMCTableV0 Address : ' + hex(smc_header_v0_offset))
print('appleSMCTableV0 Private Key #: 0xF2/242')
print('appleSMCTableV0 Public Key #: 0xF0/240')
2015-01-31 19:56:43 +01:00
if (smc_adr - smc_key0) != 72:
2016-12-15 11:59:03 +01:00
print('appleSMCTableV0 Table : ' + hex(smc_key0))
2016-07-17 14:33:26 +02:00
smc_old_memptr, smc_new_memptr = patchkeys(f, smc_key0)
2015-01-31 19:56:43 +01:00
elif (smc_adr - smc_key1) != 72:
2016-12-15 11:59:03 +01:00
print('appleSMCTableV0 Table : ' + hex(smc_key1))
2016-07-17 14:33:26 +02:00
smc_old_memptr, smc_new_memptr = patchkeys(f, smc_key1)
2015-01-31 19:56:43 +01:00
2016-12-15 11:59:03 +01:00
print()
2015-01-31 19:56:43 +01:00
# Print vSMC1 tables and keys
2016-12-15 11:59:03 +01:00
print('appleSMCTableV1 (smc.version = "1")')
print('appleSMCTableV1 Address : ' + hex(smc_header_v1_offset))
print('appleSMCTableV1 Private Key #: 0x01B4/436')
print('appleSMCTableV1 Public Key #: 0x01B0/432')
2015-01-31 19:56:43 +01:00
if (smc_adr - smc_key0) == 72:
2016-12-15 11:59:03 +01:00
print('appleSMCTableV1 Table : ' + hex(smc_key0))
2016-07-17 14:33:26 +02:00
smc_old_memptr, smc_new_memptr = patchkeys(f, smc_key0)
2015-01-31 19:56:43 +01:00
elif (smc_adr - smc_key1) == 72:
2016-12-15 11:59:03 +01:00
print('appleSMCTableV1 Table : ' + hex(smc_key1))
2016-07-17 14:33:26 +02:00
smc_old_memptr, smc_new_memptr = patchkeys(f, smc_key1)
2015-01-31 19:56:43 +01:00
2016-12-15 11:59:03 +01:00
print()
2015-01-31 19:56:43 +01:00
2015-06-16 13:45:15 +02:00
# Find matching RELA record in .rela.dyn in ESXi ELF files
# This is temporary code until proper ELF parsing written
2015-09-16 18:09:59 +02:00
if sharedobj:
2016-12-15 11:59:03 +01:00
print('Modifying RELA records from: ' + hex(smc_old_memptr) + ' to ' + hex(smc_new_memptr))
2016-07-17 14:33:26 +02:00
patchelf(f, smc_old_memptr, smc_new_memptr)
2015-06-16 13:45:15 +02:00
2015-01-31 19:56:43 +01:00
# Tidy up
f.flush()
f.close()
def patchbase(name):
# Patch file
2016-12-15 11:59:03 +01:00
print('GOS Patching: ' + name)
2015-01-31 19:56:43 +01:00
f = open(name, 'r+b')
# Entry to search for in GOS table
# Should work for Workstation 12-15...
darwin = re.compile(
b'\x10\x00\x00\x00[\x10|\x20]\x00\x00\x00[\x01|\x02]\x00\x00\x00\x00\x00\x00\x00'
b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00')
2015-01-31 19:56:43 +01:00
# Read file into string variable
base = f.read()
2017-10-05 09:26:04 +02:00
# Loop through each entry and set top bit
# 0xBE --> 0xBF (WKS 12)
# 0x3E --> 0x3F (WKS 14)
for m in darwin.finditer(base):
offset = m.start()
2015-01-31 19:56:43 +01:00
f.seek(offset + 32)
2017-10-05 09:26:04 +02:00
flag = ord(f.read(1))
flag = set_bit(flag, 0)
# flag = chr(flag)
f.seek(offset + 32)
f.write(bytes([flag]))
2017-10-05 09:26:04 +02:00
print('GOS Patched flag @: ' + hex(offset))
2015-01-31 19:56:43 +01:00
2015-06-29 09:39:05 +02:00
# Tidy up
2015-01-31 19:56:43 +01:00
f.flush()
f.close()
2016-12-15 11:59:03 +01:00
print('GOS Patched: ' + name)
2015-01-31 19:56:43 +01:00
2015-06-29 09:39:05 +02:00
def patchvmkctl(name):
# Patch file
2016-12-15 11:59:03 +01:00
print('smcPresent Patching: ' + name)
2015-06-29 09:39:05 +02:00
f = open(name, 'r+b')
# Read file into string variable
vmkctl = f.read()
2016-12-15 11:59:03 +01:00
applesmc = vmkctl.find(b'applesmc')
2015-06-29 09:39:05 +02:00
f.seek(applesmc)
2016-12-15 11:59:03 +01:00
f.write(b'vmkernel')
2015-06-29 09:39:05 +02:00
# Tidy up
f.flush()
f.close()
2016-12-15 11:59:03 +01:00
print('smcPresent Patched: ' + name)
2015-06-29 09:39:05 +02:00
2015-01-31 19:56:43 +01:00
2018-09-24 14:49:47 +02:00
# noinspection PyUnresolvedReferences
2015-10-10 07:36:51 +02:00
def main():
2015-01-31 19:56:43 +01:00
# Work around absent Platform module on VMkernel
if os.name == 'nt' or os.name == 'cli':
osname = 'windows'
else:
osname = os.uname()[0].lower()
2018-09-24 14:49:47 +02:00
# vmwarebase = ''
2015-09-14 14:51:31 +02:00
vmx_so = False
2015-01-31 19:56:43 +01:00
# Setup default paths
2018-09-24 14:49:47 +02:00
if osname == 'linux':
2015-01-31 19:56:43 +01:00
vmx_path = '/usr/lib/vmware/bin/'
2016-12-15 11:59:03 +01:00
vmx = joinpath(vmx_path, 'vmware-vmx')
vmx_debug = joinpath(vmx_path, 'vmware-vmx-debug')
vmx_stats = joinpath(vmx_path, 'vmware-vmx-stats')
if os.path.isfile('/usr/lib/vmware/lib/libvmwarebase.so/libvmwarebase.so'):
2015-09-14 14:51:31 +02:00
vmx_so = True
vmwarebase = '/usr/lib/vmware/lib/libvmwarebase.so/libvmwarebase.so'
else:
vmwarebase = '/usr/lib/vmware/lib/libvmwarebase.so.0/libvmwarebase.so.0'
2015-01-31 19:56:43 +01:00
elif osname == 'windows':
reg = ConnectRegistry(None, HKEY_LOCAL_MACHINE)
key = OpenKey(reg, r'SOFTWARE\Wow6432Node\VMware, Inc.\VMware Workstation')
vmwarebase_path = QueryValueEx(key, 'InstallPath')[0]
vmx_path = QueryValueEx(key, 'InstallPath64')[0]
2016-12-15 11:59:03 +01:00
vmx = joinpath(vmx_path, 'vmware-vmx.exe')
vmx_debug = joinpath(vmx_path, 'vmware-vmx-debug.exe')
vmx_stats = joinpath(vmx_path, 'vmware-vmx-stats.exe')
vmwarebase = joinpath(vmwarebase_path, 'vmwarebase.dll')
2015-01-31 19:56:43 +01:00
else:
print('Unknown Operating System: ' + osname)
return
# Patch the vmx executables skipping stats version for Player
2016-07-17 14:33:26 +02:00
patchsmc(vmx, vmx_so)
patchsmc(vmx_debug, vmx_so)
2016-12-15 11:59:03 +01:00
if os.path.isfile(vmx_stats):
2016-07-17 14:33:26 +02:00
patchsmc(vmx_stats, vmx_so)
2015-01-31 19:56:43 +01:00
# Patch vmwarebase for Workstation and Player
2018-09-24 14:49:47 +02:00
patchbase(vmwarebase)
2015-06-29 09:39:05 +02:00
2015-01-31 19:56:43 +01:00
if __name__ == '__main__':
main()