使用“以管理员身份运行”创建快捷方式

时间:2018-10-01 07:58:37

标签: python arrays powershell byte shortcut

我试图创建一个需要管理员权限的快捷方式...我在互联网上找到了此代码,但是它在powershell中进行了编码...我对其进行了测试!但我需要在python中使用python

做同样的事情

powershell代码:

  

以字节数组形式读取.lnk文件。找到字节21(0x15)和   将位6(0x20)更改为1。这是RunAsAdministrator标志。然后   您将字节数组写回到.lnk文件。

$WshShell = New-Object -comObject WScript.Shell
$Shortcut = $WshShell.CreateShortcut("$Home\Desktop\ColorPix.lnk")
$Shortcut.TargetPath = "C:\Program Files (x86)\ColorPix\ColorPix.exe"
$Shortcut.Save()

$bytes = [System.IO.File]::ReadAllBytes("$Home\Desktop\ColorPix.lnk")
$bytes[0x15] = $bytes[0x15] -bor 0x20 #set byte 21 (0x15) bit 6 (0x20) ON
[System.IO.File]::WriteAllBytes("$Home\Desktop\ColorPix.lnk", $bytes)

这是我的python代码:

filename = r'C:\Users\root\Desktop\qassam.lnk'

with open(filename, "rb") as f2:

    while True:
      current_byte = f2.read(1)
      if (not current_byte):
        break
      val = ord(current_byte)
      q = hex(val)
      print q 

我不知道下一步该怎么办?

2 个答案:

答案 0 :(得分:1)

您可以使用库来编辑.lnk个文件(我未检查):

或使用bytearray

模拟命令的行为
filename = r'C:\Users\root\Desktop\qassam.lnk'

# $bytes = [System.IO.File]::ReadAllBytes("$Home\Desktop\ColorPix.lnk")
with open(filename, "rb") as f2:
  ba = bytearray(f2.read())

# $bytes[0x15] = $bytes[0x15] -bor 0x20 #set byte 21 (0x15) bit 6 (0x20) ON
ba[0x15] = ba[0x15] | 0x20

# [System.IO.File]::WriteAllBytes("$Home\Desktop\ColorPix.lnk", $bytes)
with open(filename, "wb") as f3:
  f3.write(ba)

答案 1 :(得分:0)

此代码生成了快捷方式。但是我不知道你到底想要什么。

代码:

import os

def short(Shortcut_Path, _Path):
    Shortcut = """
    $WshShell = New-Object -comObject WScript.Shell
    $Shortcut = $WshShell.CreateShortcut("{}")
    $Shortcut.TargetPath = "{}"
    $Shortcut.Save()""".format(Shortcut_Path, _Path)

    open("Shortcut.ps1", "w").write(Shortcut)
    os.system("Shortcut.ps1")
    os.remove("Shortcut.ps1")

short(os.environ["USERPROFILE"] + r"\Desktop\AIMP.lnk", r"C:\Program Files 
(x86)\AIMP\AIMP.exe")
相关问题