refs #1307 fix calls to chntpw

pull/1/head
Natalia Serrano 2024-12-18 12:12:25 +01:00
parent f6ae950201
commit 93177def1f
1 changed files with 66 additions and 68 deletions

View File

@ -19,7 +19,9 @@ import FileLib
# Función ficticia para lanzar chntpw con timeout de 5 s., evitando cuelgues del programa.
chntpw_exe = shutil.which ('drbl-chntpw') or shutil.which ('chntpw')
def chntpw (hivefile, input_file):
return subprocess.run ([chntpw_exe, '-e', hivefile], timeout=5, input=open(input_file, 'r'), capture_output=True, text=True).stdout
with open (input_file, 'r') as fd:
input_contents = fd.read()
return subprocess.run ([chntpw_exe, '-e', hivefile], timeout=5, input=input_contents, capture_output=True, text=True).stdout
## en el codigo bash aparecen "${3%\\*}" y "${3##*\\}" varias veces
## ${3%\\*} es el "dirname" de una key del registro
@ -49,13 +51,13 @@ def ogAddRegistryKey (mntpt, hive, k):
k_dirname, k_basename = _split_k (k)
tmpfile = tempfile.TemporaryFile (prefix='chntpw-', mode='w')
with open (tmpfile, 'w') as f:
with tempfile.NamedTemporaryFile (delete_on_close=False, prefix='chntpw-', mode='w') as f:
f.write (f'cd {k_dirname}\n')
f.write (f'nk {k_basename}\n')
f.write ('q\ny\n')
chntpw (hivefile, tmpfile)
os.remove (tmpfile)
f.close()
chntpw (hivefile, f.name)
os.remove (f.name)
#/**
# ogAddRegistryValue path_mountpoint str_hive str_valuename [str_valuetype]
@ -88,13 +90,13 @@ def ogAddRegistryValue (mntpt, hive, k, vtype='STRING'):
SystemLib.ogRaiseError ([], ogGlobals.OG_ERR_OUTOFLIMIT, vtype)
return
tmpfile = tempfile.TemporaryFile (prefix='chntpw-', mode='w')
with open (tmpfile, 'w') as f:
with tempfile.NamedTemporaryFile (delete_on_close=False, prefix='chntpw-', mode='w') as f:
f.write (f'cd {k_dirname}\n')
f.write (f'nv {TYPE} {k_basename}\n')
f.write ('q\ny\n')
chntpw (hivefile, tmpfile)
os.remove (tmpfile)
f.close()
chntpw (hivefile, f.name)
os.remove (f.name)
#/**
@ -117,13 +119,13 @@ def ogDeleteRegistryKey (mntpt, hive, k):
k_dirname, k_basename = _split_k (k)
tmpfile = tempfile.TemporaryFile (prefix='chntpw-', mode='w')
with open (tmpfile, 'w') as f:
with tempfile.NamedTemporaryFile (delete_on_close=False, prefix='chntpw-', mode='w') as f:
f.write (f'cd {k_dirname}\n')
f.write (f'dk {k_basename}\n')
f.write ('q\ny\n')
chntpw (hivefile, tmpfile)
os.remove (tmpfile)
f.close()
chntpw (hivefile, f.name)
os.remove (f.name)
#/**
@ -146,13 +148,13 @@ def ogDeleteRegistryValue (mntpt, hive, k):
k_dirname, k_basename = _split_k (k)
tmpfile = tempfile.TemporaryFile (prefix='chntpw-', mode='w')
with open(tmpfile, 'w') as f:
with tempfile.NamedTemporaryFile (delete_on_close=False, prefix='chntpw-', mode='w') as f:
f.write (f'cd {k_dirname}\n')
f.write (f'dv {k_basename}\n')
f.write ('q\ny\n')
chntpw (hivefile, tmpfile)
os.remove(tmpfile)
f.close()
chntpw (hivefile, f.name)
os.remove(f.name)
#/**
@ -167,6 +169,8 @@ def ogDeleteRegistryValue (mntpt, hive, k):
#@warning El sistema de archivos de Windows debe estar montada previamente.
#*/ ##
#ogGetHivePath ('/mnt/sda1', 'user1') => /mnt/sda1/Users/user1/NTUSER.DAT
#ogGetHivePath ('/mnt/sda1', 'SYSTEM') => //mnt/sda1/Windows/System32/config/SYSTEM
#ogGetHivePath ('/mnt/sda1', 'IEUser') => //mnt/sda1/Users/IEUser/NTUSER.DAT
def ogGetHivePath(mntpt, hive):
# Camino del fichero de registro de usuario o de sistema (de menor a mayor prioridad).
FILE = FileLib.ogGetPath(file=f"/{mntpt}/Windows/System32/config/{hive}")
@ -176,11 +180,7 @@ def ogGetHivePath(mntpt, hive):
if FILE and os.path.isfile(FILE):
return FILE
else:
SystemLib.ogRaiseError(
[],
ogGlobals.OG_ERR_NOTFOUND,
f"{mntpt} {hive}"
)
SystemLib.ogRaiseError ([], ogGlobals.OG_ERR_NOTFOUND, f'{mntpt} {hive}')
return None
@ -198,19 +198,19 @@ def ogGetHivePath(mntpt, hive):
#@warning El sistema de archivos de Windows debe estar montado previamente.
#*/ ##
def ogGetRegistryValue (mntpt, hive, k):
FILE = ogGetHivePath(mntpt, hive)
if not FILE: return
hivefile = ogGetHivePath(mntpt, hive)
if not hivefile: return
k_dirname, k_basename = _split_k (k)
tmpfile = tempfile.TemporaryFile (prefix='chntpw-', mode='w')
with open(tmpfile, 'w') as f:
with tempfile.NamedTemporaryFile (delete_on_close=False, prefix='chntpw-', mode='w') as f:
f.write (f'cd {k_dirname}\n')
f.write (f'cat {k_basename}\n')
f.write ('q\n')
f.close()
chntpw_out = chntpw (hivefile, f.name)
os.remove (f.name)
chntpw_out = chntpw (hivefile, tmpfile)
os.remove (tmpfile)
lines = chntpw_out.splitlines()
if 2 != len (lines):
return None
@ -234,17 +234,17 @@ def ogGetRegistryValue (mntpt, hive, k):
#*/ ##
#ogListRegistryKeys ('/mnt/sda1', 'SOFTWARE', '\Microsoft\Windows\CurrentVersion')
def ogListRegistryKeys (mntpt, hive, k):
FILE = ogGetHivePath(mntpt, hive)
if not FILE: return
hivefile = ogGetHivePath(mntpt, hive)
if not hivefile: return
tmpfile = tempfile.TemporaryFile (prefix='chntpw-', mode='w')
with open(tmpfile, 'w') as f:
with tempfile.NamedTemporaryFile (delete_on_close=False, prefix='chntpw-', mode='w') as f:
f.write (f'ls {k}\n')
f.write ('q\n')
chntpw_out = chntpw (hivefile, tmpfile)
os.remove (tmpfile)
lines = chntpw_out.splitlines()
f.close()
chntpw_out = chntpw (hivefile, f.name)
os.remove (f.name)
lines = chntpw_out.splitlines()
ret = []
for l in lines:
elems = re.split ('[<>]', l)
@ -269,17 +269,17 @@ def ogListRegistryKeys (mntpt, hive, k):
#*/ ##
#ogListRegistryValues ('/mnt/sda1', 'SOFTWARE', '\Microsoft\Windows\CurrentVersion')
def ogListRegistryValues (mntpt, hive, k):
FILE = ogGetHivePath(mntpt, hive)
if not FILE: return
hivefile = ogGetHivePath(mntpt, hive)
if not hivefile: return
tmpfile = tempfile.TemporaryFile (prefix='chntpw-', mode='w')
with open(tmpfile, 'w') as f:
with tempfile.NamedTemporaryFile (delete_on_close=False, prefix='chntpw-', mode='w') as f:
f.write (f'ls {k}\n')
f.write ('q\n')
chntpw_out = chntpw (hivefile, tmpfile)
os.remove (tmpfile)
lines = chntpw_out.splitlines()
f.close()
chntpw_out = chntpw (hivefile, f.name)
os.remove (f.name)
lines = chntpw_out.splitlines()
ret = []
for l in lines:
elems = re.split ('[<>]', l)
@ -331,33 +331,31 @@ def ogSetRegistryValue (mntpt, hive, k, v):
k_dirname, k_basename = _split_k (k)
tmpfile = tempfile.TemporaryFile (prefix='chntpw-', mode='w')
try:
with open(tmpfile, 'w') as f:
f.write (f"ls {k_dirname}\n")
f.write ('q\n')
with tempfile.NamedTemporaryFile (delete_on_close=False, prefix='chntpw-', mode='w') as f:
## TODO: indentation here. This 'try' should be indented
f.write (f"ls {k_dirname}\n")
f.write ('q\n')
f.close()
chntpw_out = chntpw (hivefile, f.name)
os.remove(f.name)
chntpw_out = chntpw (hivefile, tmpfile)
if re.search (f"BINARY.*<{k_basename}>", chntpw_out):
## the entry in the registry is binary. Our input should be a sequence of bytes
if re.search (f"BINARY.*<{k_basename}>", chntpw_out):
## the entry in the registry is binary. Our input should be a sequence of bytes
if ' ' != v[-1]: v += ' ' ## the regex below requires a trailing space
if not re.match (r'^([0-9A-F]{2} )*$', v.upper()):
SystemLib.ogRaiseError ([], ogGlobals.OG_ERR_FORMAT, f'"{v}"')
return
if ' ' != v[-1]: v += ' ' ## the regex below requires a trailing space
if not re.match (r'^([0-9A-F]{2} )*$', v.upper()):
SystemLib.ogRaiseError ([], ogGlobals.OG_ERR_FORMAT, f'"{v}"')
return
formatted = _format_hex (v.upper())
formatted += '\ns'
else:
formatted = v
formatted = _format_hex (v.upper())
formatted += '\ns'
else:
formatted = v
with open(tmpfile, 'w') as f:
f.write (f'cd {k_dirname}\n')
f.write (f'ed {k_basename}\n')
f.write (f'{formatted}\n')
f.write ('q\ny\n')
# Aplicar cambios.
chntpw (hivefile, tmpfile)
finally:
os.remove(tmpfile)
with tempfile.NamedTemporaryFile (delete_on_close=False, prefix='chntpw-', mode='w') as f:
f.write (f'cd {k_dirname}\n')
f.write (f'ed {k_basename}\n')
f.write (f'{formatted}\n')
f.write ('q\ny\n')
chntpw (hivefile, f.name)
os.remove(f.name)