From b901c10f3c54c921ecdc8b485df0acc57050ead9 Mon Sep 17 00:00:00 2001 From: Alen <33656288+cnxysoft@users.noreply.github.com> Date: Tue, 30 Jul 2024 17:56:28 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9BW5=E5=90=AF=E5=8A=A8?= =?UTF-8?q?=E8=84=9A=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 支持脚本自动提权 自动覆盖dbghelp.dll 修改默认启用UTF8 修改不再从新窗口运行NC --- script/BootWay05-utf8.ps1 | 67 --------------------------------- script/BootWay05.ps1 | 78 +++++++++++++++++++++++++++++++++------ 2 files changed, 67 insertions(+), 78 deletions(-) delete mode 100644 script/BootWay05-utf8.ps1 diff --git a/script/BootWay05-utf8.ps1 b/script/BootWay05-utf8.ps1 deleted file mode 100644 index 054c7948..00000000 --- a/script/BootWay05-utf8.ps1 +++ /dev/null @@ -1,67 +0,0 @@ -function Get-QQpath { - try { - $key = Get-ItemProperty -Path "HKLM:\SOFTWARE\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall\QQ" - $uninstallString = $key.UninstallString - return [System.IO.Path]::GetDirectoryName($uninstallString) + "\QQ.exe" - } - catch { - throw "get QQ path error: $_" - } -} -function Select-QQPath { - Add-Type -AssemblyName System.Windows.Forms - [System.Windows.Forms.Application]::EnableVisualStyles() - - $dialogTitle = "Select QQ.exe" - - $filePicker = New-Object System.Windows.Forms.OpenFileDialog - $filePicker.Title = $dialogTitle - $filePicker.Filter = "Executable Files (*.exe)|*.exe|All Files (*.*)|*.*" - $filePicker.FilterIndex = 1 - $null = $filePicker.ShowDialog() - if (-not ($filePicker.FileName)) { - throw "User did not select an .exe file." - } - return $filePicker.FileName -} - -# 获取当前目录路径 -$currentPath = Get-Location - -# 替换\为/ -$currentPath = $currentPath -replace '\\', '/' - -# 生成JavaScript代码 -$jsCode = @" -(async () => { - await import('file:///$currentPath/napcat.mjs'); -})(); -"@ - -# 将JavaScript代码保存到文件中 -$jsFilePath = Join-Path $currentPath "loadScript.js" -$jsCode | Out-File -FilePath $jsFilePath -Encoding UTF8 - -Write-Output "JavaScript code has been generated and saved to $jsFilePath" -# 设置NAPCAT_PATH环境变量为 当前目录的loadScript.js地址 -$env:NAPCAT_PATH = $jsFilePath -for ($i = 0; $i -lt $args.Length; $i++) { - $param = $args[$i] - if ($args[$i] -eq "-q" -and $i + 1 -lt $args.Length) { - $nextParam = $args[$i + 1] - $params = "$param $nextParam" - break - } -} -Try { - $QQpath = Get-QQpath -} -Catch { - $QQpath = Select-QQPath -} - if (!(Test-Path $QQpath)) { - throw "provided QQ path is invalid: $QQpath" -} - -$commandInfo = Get-Command $QQpath -ErrorAction Stop -Start-Process powershell -ArgumentList "-noexit", "-noprofile", "-command &{& chcp 65001;& '$($commandInfo.Path)' --enable-logging $params}" diff --git a/script/BootWay05.ps1 b/script/BootWay05.ps1 index 01f4e287..fe643ba6 100644 --- a/script/BootWay05.ps1 +++ b/script/BootWay05.ps1 @@ -1,3 +1,22 @@ +# 检查当前会话是否具有管理员权限 +function Test-Administrator { + $user = [Security.Principal.WindowsIdentity]::GetCurrent() + (New-Object Security.Principal.WindowsPrincipal $user).IsInRole([Security.Principal.WindowsBuiltinRole]::Administrator) +} + +if (-not (Test-Administrator)) { + # 如果不是管理员,则重新启动脚本以管理员模式运行 + $scriptPath = $myInvocation.MyCommand.Path + if (-not $scriptPath) { + $scriptPath = $PSCommandPath + } + $newProcess = New-Object System.Diagnostics.ProcessStartInfo "powershell"; + $newProcess.Arguments = "-File `"$scriptPath`" $args" + $newProcess.Verb = "runas"; + [System.Diagnostics.Process]::Start($newProcess); + exit +} + function Get-QQpath { try { $key = Get-ItemProperty -Path "HKLM:\SOFTWARE\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall\QQ" @@ -25,6 +44,10 @@ function Select-QQPath { return $filePicker.FileName } +# 设置当前工作目录 +$scriptDirectory = Split-Path -Path $MyInvocation.MyCommand.Definition -Parent +Set-Location $scriptDirectory + # 获取当前目录路径 $currentPath = Get-Location @@ -45,23 +68,56 @@ $jsCode | Out-File -FilePath $jsFilePath -Encoding UTF8 Write-Output "JavaScript code has been generated and saved to $jsFilePath" # 设置NAPCAT_PATH环境变量为 当前目录的loadScript.js地址 $env:NAPCAT_PATH = $jsFilePath -for ($i = 0; $i -lt $args.Length; $i++) { - $param = $args[$i] - if ($args[$i] -eq "-q" -and $i + 1 -lt $args.Length) { - $nextParam = $args[$i + 1] - $params = "$param $nextParam" - break - } -} + +$params = $args -join " " Try { $QQpath = Get-QQpath } Catch { $QQpath = Select-QQPath } - if (!(Test-Path $QQpath)) { - throw "provided QQ path is invalid: $QQpath" +# 拿不到QQ路径则退出 +if (!(Test-Path $QQpath)) { + Write-Output "provided QQ path is invalid: $QQpath" + Read-Host "Press any key to continue..." + exit } $commandInfo = Get-Command $QQpath -ErrorAction Stop -Start-Process powershell -ArgumentList "-noexit", "-noprofile", "-command &{& '$($commandInfo.Path)' --enable-logging $params}" + +# 收集dbghelp.dll路径和HASH信息 +$QQpath = Split-Path $QQpath +$oldDllPath = Join-Path $QQpath "dbghelp.dll" +$oldDllHash = Get-FileHash $oldDllPath -Algorithm MD5 +$newDllPath = Join-Path $currentPath "dbghelp.dll" +$newDllHash = Get-FileHash $newDllPath -Algorithm MD5 +# 如果文件一致则跳过 +if ($oldDllHash.Hash -ne $newDllHash.Hash) { + $processes = Get-Process -Name QQ -ErrorAction SilentlyContinue + if ($processes) { + # 文件占用则退出 + Write-Output "dbghelp.dll is in use by the following processes:" + $processes | ForEach-Object { Write-Output "$($_.Id) $($_.Name) $($_.Path)" } + Write-Output "dbghelp.dll is in use, cannot continue." + Read-Host "Press any key to continue..." + exit + } else { + # 文件未占用则尝试覆盖 + try { + Copy-Item -Path "$newDllPath" -Destination "$oldDllPath" -Force + Write-Output "dbghelp.dll has been copied to $QQpath" + } catch { + Write-Output "Failed to copy dbghelp.dll: $_" + Read-Host "Press any key to continue..." + exit + } + } +} + +# 带参数启动QQ +try { + Start-Process powershell -ArgumentList '-noexit', '-noprofile', "-command &{& chcp 65001;& '$($commandInfo.Path)' --enable-logging $params}" -NoNewWindow -ErrorAction Stop +} catch { + Write-Output "Failed to start process as administrator: $_" + Read-Host "Press any key to continue..." +} \ No newline at end of file