【问题标题】:create shortcut with Run As Administrator以管理员身份运行创建快捷方式
【发布时间】:2018-10-01 07:58:36
【问题描述】:

我正在尝试创建一个需要管理员权限的快捷方式...我在互联网上找到了此代码,但它是在 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 

我不知道下一步是什么你能帮我吗?

【问题讨论】:

    标签: python arrays powershell byte shortcut


    【解决方案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)
    

    【讨论】:

    • 我收到此错误:使用 open(filename, "wb") as f3: IOError: [Errno 13] Permission denied: 'C:\\Users\\Public\\Desktop\\Google Chrome .lnk'
    • @evilcode1 您可以尝试使用自己创建的.lnk 吗?对我来说,您无权编辑 chrome 快捷方式(这是病毒会做的)
    • 解决了,非常感谢你....你能解释一下这条线在做什么吗?为了 ?? ( 巴[0x15] = 巴[0x15] | 0x20 )
    • @evilcode1 我已将这些行与您的 powershell 示例的行进行了匹配。我希望现在一切都清楚了
    【解决方案2】:

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

    代码:

    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")
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-05-07
      • 1970-01-01
      • 2016-08-31
      • 2015-05-13
      • 2011-11-15
      • 1970-01-01
      • 2011-04-12
      • 2012-06-25
      相关资源
      最近更新 更多