python - 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 ?
解决方案
您可以使用库来编辑.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)
推荐阅读
- coldfusion - ColdFusion cffeed/cfoutput
- pyspark - 在pyspark中分组和透视而不聚合
- c# - 由于版本原因,无法使用 WooCommerceNET C# 与 Woocommerce 连接
- android - 查询设备通话记录时未知通话类型
- spring - 如何优雅地关闭 Spring Batch Job?(当前正在运行的作业应该完成)
- vue.js - 如何从VueJs中的数组中获取单个值
- html - 是什么决定了它的高度
元素? - html - 如何更改css样式的边框圆形项目符号?
- jenkins - Testcafe - 测试场景失败
- typescript - 与 Webpack 捆绑时出现循环引用错误