Advertisement
Guest User

Untitled

a guest
Sep 24th, 2019
1,044
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 13.32 KB | None | 0 0
  1. #!/usr/bin/env python
  2. """
  3. The MIT License (MIT)
  4.  
  5. Copyright (c) 2014-2018 Dave Parsons & Sam Bingner
  6.  
  7. Permission is hereby granted, free of charge, to any person obtaining a copy
  8. of this software and associated documentation files (the 'Software'), to deal
  9. in the Software without restriction, including without limitation the rights
  10. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. copies of the Software, and to permit persons to whom the Software is
  12. furnished to do so, subject to the following conditions:
  13.  
  14. The above copyright notice and this permission notice shall be included in
  15. all copies or substantial portions of the Software.
  16.  
  17. THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  20. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  22. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  23. THE SOFTWARE.
  24.  
  25. vSMC Header Structure
  26. Offset  Length  Struct Type Description
  27. ----------------------------------------
  28. 0x00/00 0x08/08 Q      ptr  Offset to key table
  29. 0x08/08 0x04/4  I      int  Number of private keys
  30. 0x0C/12 0x04/4  I      int  Number of public keys
  31.  
  32. vSMC Key Data Structure
  33. Offset  Length  Struct Type Description
  34. ----------------------------------------
  35. 0x00/00 0x04/04 4s     int  Key name (byte reversed e.g. #KEY is YEK#)
  36. 0x04/04 0x01/01 B      byte Length of returned data
  37. 0x05/05 0x04/04 4s     int  Data type (byte reversed e.g. ui32 is 23iu)
  38. 0x09/09 0x01/01 B      byte Flag R/W
  39. 0x0A/10 0x06/06 6x     byte Padding
  40. 0x10/16 0x08/08 Q      ptr  Internal VMware routine
  41. 0x18/24 0x30/48 48B    byte Data
  42. """
  43.  
  44. from __future__ import print_function
  45. import codecs
  46. import os
  47. import re
  48. import struct
  49. import sys
  50.  
  51. if sys.version_info < (2, 7):
  52.     sys.stderr.write('You need Python 2.7 or later\n')
  53.     sys.exit(1)
  54.  
  55. # Setup imports depending on whether IronPython or CPython
  56. if sys.platform == 'win32' \
  57.         or sys.platform == 'cli':
  58.     # noinspection PyUnresolvedReferences
  59.     if sys.version_info > (3, 0):
  60.         from winreg import *
  61.     else:
  62.         from _winreg import *
  63.  
  64.  
  65.  
  66. def bytetohex(data):
  67.     if sys.version_info > (3, 0):
  68.         # Python 3 code in this block
  69.         return "".join("{:02X} ".format(c) for c in data)
  70.     else:
  71.         # Python 2 code in this block
  72.         return "".join("{:02X} ".format(ord(c)) for c in data)
  73.  
  74.  
  75. def joinpath(folder, filename):
  76.     return os.path.join(folder, filename)
  77.  
  78.  
  79. def printkey(i, offset, smc_key, smc_data):
  80.     print(str(i + 1).zfill(3)
  81.           + ' ' + hex(offset)
  82.           + ' ' + smc_key[0][::-1].decode('UTF-8')
  83.           + ' ' + str(smc_key[1]).zfill(2)
  84.           + ' ' + smc_key[2][::-1].replace(b'\x00', b' ').decode('UTF-8')
  85.           + ' ' + '{0:#0{1}x}'.format(smc_key[3], 4)
  86.           + ' ' + hex(smc_key[4])
  87.           + ' ' + bytetohex(smc_data))
  88.  
  89.  
  90. def set_bit(value, bit):
  91.     return value | (1 << bit)
  92.  
  93.  
  94. def clear_bit(value, bit):
  95.     return value & ~(1 << bit)
  96.  
  97.  
  98. def test_bit(value, bit):
  99.     return value & bit
  100.  
  101.  
  102. E_CLASS64 = 2
  103. E_SHT_RELA = 4
  104.  
  105.  
  106. def patchelf(f, oldoffset, newoffset):
  107.     f.seek(0)
  108.     magic = f.read(4)
  109.     if not magic == b'\x7fELF':
  110.         raise Exception('Magic number does not match')
  111.  
  112.     ei_class = struct.unpack('=B', f.read(1))[0]
  113.     if ei_class != E_CLASS64:
  114.         raise Exception('Not 64bit elf header: ' + ei_class)
  115.  
  116.     f.seek(40)
  117.     e_shoff = struct.unpack('=Q', f.read(8))[0]
  118.     f.seek(58)
  119.     e_shentsize = struct.unpack('=H', f.read(2))[0]
  120.     e_shnum = struct.unpack('=H', f.read(2))[0]
  121.     e_shstrndx = struct.unpack('=H', f.read(2))[0]
  122.  
  123.     print('e_shoff: 0x{:x} e_shentsize: 0x{:x} e_shnum:0x{:x} e_shstrndx:0x{:x}'.format(e_shoff, e_shentsize,
  124.                                                                                         e_shnum, e_shstrndx))
  125.  
  126.     for i in range(0, e_shnum):
  127.         f.seek(e_shoff + i * e_shentsize)
  128.         e_sh = struct.unpack('=LLQQQQLLQQ', f.read(e_shentsize))
  129.         # e_sh_name = e_sh[0]
  130.         e_sh_type = e_sh[1]
  131.         e_sh_offset = e_sh[4]
  132.         e_sh_size = e_sh[5]
  133.         e_sh_entsize = e_sh[9]
  134.         if e_sh_type == E_SHT_RELA:
  135.             e_sh_nument = int(e_sh_size / e_sh_entsize)
  136.             # print 'RELA at 0x{:x} with {:d} entries'.format(e_sh_offset, e_sh_nument)
  137.             for j in range(0, e_sh_nument):
  138.                 f.seek(e_sh_offset + e_sh_entsize * j)
  139.                 rela = struct.unpack('=QQq', f.read(e_sh_entsize))
  140.                 r_offset = rela[0]
  141.                 r_info = rela[1]
  142.                 r_addend = rela[2]
  143.                 if r_addend == oldoffset:
  144.                     r_addend = newoffset
  145.                     f.seek(e_sh_offset + e_sh_entsize * j)
  146.                     f.write(struct.pack('=QQq', r_offset, r_info, r_addend))
  147.                     print('Relocation modified at: ' + hex(e_sh_offset + e_sh_entsize * j))
  148.  
  149.  
  150. def patchkeys(f, key):
  151.     # Setup struct pack string
  152.     key_pack = '=4sB4sB6xQ'
  153.     # smc_old_memptr = 0
  154.     smc_new_memptr = 0
  155.  
  156.     # Do Until OSK1 read
  157.     i = 0
  158.     while True:
  159.  
  160.         # Read key into struct str and data byte str
  161.         offset = key + (i * 72)
  162.         f.seek(offset)
  163.         smc_key = struct.unpack(key_pack, f.read(24))
  164.         smc_data = f.read(smc_key[1])
  165.  
  166.         # Reset pointer to beginning of key entry
  167.         f.seek(offset)
  168.  
  169.         if smc_key[0] == b'SKL+':
  170.             # Use the +LKS data routine for OSK0/1
  171.             smc_new_memptr = smc_key[4]
  172.             print('+LKS Key: ')
  173.             printkey(i, offset, smc_key, smc_data)
  174.  
  175.         elif smc_key[0] == b'0KSO':
  176.             # Write new data routine pointer from +LKS
  177.             print('OSK0 Key Before:')
  178.             printkey(i, offset, smc_key, smc_data)
  179.             # smc_old_memptr = smc_key[4]
  180.             f.seek(offset)
  181.             f.write(struct.pack(key_pack, smc_key[0], smc_key[1], smc_key[2], smc_key[3], smc_new_memptr))
  182.             f.flush()
  183.  
  184.             # Write new data for key
  185.             f.seek(offset + 24)
  186.             smc_new_data = codecs.encode('bheuneqjbexolgurfrjbeqfthneqrqcy', 'rot_13')
  187.             f.write(smc_new_data.encode('UTF-8'))
  188.             f.flush()
  189.  
  190.             # Re-read and print key
  191.             f.seek(offset)
  192.             smc_key = struct.unpack(key_pack, f.read(24))
  193.             smc_data = f.read(smc_key[1])
  194.             print('OSK0 Key After:')
  195.             printkey(i, offset, smc_key, smc_data)
  196.  
  197.         elif smc_key[0] == b'1KSO':
  198.             # Write new data routine pointer from +LKS
  199.             print('OSK1 Key Before:')
  200.             printkey(i, offset, smc_key, smc_data)
  201.             smc_old_memptr = smc_key[4]
  202.             f.seek(offset)
  203.             f.write(struct.pack(key_pack, smc_key[0], smc_key[1], smc_key[2], smc_key[3], smc_new_memptr))
  204.             f.flush()
  205.  
  206.             # Write new data for key
  207.             f.seek(offset + 24)
  208.             smc_new_data = codecs.encode('rnfrqbagfgrny(p)NccyrPbzchgreVap', 'rot_13')
  209.             f.write(smc_new_data.encode('UTF-8'))
  210.             f.flush()
  211.  
  212.             # Re-read and print key
  213.             f.seek(offset)
  214.             smc_key = struct.unpack(key_pack, f.read(24))
  215.             smc_data = f.read(smc_key[1])
  216.             print('OSK1 Key After:')
  217.             printkey(i, offset, smc_key, smc_data)
  218.  
  219.             # Finished so get out of loop
  220.             break
  221.  
  222.         else:
  223.             pass
  224.  
  225.         i += 1
  226.     return smc_old_memptr, smc_new_memptr
  227.  
  228.  
  229. def patchsmc(name, sharedobj):
  230.     with open(name, 'r+b') as f:
  231.  
  232.         smc_old_memptr = 0
  233.         smc_new_memptr = 0
  234.  
  235.         # Read file into string variable
  236.         vmx = f.read()
  237.  
  238.         print('File: ' + name + '\n')
  239.  
  240.         # Setup hex string for vSMC headers
  241.         # These are the private and public key counts
  242.         smc_header_v0 = b'\xF2\x00\x00\x00\xF0\x00\x00\x00'
  243.         smc_header_v1 = b'\xB4\x01\x00\x00\xB0\x01\x00\x00'
  244.  
  245.         # Setup hex string for #KEY key
  246.         key_key = b'\x59\x45\x4B\x23\x04\x32\x33\x69\x75'
  247.  
  248.         # Setup hex string for $Adr key
  249.         adr_key = b'\x72\x64\x41\x24\x04\x32\x33\x69\x75'
  250.  
  251.         # Find the vSMC headers
  252.         smc_header_v0_offset = vmx.find(smc_header_v0) - 8
  253.         smc_header_v1_offset = vmx.find(smc_header_v1) - 8
  254.  
  255.         # Find '#KEY' keys
  256.         smc_key0 = vmx.find(key_key)
  257.         smc_key1 = vmx.rfind(key_key)
  258.  
  259.         # Find '$Adr' key only V1 table
  260.         smc_adr = vmx.find(adr_key)
  261.  
  262.         # Print vSMC0 tables and keys
  263.         print('appleSMCTableV0 (smc.version = "0")')
  264.         print('appleSMCTableV0 Address      : ' + hex(smc_header_v0_offset))
  265.         print('appleSMCTableV0 Private Key #: 0xF2/242')
  266.         print('appleSMCTableV0 Public Key  #: 0xF0/240')
  267.  
  268.         if (smc_adr - smc_key0) != 72:
  269.             print('appleSMCTableV0 Table        : ' + hex(smc_key0))
  270.             smc_old_memptr, smc_new_memptr = patchkeys(f, smc_key0)
  271.         elif (smc_adr - smc_key1) != 72:
  272.             print('appleSMCTableV0 Table        : ' + hex(smc_key1))
  273.             smc_old_memptr, smc_new_memptr = patchkeys(f, smc_key1)
  274.  
  275.         print()
  276.  
  277.         # Print vSMC1 tables and keys
  278.         print('appleSMCTableV1 (smc.version = "1")')
  279.         print('appleSMCTableV1 Address      : ' + hex(smc_header_v1_offset))
  280.         print('appleSMCTableV1 Private Key #: 0x01B4/436')
  281.         print('appleSMCTableV1 Public Key  #: 0x01B0/432')
  282.  
  283.         if (smc_adr - smc_key0) == 72:
  284.             print('appleSMCTableV1 Table        : ' + hex(smc_key0))
  285.             smc_old_memptr, smc_new_memptr = patchkeys(f, smc_key0)
  286.         elif (smc_adr - smc_key1) == 72:
  287.             print('appleSMCTableV1 Table        : ' + hex(smc_key1))
  288.             smc_old_memptr, smc_new_memptr = patchkeys(f, smc_key1)
  289.  
  290.         print()
  291.  
  292.         # Find matching RELA record in .rela.dyn in ESXi ELF files
  293.         # This is temporary code until proper ELF parsing written
  294.         if sharedobj:
  295.             print('Modifying RELA records from: ' + hex(smc_old_memptr) + ' to ' + hex(smc_new_memptr))
  296.             patchelf(f, smc_old_memptr, smc_new_memptr)
  297.  
  298.         # Tidy up
  299.         f.flush()
  300.         f.close()
  301.  
  302.  
  303. def patchbase(name):
  304.     # Patch file
  305.     print('GOS Patching: ' + name)
  306.     f = open(name, 'r+b')
  307.  
  308.     # Entry to search for in GOS table
  309.     # Should work for Workstation 12-15...
  310.     darwin = re.compile(
  311.              b'\x10\x00\x00\x00[\x10|\x20]\x00\x00\x00[\x01|\x02]\x00\x00\x00\x00\x00\x00\x00'
  312.              b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00')
  313.  
  314.     # Read file into string variable
  315.     base = f.read()
  316.  
  317.     # Loop through each entry and set top bit
  318.     # 0xBE --> 0xBF (WKS 12)
  319.     # 0x3E --> 0x3F (WKS 14)
  320.     for m in darwin.finditer(base):
  321.         offset = m.start()
  322.         f.seek(offset + 32)
  323.         flag = ord(f.read(1))
  324.         flag = set_bit(flag, 0)
  325. #        flag = chr(flag)
  326.         f.seek(offset + 32)
  327.         f.write(bytes([flag]))
  328.         print('GOS Patched flag @: ' + hex(offset))
  329.  
  330.     # Tidy up
  331.     f.flush()
  332.     f.close()
  333.     print('GOS Patched: ' + name)
  334.  
  335.  
  336. def patchvmkctl(name):
  337.     # Patch file
  338.     print('smcPresent Patching: ' + name)
  339.     f = open(name, 'r+b')
  340.  
  341.     # Read file into string variable
  342.     vmkctl = f.read()
  343.     applesmc = vmkctl.find(b'applesmc')
  344.     f.seek(applesmc)
  345.     f.write(b'vmkernel')
  346.  
  347.     # Tidy up
  348.     f.flush()
  349.     f.close()
  350.     print('smcPresent Patched: ' + name)
  351.  
  352.  
  353. # noinspection PyUnresolvedReferences
  354. def main():
  355.     # Work around absent Platform module on VMkernel
  356.     if os.name == 'nt' or os.name == 'cli':
  357.         osname = 'windows'
  358.     else:
  359.         osname = os.uname()[0].lower()
  360.  
  361.     # vmwarebase = ''
  362.     vmx_so = False
  363.  
  364.     # Setup default paths
  365.     if osname == 'linux':
  366.         vmx_path = '/usr/lib/vmware/bin/'
  367.         vmx = joinpath(vmx_path, 'vmware-vmx')
  368.         vmx_debug = joinpath(vmx_path, 'vmware-vmx-debug')
  369.         vmx_stats = joinpath(vmx_path, 'vmware-vmx-stats')
  370.         if os.path.isfile('/usr/lib/vmware/lib/libvmwarebase.so/libvmwarebase.so'):
  371.             vmx_so = True
  372.             vmwarebase = '/usr/lib/vmware/lib/libvmwarebase.so/libvmwarebase.so'
  373.         else:
  374.             vmwarebase = '/usr/lib/vmware/lib/libvmwarebase.so.0/libvmwarebase.so.0'
  375.  
  376.     elif osname == 'windows':
  377.         reg = ConnectRegistry(None, HKEY_LOCAL_MACHINE)
  378.         key = OpenKey(reg, r'SOFTWARE\Wow6432Node\VMware, Inc.\VMware Workstation')
  379.         vmwarebase_path = QueryValueEx(key, 'InstallPath')[0]
  380.         vmx_path = QueryValueEx(key, 'InstallPath64')[0]
  381.         vmx = joinpath(vmx_path, 'vmware-vmx.exe')
  382.         vmx_debug = joinpath(vmx_path, 'vmware-vmx-debug.exe')
  383.         vmx_stats = joinpath(vmx_path, 'vmware-vmx-stats.exe')
  384.         vmwarebase = joinpath(vmwarebase_path, 'vmwarebase.dll')
  385.  
  386.     else:
  387.         print('Unknown Operating System: ' + osname)
  388.         return
  389.  
  390.     # Patch the vmx executables skipping stats version for Player
  391.     patchsmc(vmx, vmx_so)
  392.     patchsmc(vmx_debug, vmx_so)
  393.     if os.path.isfile(vmx_stats):
  394.         patchsmc(vmx_stats, vmx_so)
  395.  
  396.     # Patch vmwarebase for Workstation and Player
  397.     patchbase(vmwarebase)
  398.  
  399.  
  400. if __name__ == '__main__':
  401.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement