|
| 1 | +from CustomLibs import ShadowCopies |
| 2 | +from CustomLibs import time_conversion as TC |
| 3 | +import os |
| 4 | +import shutil |
| 5 | +from Registry import Registry |
| 6 | +import codecs |
| 7 | + |
| 8 | +def decode_rot13(string): |
| 9 | + return codecs.decode(string, 'rot13') |
| 10 | + |
| 11 | + |
| 12 | +# copy locked NTUSER.DAT file |
| 13 | +def copy_locked_NTUSER(user): |
| 14 | + try: |
| 15 | + ShadowCopies.create_shadow_copy() # create shadow copy |
| 16 | + shadow_copy_path = ShadowCopies.get_latest_shadow_copy() # get latest shadow copy |
| 17 | + NTUSER_source = os.path.join(shadow_copy_path, "Users", str(user), "NTUSER.DAT") # NTUSER.DAT source path |
| 18 | + destination_path = os.path.join(os.getcwd(), "NTUSER_copy") # destination to copy NTUSER to |
| 19 | + |
| 20 | + shutil.copy(NTUSER_source, destination_path) # copy the NTUSER.DAT file |
| 21 | + |
| 22 | + # delete shadow copy |
| 23 | + shadow_ID = ShadowCopies.get_latest_shadow_copy_id() # get shadow copy ID |
| 24 | + ShadowCopies.delete_shadow_copy(shadow_ID) # delete shadow copy |
| 25 | + except Exception as e: |
| 26 | + print("Error: Make sure you are running as administrator.") |
| 27 | + |
| 28 | +# copy NTUSER.DAT file from mounted drive |
| 29 | +def copy_mounted_NTUSER(drive, user): |
| 30 | + NTUSER_path = f"{drive}[root]\\Users\\{user}\\NTUSER.DAT" |
| 31 | + destination_path = os.path.join(os.getcwd(), "NTUSER_copy") |
| 32 | + shutil.copy(NTUSER_path, destination_path) |
| 33 | + |
| 34 | +def find_lnk_guid_path(user_assist_key): |
| 35 | + # Search for the GUID containing LNK files (".yax" in ROT13) |
| 36 | + for GUID in user_assist_key.subkeys(): |
| 37 | + for Count in GUID.subkeys(): |
| 38 | + for value in Count.values(): |
| 39 | + if value.name().endswith(".yax"): |
| 40 | + clean_path = (user_assist_key.path()).split("Software", 1)[-1] |
| 41 | + return f"Software{clean_path}\\{GUID.name()}\\Count" |
| 42 | + return None |
| 43 | + |
| 44 | +def decode_data(data): |
| 45 | + if len(data) >= 12: |
| 46 | + last_executed_filetime = int.from_bytes(data[60:68], byteorder="little") |
| 47 | + return last_executed_filetime |
| 48 | + return None |
| 49 | + |
| 50 | + |
| 51 | +def sanitize_name(name): |
| 52 | + if "{0139D44E-6AFE-49F2-8690-3DAFCAE6FFB8}" in name: |
| 53 | + return name.replace("{0139D44E-6AFE-49F2-8690-3DAFCAE6FFB8}", "{Common Programs}") |
| 54 | + elif "{9E3995AB-1F9C-4F13-B827-48B24B6C7174}" in name: |
| 55 | + return name.replace("{9E3995AB-1F9C-4F13-B827-48B24B6C7174}", "{User Pinned}") |
| 56 | + elif "{A77F5D77-2E2B-44C3-A6A2-ABA601054A51}" in name: |
| 57 | + return name.replace("{A77F5D77-2E2B-44C3-A6A2-ABA601054A51}", "{Programs}") |
| 58 | + else: |
| 59 | + return name |
| 60 | + |
| 61 | + |
| 62 | +# parse NTUSER.DAT information |
| 63 | +def get_user_assist(drive, user): |
| 64 | + lnk_guid_list = [] |
| 65 | + |
| 66 | + # create NTUSER.DAT copy |
| 67 | + if drive == "C:\\": |
| 68 | + if not os.path.exists("NTUSER_copy"): |
| 69 | + copy_locked_NTUSER(user) |
| 70 | + else: |
| 71 | + if not os.path.exists("NTUSER_copy"): |
| 72 | + copy_mounted_NTUSER(drive, user) |
| 73 | + |
| 74 | + # open registry file and access user assist key |
| 75 | + reg = Registry.Registry("NTUSER_copy") |
| 76 | + user_assist_key_path = "Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\UserAssist" |
| 77 | + user_assist_key = reg.open(user_assist_key_path) |
| 78 | + |
| 79 | + # get path with lnk files |
| 80 | + lnk_guid = find_lnk_guid_path(user_assist_key) |
| 81 | + if lnk_guid is None: |
| 82 | + return 0 |
| 83 | + |
| 84 | + # collect contents of LNK GUID folder |
| 85 | + lnk_guid = reg.open(lnk_guid) |
| 86 | + for value in lnk_guid.values(): |
| 87 | + if (value.name()).endswith(".yax"): |
| 88 | + # name full path of program |
| 89 | + path = decode_rot13(value.name()) |
| 90 | + |
| 91 | + # sanitize names |
| 92 | + path = sanitize_name(path) |
| 93 | + |
| 94 | + last_execution = TC.filetime_convert(decode_data(value.value())) # last execution date |
| 95 | + |
| 96 | + # add data to list |
| 97 | + if path.endswith(".lnk"): |
| 98 | + path = path[:-4] |
| 99 | + lnk_guid_list.append(["Executed Program", path, last_execution]) |
| 100 | + |
| 101 | + os.remove("NTUSER_copy") |
| 102 | + return lnk_guid_list |
0 commit comments