Created
December 31, 2021 09:38
-
-
Save segevfiner/0b530ee179ce6dd04fed211987c74dc5 to your computer and use it in GitHub Desktop.
Dump an .lnk file
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
""" | |
Dump an .lnk file. | |
""" | |
import sys | |
import argparse | |
from pprint import pprint | |
from win32com.shell import shell, shellcon # type: ignore | |
from win32com.propsys import propsys # type: ignore | |
import pythoncom | |
SLGP_RELATIVEPRIORITY = 0x0008 | |
def main(): | |
parser = argparse.ArgumentParser(description=__doc__) | |
parser.add_argument("file") | |
args = parser.parse_args() | |
shortcut = pythoncom.CoCreateInstance( | |
shell.CLSID_ShellLink, None, | |
pythoncom.CLSCTX_INPROC_SERVER, shell.IID_IShellLink | |
) | |
shortcut.QueryInterface(pythoncom.IID_IPersistFile).Load(args.file) | |
shortcut_data_list = shortcut.QueryInterface(shell.IID_IShellLinkDataList) | |
propstore = shortcut.QueryInterface(propsys.IID_IPropertyStore) | |
out = { | |
"RawPath": shortcut.GetPath(shell.SLGP_RAWPATH), | |
"RelativePath": shortcut.GetPath(SLGP_RELATIVEPRIORITY), | |
"IDList": shortcut.GetIDList(), | |
"Description": shortcut.GetDescription(), | |
"WorkingDirectory": shortcut.GetWorkingDirectory(), | |
"Arguments": shortcut.GetArguments(), | |
"Hotkey": shortcut.GetHotkey(), | |
"ShowCmd": shortcut.GetShowCmd(), | |
"IconLocation": shortcut.GetIconLocation(), | |
"Flags": shortcut_data_list.GetFlags(), | |
} | |
data = out["data"] = {} | |
flags = out["Flags"] | |
if flags & shellcon.SLDF_HAS_DARWINID: | |
data["EXP_DARWIN_LINK"] = shortcut_data_list.CopyDataBlock(shellcon.EXP_DARWIN_ID_SIG) | |
try: | |
data["EXP_SPECIAL_FOLDER"] = shortcut_data_list.CopyDataBlock(shellcon.EXP_SPECIAL_FOLDER_SIG) | |
except pythoncom.error: | |
pass | |
if flags & shellcon.SLDF_HAS_EXP_SZ: | |
data["EXP_SZ_LINK"] = shortcut_data_list.CopyDataBlock(shellcon.EXP_SZ_LINK_SIG) | |
if flags & shellcon.SLDF_HAS_EXP_ICON_SZ: | |
data["EXP_SZ_ICON"] = shortcut_data_list.CopyDataBlock(shellcon.EXP_SZ_ICON_SIG) | |
try: | |
data["NT_CONSOLE_PROPS"] = shortcut_data_list.CopyDataBlock(shellcon.NT_CONSOLE_PROPS_SIG) | |
except pythoncom.error: | |
pass | |
try: | |
data["NT_FE_CONSOLE_PROPS"] = shortcut_data_list.CopyDataBlock(shellcon.NT_FE_CONSOLE_PROPS_SIG) | |
except pythoncom.error: | |
pass | |
properties = out["Properties"] = [] | |
for i in range(propstore.GetCount()): | |
key = propstore.GetAt(i) | |
prop = { | |
"Key": key, | |
"Value": propstore.GetValue(key).ToString(), | |
} | |
try: | |
description = propsys.PSGetPropertyDescription(key) | |
prop["CanonicalName"] = description.GetCanonicalName() | |
prop["DisplayName"] = description.GetDisplayName() | |
except pythoncom.error: | |
pass | |
properties.append(prop) | |
pprint(out) | |
if __name__ == "__main__": | |
sys.exit(main()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment