首页 > 解决方案 > create shortcut with Run As Administrator

问题描述

im trying to create a shortcut that needs administrator privileges ... i found this code on the internet but its coded in powershell ... i test it its working !! but i need it in python how i can do the same with python

powershell code :

Read the .lnk file in as an array of bytes. Locate byte 21 (0x15) and change bit 6 (0x20) to 1. This is the RunAsAdministrator flag. Then you write you byte array back into the .lnk file.

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

this is my python code :

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 

i dont know what is the next step can u help me ?

标签: pythonarrayspowershellbyteshortcut

解决方案


您可以使用库来编辑.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)

推荐阅读