Cleanup and test scripts

This commit is contained in:
Dave Parsons 2015-01-31 18:56:43 +00:00
parent 98ee837ce6
commit b9bbe599e2

View File

@ -1,316 +1,320 @@
""" """
The MIT License (MIT) The MIT License (MIT)
Copyright (c) 2014 Dave Parsons Copyright (c) 2014 Dave Parsons
Permission is hereby granted, free of charge, to any person obtaining a copy Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the 'Software'), to deal of this software and associated documentation files (the 'Software'), to deal
in the Software without restriction, including without limitation the rights in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions: furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software. all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 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 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE. THE SOFTWARE.
vSMC Header Structure vSMC Header Structure
Offset Length struct Type Description Offset Length struct Type Description
---------------------------------------- ----------------------------------------
0x00/00 0x08/08 Q ptr Offset to key table 0x00/00 0x08/08 Q ptr Offset to key table
0x08/08 0x04/4 I int Number of private keys 0x08/08 0x04/4 I int Number of private keys
0x0C/12 0x04/4 I int Number of public keys 0x0C/12 0x04/4 I int Number of public keys
vSMC Key Data Structure vSMC Key Data Structure
Offset Length struct Type Description Offset Length struct Type Description
---------------------------------------- ----------------------------------------
0x00/00 0x04/04 4s int Key name (byte reversed e.g. #KEY is YEK#) 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 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) 0x05/05 0x04/04 4s int Data type (byte reversed e.g. ui32 is 23iu)
0x09/09 0x01/01 B byte Flag R/W 0x09/09 0x01/01 B byte Flag R/W
0x0A/10 0x06/06 6x byte Padding 0x0A/10 0x06/06 6x byte Padding
0x10/16 0x08/08 Q ptr Internal VMware routine 0x10/16 0x08/08 Q ptr Internal VMware routine
0x18/24 0x30/48 48B byte Data 0x18/24 0x30/48 48B byte Data
""" """
import os import os
import sys import sys
import struct import struct
# Setup imports depending on whether IronPython or CPython if sys.version_info < (2, 7):
if sys.platform == 'win32' \ sys.stderr.write('You need Python 2.7 or later\n')
or sys.platform == 'cli': sys.exit(1)
from _winreg import *
# Setup imports depending on whether IronPython or CPython
if sys.platform == 'win32' \
def rot13(s): or sys.platform == 'cli':
chars = 'AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz' from _winreg import *
trans = chars[26:] + chars[:26]
rot_char = lambda c: trans[chars.find(c)] if chars.find(c) > -1 else c
return ''.join(rot_char(c) for c in s) def rot13(s):
chars = 'AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz'
trans = chars[26:] + chars[:26]
def bytetohex(bytestr): rot_char = lambda c: trans[chars.find(c)] if chars.find(c) > -1 else c
return ''.join(['%02X ' % ord(x) for x in bytestr]).strip() return ''.join(rot_char(c) for c in s)
def printkey(i, smc_key, smc_data): def bytetohex(bytestr):
print str(i+1).zfill(3) \ return ''.join(['%02X ' % ord(x) for x in bytestr]).strip()
+ ' ' + smc_key[0][::-1] \
+ ' ' + str(smc_key[1]).zfill(2) \
+ ' ' + smc_key[2][::-1].replace('\x00', ' ') \ def printkey(i, smc_key, smc_data):
+ ' ' + '{0:#0{1}x}'.format(smc_key[3], 4) \ print str(i+1).zfill(3) \
+ ' ' + hex(smc_key[4]) \ + ' ' + smc_key[0][::-1] \
+ ' ' + bytetohex(smc_data) + ' ' + str(smc_key[1]).zfill(2) \
+ ' ' + smc_key[2][::-1].replace('\x00', ' ') \
+ ' ' + '{0:#0{1}x}'.format(smc_key[3], 4) \
def patchkeys(f, key): + ' ' + hex(smc_key[4]) \
# Setup struct pack string + ' ' + bytetohex(smc_data)
global smc_new_memptr
key_pack = '=4sB4sB6xQ'
def patchkeys(f, key):
# Do Until OSK1 read # Setup struct pack string
i = 0 global smc_new_memptr
while True: key_pack = '=4sB4sB6xQ'
# Read key into struct str and data byte str # Do Until OSK1 read
offset = key + (i * 72) i = 0
f.seek(offset) while True:
smc_key = struct.unpack(key_pack, f.read(24))
smc_data = f.read(smc_key[1]) # Read key into struct str and data byte str
offset = key + (i * 72)
# Reset pointer to beginning of key entry f.seek(offset)
f.seek(offset) smc_key = struct.unpack(key_pack, f.read(24))
smc_data = f.read(smc_key[1])
if smc_key[0] == 'SKL+':
# Use the +LKS data routine for OSK0/1 # Reset pointer to beginning of key entry
smc_new_memptr = smc_key[4] f.seek(offset)
print '+LKS Key: '
printkey(i, smc_key, smc_data) if smc_key[0] == 'SKL+':
# Use the +LKS data routine for OSK0/1
elif smc_key[0] == '0KSO': smc_new_memptr = smc_key[4]
# Write new data routine pointer from +LKS print '+LKS Key: '
print 'OSK0 Key Before:' printkey(i, smc_key, smc_data)
printkey(i, smc_key, smc_data)
f.seek(offset) elif smc_key[0] == '0KSO':
f.write(struct.pack(key_pack, smc_key[0], smc_key[1], smc_key[2], smc_key[3], smc_new_memptr)) # Write new data routine pointer from +LKS
f.flush() print 'OSK0 Key Before:'
printkey(i, smc_key, smc_data)
# Write new data for key f.seek(offset)
f.seek(offset + 24) f.write(struct.pack(key_pack, smc_key[0], smc_key[1], smc_key[2], smc_key[3], smc_new_memptr))
smc_new_data = rot13('bheuneqjbexolgurfrjbeqfthneqrqcy') f.flush()
f.write(smc_new_data)
f.flush() # Write new data for key
f.seek(offset + 24)
# Re-read and print key smc_new_data = rot13('bheuneqjbexolgurfrjbeqfthneqrqcy')
f.seek(offset) f.write(smc_new_data)
smc_key = struct.unpack(key_pack, f.read(24)) f.flush()
smc_data = f.read(smc_key[1])
print 'OSK0 Key After:' # Re-read and print key
printkey(i, smc_key, smc_data) f.seek(offset)
smc_key = struct.unpack(key_pack, f.read(24))
elif smc_key[0] == '1KSO': smc_data = f.read(smc_key[1])
# Write new data routine pointer from +LKS print 'OSK0 Key After:'
print 'OSK1 Key Before:' printkey(i, smc_key, smc_data)
printkey(i, smc_key, smc_data)
f.seek(offset) elif smc_key[0] == '1KSO':
f.write(struct.pack(key_pack, smc_key[0], smc_key[1], smc_key[2], smc_key[3], smc_new_memptr)) # Write new data routine pointer from +LKS
f.flush() print 'OSK1 Key Before:'
printkey(i, smc_key, smc_data)
# Write new data for key f.seek(offset)
f.seek(offset + 24) f.write(struct.pack(key_pack, smc_key[0], smc_key[1], smc_key[2], smc_key[3], smc_new_memptr))
smc_new_data = rot13('rnfrqbagfgrny(p)NccyrPbzchgreVap') f.flush()
f.write(smc_new_data)
f.flush() # Write new data for key
f.seek(offset + 24)
# Re-read and print key smc_new_data = rot13('rnfrqbagfgrny(p)NccyrPbzchgreVap')
f.seek(offset) f.write(smc_new_data)
smc_key = struct.unpack(key_pack, f.read(24)) f.flush()
smc_data = f.read(smc_key[1])
print 'OSK1 Key After:' # Re-read and print key
printkey(i, smc_key, smc_data) f.seek(offset)
smc_key = struct.unpack(key_pack, f.read(24))
# Finished so get out of loop smc_data = f.read(smc_key[1])
break print 'OSK1 Key After:'
printkey(i, smc_key, smc_data)
else:
pass # Finished so get out of loop
break
i += 1
else:
pass
def patchsmc(name):
with open(name, 'r+b') as f: i += 1
# Read file into string variable
vmx = f.read() def patchsmc(name):
with open(name, 'r+b') as f:
print 'File: ' + name
# Read file into string variable
# Setup hex string for vSMC headers vmx = f.read()
# These are the private and public key counts
smc_header_v0 = '\xF2\x00\x00\x00\xF0\x00\x00\x00' print 'File: ' + name
smc_header_v1 = '\xB4\x01\x00\x00\xB0\x01\x00\x00'
# Setup hex string for vSMC headers
# Setup hex string for #KEY key # These are the private and public key counts
key_key = '\x59\x45\x4B\x23\x04\x32\x33\x69\x75' smc_header_v0 = '\xF2\x00\x00\x00\xF0\x00\x00\x00'
smc_header_v1 = '\xB4\x01\x00\x00\xB0\x01\x00\x00'
# Setup hex string for $Adr key
adr_key = '\x72\x64\x41\x24\x04\x32\x33\x69\x75' # Setup hex string for #KEY key
key_key = '\x59\x45\x4B\x23\x04\x32\x33\x69\x75'
# Find the vSMC headers
smc_header_v0_offset = vmx.find(smc_header_v0) - 8 # Setup hex string for $Adr key
smc_header_v1_offset = vmx.find(smc_header_v1) - 8 adr_key = '\x72\x64\x41\x24\x04\x32\x33\x69\x75'
# Find '#KEY' keys # Find the vSMC headers
smc_key0 = vmx.find(key_key) smc_header_v0_offset = vmx.find(smc_header_v0) - 8
smc_key1 = vmx.rfind(key_key) smc_header_v1_offset = vmx.find(smc_header_v1) - 8
# Find '$Adr' key only V1 table # Find '#KEY' keys
smc_adr = vmx.find(adr_key) smc_key0 = vmx.find(key_key)
smc_key1 = vmx.rfind(key_key)
# Print vSMC0 tables and keys
print 'applesmctablev0 (smc.version = "0")' # Find '$Adr' key only V1 table
print 'applesmctablev0 Address : ' + hex(smc_header_v0_offset) smc_adr = vmx.find(adr_key)
print 'applesmctablev0 Private Key #: 0xF2/242'
print 'applesmctablev0 Public Key #: 0xF0/240' # Print vSMC0 tables and keys
print 'appleSMCTableV0 (smc.version = "0")'
if (smc_adr - smc_key0) != 72: print 'appleSMCTableV0 Address : ' + hex(smc_header_v0_offset)
print 'applesmctablev0 Table : ' + hex(smc_key0) print 'appleSMCTableV0 Private Key #: 0xF2/242'
patchkeys(f, smc_key0) print 'appleSMCTableV0 Public Key #: 0xF0/240'
elif (smc_adr - smc_key1) != 72:
print 'applesmctablev0 Table : ' + hex(smc_key1) if (smc_adr - smc_key0) != 72:
patchkeys(f, smc_key1) print 'appleSMCTableV0 Table : ' + hex(smc_key0)
patchkeys(f, smc_key0)
print elif (smc_adr - smc_key1) != 72:
print 'appleSMCTableV0 Table : ' + hex(smc_key1)
# Print vSMC1 tables and keys patchkeys(f, smc_key1)
print 'applesmctablev1 (smc.version = "1")'
print 'applesmctablev1 Address : ' + hex(smc_header_v1_offset) print
print 'applesmctablev1 Private Key #: 0x01B4/436'
print 'applesmctablev1 Public Key #: 0x01B0/432' # Print vSMC1 tables and keys
print 'appleSMCTableV1 (smc.version = "1")'
if (smc_adr - smc_key0) == 72: print 'appleSMCTableV1 Address : ' + hex(smc_header_v1_offset)
print 'applesmctablev1 Table : ' + hex(smc_key0) print 'appleSMCTableV1 Private Key #: 0x01B4/436'
patchkeys(f, smc_key0) print 'appleSMCTableV1 Public Key #: 0x01B0/432'
elif (smc_adr - smc_key1) == 72:
print 'applesmctablev1 Table : ' + hex(smc_key1) if (smc_adr - smc_key0) == 72:
patchkeys(f, smc_key1) print 'appleSMCTableV1 Table : ' + hex(smc_key0)
patchkeys(f, smc_key0)
print elif (smc_adr - smc_key1) == 72:
print 'appleSMCTableV1 Table : ' + hex(smc_key1)
# Tidy up patchkeys(f, smc_key1)
f.flush()
f.close() print
# Tidy up
def patchbase(name): f.flush()
# Patch file f.close()
print 'GOS Patching: ' + name
f = open(name, 'r+b')
def patchbase(name):
# Entry to search for in GOS table # Patch file
darwin = ( print 'GOS Patching: ' + name
'\x10\x00\x00\x00\x10\x00\x00\x00' f = open(name, 'r+b')
'\x02\x00\x00\x00\x00\x00\x00\x00'
'\x00\x00\x00\x00\x00\x00\x00\x00' # Entry to search for in GOS table
'\x00\x00\x00\x00\x00\x00\x00\x00' darwin = (
'\xBE' '\x10\x00\x00\x00\x10\x00\x00\x00'
) '\x02\x00\x00\x00\x00\x00\x00\x00'
'\x00\x00\x00\x00\x00\x00\x00\x00'
# Read file into string variable '\x00\x00\x00\x00\x00\x00\x00\x00'
base = f.read() '\xBE'
)
# Loop thorugh each entry and set top bit
# 0xBE --> 0xBF # Read file into string variable
offset = 0 base = f.read()
while offset < len(base):
offset = base.find(darwin, offset) # Loop thorugh each entry and set top bit
if offset == -1: # 0xBE --> 0xBF
break offset = 0
f.seek(offset + 32) while offset < len(base):
flag = f.read(1) offset = base.find(darwin, offset)
if flag == '\xBE': if offset == -1:
f.seek(offset + 32) break
f.write('\xBF') f.seek(offset + 32)
print 'GOS Patched flag @: ' + hex(offset) flag = f.read(1)
else: if flag == '\xBE':
print 'GOS Unknown flag @: {0}/{1}'.format(hex(offset), hex(flag)) f.seek(offset + 32)
f.write('\xBF')
offset += 33 print 'GOS Patched flag @: ' + hex(offset)
else:
# # Tidy up print 'GOS Unknown flag @: ' + hex(offset) + '/' + hex(int(flag))
f.flush()
f.close() offset += 33
print 'GOS Patched: ' + name
# # Tidy up
f.flush()
def main(): f.close()
print 'GOS Patched: ' + name
# Work around absent Platform module on VMkernel
if os.name == 'nt' or os.name == 'cli':
osname = 'windows' def main():
else:
osname = os.uname()[0].lower() # Work around absent Platform module on VMkernel
if os.name == 'nt' or os.name == 'cli':
# Setup default paths osname = 'windows'
if osname == 'darwin': else:
vmx_path = '/Applications/VMware Fusion.app/Contents/Library/' osname = os.uname()[0].lower()
vmx = vmx_path + 'vmware-vmx'
vmx_debug = vmx_path + 'vmware-vmx-debug' # Setup default paths
vmx_stats = vmx_path + 'vmware-vmx-stats' if osname == 'darwin':
vmwarebase = '' vmx_path = '/Applications/VMware Fusion.app/Contents/Library/'
vmx = vmx_path + 'vmware-vmx'
elif osname == 'linux': vmx_debug = vmx_path + 'vmware-vmx-debug'
vmx_path = '/usr/lib/vmware/bin/' vmx_stats = vmx_path + 'vmware-vmx-stats'
vmx = vmx_path + 'vmware-vmx' vmwarebase = ''
vmx_debug = vmx_path + 'vmware-vmx-debug'
vmx_stats = vmx_path + 'vmware-vmx-stats' elif osname == 'linux':
vmwarebase = '/usr/lib/vmware/lib/libvmwarebase.so.0/libvmwarebase.so.0' vmx_path = '/usr/lib/vmware/bin/'
vmx = vmx_path + 'vmware-vmx'
elif osname == 'vmkernel': vmx_debug = vmx_path + 'vmware-vmx-debug'
vmx_path = '/unlocker/' vmx_stats = vmx_path + 'vmware-vmx-stats'
vmx = vmx_path + 'vmx' vmwarebase = '/usr/lib/vmware/lib/libvmwarebase.so.0/libvmwarebase.so.0'
vmx_debug = vmx_path + 'vmx-debug'
vmx_stats = vmx_path + 'vmx-stats' elif osname == 'vmkernel':
vmwarebase = '' vmx_path = '/unlocker/'
vmx = vmx_path + 'vmx'
elif osname == 'windows': vmx_debug = vmx_path + 'vmx-debug'
reg = ConnectRegistry(None, HKEY_LOCAL_MACHINE) vmx_stats = vmx_path + 'vmx-stats'
key = OpenKey(reg, r'SOFTWARE\Wow6432Node\VMware, Inc.\VMware Workstation') vmwarebase = ''
vmwarebase_path = QueryValueEx(key, 'InstallPath')[0]
vmx_path = QueryValueEx(key, 'InstallPath64')[0] elif osname == 'windows':
vmx = vmx_path + 'vmware-vmx.exe' reg = ConnectRegistry(None, HKEY_LOCAL_MACHINE)
vmx_debug = vmx_path + 'vmware-vmx-debug.exe' key = OpenKey(reg, r'SOFTWARE\Wow6432Node\VMware, Inc.\VMware Workstation')
vmx_stats = vmx_path + 'vmware-vmx-stats.exe' vmwarebase_path = QueryValueEx(key, 'InstallPath')[0]
vmwarebase = vmwarebase_path + 'vmwarebase.dll' vmx_path = QueryValueEx(key, 'InstallPath64')[0]
vmx = vmx_path + 'vmware-vmx.exe'
else: vmx_debug = vmx_path + 'vmware-vmx-debug.exe'
print('Unknown Operating System: ' + osname) vmx_stats = vmx_path + 'vmware-vmx-stats.exe'
return vmwarebase = vmwarebase_path + 'vmwarebase.dll'
# Patch the vmx executables skipping stats version for Player else:
patchsmc(vmx) print('Unknown Operating System: ' + osname)
patchsmc(vmx_debug) return
try:
patchsmc(vmx_stats) # Patch the vmx executables skipping stats version for Player
except IOError: patchsmc(vmx)
pass patchsmc(vmx_debug)
try:
# Patch vmwarebase for Workstation and Player patchsmc(vmx_stats)
# Not required on Fusion or ESXi as table already has correct flags except IOError:
if vmwarebase != '': pass
patchbase(vmwarebase)
else: # Patch vmwarebase for Workstation and Player
print 'Patching vmwarebase is not required on this system' # Not required on Fusion or ESXi as table already has correct flags
if vmwarebase != '':
patchbase(vmwarebase)
if __name__ == '__main__': else:
main() print 'Patching vmwarebase is not required on this system'
if __name__ == '__main__':
main()