nvidia-patch/win/tools/autopatch/autopatch.py

251 lines
8.7 KiB
Python
Raw Normal View History

2019-02-11 20:51:25 +00:00
#!/usr/bin/env python3
import argparse
2019-02-11 20:51:25 +00:00
import sys
import subprocess
import tempfile
import os.path
from binascii import unhexlify
import xml.etree.ElementTree as ET
import itertools
import functools
2019-02-11 20:51:25 +00:00
CRLF = b"\x0d\x0a"
HEADER_FORMAT = b">%s"
LINE_FORMAT = CRLF + b"%016X:%02X->%02X"
OFFSET_ADJUSTMENT = 0xC00 # shift specific to x64dbg .1337 format
def parse_args():
parser = argparse.ArgumentParser(
description="Generates .1337 patch for Nvidia drivers for Windows",
formatter_class=argparse.ArgumentDefaultsHelpFormatter)
parser.add_argument("installer_file",
nargs="+",
help="location of installer executable(s)")
2019-02-11 20:51:25 +00:00
parser.add_argument("-7", "--7zip",
default="7z",
dest="sevenzip",
help="location of 7-zip `7z` executable")
parser.add_argument("-T", "--target",
nargs="+",
default=[
"Display.Driver/nvcuvid64.dl_",
"Display.Driver/nvcuvid32.dl_",
],
help="target location(s) in archive")
2019-02-11 20:51:25 +00:00
parser.add_argument("-N", "--target-name",
nargs="+",
default=[
"nvcuvid.dll",
"nvcuvid.dll",
],
help="name(s) of installed target file. Used for patch "
2019-02-11 20:51:25 +00:00
"header")
parser.add_argument("-P", "--patch-name",
nargs="+",
default=[
"nvcuvid64.1337",
"nvcuvid32.1337",
],
help="relative filename(s) of generated patch(es)")
2019-02-11 20:51:25 +00:00
parser.add_argument("-S", "--search",
nargs="+",
default=[
"FF909800000084C075",
"8B404CFFD084C075",
],
help="representation of search pattern(s) binary string")
2019-02-11 20:51:25 +00:00
parser.add_argument("-R", "--replacement",
nargs="+",
default=[
"FF90980000000C0175",
"8B404CFFD00C0175",
],
help="representation of replacement(s) binary string")
2019-02-11 20:51:25 +00:00
parser.add_argument("-o", "--stdout",
action="store_true",
help="output into stdout")
2019-09-14 13:10:28 +00:00
parser.add_argument("-D", "--direct",
action="store_true",
help="supply patched library directly instead of "
"installer file. Implies --stdout option.")
2019-02-11 20:51:25 +00:00
args = parser.parse_args()
return args
class ExtractException(Exception):
pass
class PatternNotFoundException(Exception):
pass
2019-10-22 16:00:25 +00:00
class MultipleOccurencesException(Exception):
pass
2019-06-09 18:34:57 +00:00
class UnknownPlatformException(Exception):
pass
2019-02-11 20:51:25 +00:00
class ExtractedTarget:
name = None
def __init__(self, archive, dst_dir, arch_tgt, *, sevenzip="7z"):
self._archive = archive
self._dst_dir = dst_dir
self._sevenzip = sevenzip
self._arch_tgt = arch_tgt
def __enter__(self):
ret = subprocess.call([self._sevenzip,
"e",
"-o" + self._dst_dir,
self._archive,
self._arch_tgt],
stdout=sys.stderr)
if ret != 0:
raise ExtractException("Subprocess returned non-zero exit code.")
name = os.path.join(self._dst_dir, os.path.basename(self._arch_tgt))
self.name = name
return name
def __exit__(self, exc_type, exc_value, traceback):
if self.name is not None:
os.remove(self.name)
def expand(filename, *, sevenzip="7z"):
proc = subprocess.Popen([sevenzip,
"x",
"-so",
filename], stdout=subprocess.PIPE)
result = proc.communicate()[0]
if proc.returncode != 0:
raise ExtractException("Subprocess returned non-zero exit code.")
return result
def extract_single_file(archive, filename, *, sevenzip="7z"):
proc = subprocess.Popen([sevenzip,
"e",
"-so",
archive,
filename], stdout=subprocess.PIPE)
result = proc.communicate()[0]
if proc.returncode != 0:
raise ExtractException("Subprocess returned non-zero exit code.")
return result
def make_patch(archive, *,
arch_tgt,
search,
replacement,
2019-09-14 13:10:28 +00:00
sevenzip="7z",
direct=False):
if direct:
with open(archive, 'rb') as fo:
f = fo.read()
else:
with tempfile.TemporaryDirectory() as tmpdir:
with ExtractedTarget(archive,
tmpdir,
arch_tgt,
sevenzip=sevenzip) as tgt:
f = expand(tgt, sevenzip=sevenzip)
2019-02-11 20:51:25 +00:00
offset = f.find(search)
if offset == -1:
raise PatternNotFoundException("Pattern not found.")
2019-10-22 16:00:25 +00:00
if f[offset+len(search):].find(search) != -1:
raise MultipleOccurencesException("Multiple occurences of pattern found!")
del f
2019-02-11 20:51:25 +00:00
print("Pattern found @ %016X" % (offset,), file=sys.stderr)
res = []
for (i, (left, right)) in enumerate(zip(search, replacement)):
if left != right:
res.append((offset + i, left, right))
return res
@functools.lru_cache(maxsize=None)
2019-02-11 20:51:25 +00:00
def identify_driver(archive, *, sevenzip="7z"):
manifest = extract_single_file(archive, "setup.cfg", sevenzip=sevenzip)
root = ET.fromstring(manifest)
version = root.attrib['version']
product_type = root.find('./properties/string[@name="ProductType"]')\
.attrib['value']
return version, product_type
def format_patch(diff, filename):
res = HEADER_FORMAT % filename.encode('utf-8')
for offset, left, right in diff:
res += LINE_FORMAT % (offset + OFFSET_ADJUSTMENT, left, right)
return res
def patch_flow(installer_file, search, replacement, target, target_name, patch_name, *,
direct=False, stdout=False, sevenzip="7z"):
search = unhexlify(search)
replacement = unhexlify(replacement)
2019-02-11 20:51:25 +00:00
assert len(search) == len(replacement), "len() of search and replacement"\
" is not equal"
patch = make_patch(installer_file,
arch_tgt=target,
search=search,
replacement=replacement,
sevenzip=sevenzip,
direct=direct)
patch_content = format_patch(patch, target_name)
if stdout or direct:
with open(sys.stdout.fileno(), mode='wb', closefd=False) as out:
out.write(patch_content)
else:
version, product_type = identify_driver(installer_file,
sevenzip=sevenzip)
drv_prefix = {
"100": "quadro_",
"300": "",
"301": "nsd_",
}
installer_name = os.path.basename(installer_file).lower()
if 'winserv2008' in installer_name:
os_prefix = 'ws2012_x64'
elif 'winserv-2016' in installer_name:
os_prefix = 'ws2016_x64'
elif 'win10' in installer_name:
os_prefix = 'win10_x64'
elif 'win7' in installer_name:
os_prefix = 'win7_x64'
2019-06-09 18:34:57 +00:00
else:
raise UnknownPlatformException("Can't infer platform from filename %s"
% (repr(installer_name),))
driver_name = drv_prefix[product_type] + version
out_dir = os.path.join(
os.path.dirname(
os.path.abspath(__file__)), '..', '..', os_prefix, driver_name)
os.makedirs(out_dir, 0o755, True)
out_filename = os.path.join(out_dir,
patch_name)
with open(out_filename, 'xb') as out:
out.write(patch_content)
def main():
args = parse_args()
if args.direct:
combinations = zip(args.installer_file, args.search, args.replacement,
args.target, args.target_name, args.patch_name)
else:
base_params = zip(args.search, args.replacement, args.target, args.target_name, args.patch_name)
combinations = ((l,) + r for l, r in itertools.product(args.installer_file, base_params))
for params in combinations:
patch_flow(*params, direct=args.direct, stdout=args.stdout)
2019-02-11 20:51:25 +00:00
if __name__ == '__main__':
main()