Created
June 13, 2019 12:41
-
-
Save itsho/de4a7a952e5570c99d5c6a7cc889b8bc to your computer and use it in GitHub Desktop.
Run Themida on multiple files with multiple instances of Themida.exe
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Enable -Verbose option | |
[CmdletBinding()] | |
Param( | |
[string]$dllsFolder = "$PSScriptRoot", | |
[string]$txtWithFileList = "$PSScriptRoot\ListOfFilesToUseInThemida.txt", | |
[string]$themida32Path = "C:\Program Files\Themida\Themida.exe", | |
[string]$themidaProject = "D:\ThemidaProject.tmd", | |
[int]$totalProcesses = 8 | |
) | |
Write-Output "Starting Themida script..." | |
function ProtectSingleFile { | |
Param([string]$fileFullPath) | |
if ((Test-Path -Path $fileFullPath -ErrorAction SilentlyContinue) -eq $false) { | |
Write-Output "WARN: File $fileFullPath does not exists. skipping file." | |
return | |
} | |
Write-Output "INFO: Protecting file $fileFullPath..." | |
Write-Output "themida32Path $Using:themida32Path" | |
Write-Output "themidaProject $Using:themidaProject" | |
Write-Output "dllsFolder $Using:dllsFolder" | |
# if Themida have created a log file previously - we can delete it since it's from the previous try | |
$themidaLogFile = "$fileFullPath.log" | |
Remove-Item -path $themidaLogFile -Confirm:$false -Force -ErrorAction Ignore | |
$themidaArgsList = New-Object System.Collections.Generic.List[System.String] | |
$themidaArgsList.Add("/protect") | |
$themidaArgsList.Add("""$Using:themidaProject""") | |
$themidaArgsList.Add("/inputfile") | |
$themidaArgsList.Add("""$fileFullPath""") | |
$themidaArgsList.Add("/outputfile") | |
$themidaArgsList.Add("""$fileFullPath""") | |
$themidaArgsList.Add("/shareconsole") | |
$themidaArgsList.Add("/compactlog") | |
$themidaArgsArray = $themidaArgsList.ToArray() | |
Write-Output "Running themida with ($themidaArgsArray)..." | |
$pinfo = New-Object System.Diagnostics.ProcessStartInfo | |
$pinfo.FileName = "$Using:themida32Path" | |
$pinfo.WorkingDirectory = "$Using:dllsFolder" | |
$pinfo.CreateNoWindow = $true; | |
$pinfo.RedirectStandardError = $true | |
$pinfo.RedirectStandardOutput = $true | |
$pinfo.UseShellExecute = $false | |
$pinfo.Arguments = $themidaArgsArray | |
$p = New-Object System.Diagnostics.Process | |
$p.StartInfo = $pinfo | |
$p.Start() | |
$p.WaitForExit() | |
# 0 protection was successful. | |
if ($p.ExitCode -eq 0) { | |
Write-Output "INFO: file '$fileFullPath' was successfully protected." | |
} | |
# 1 Project file does not exist or invalid. | |
elseif ($p.ExitCode -eq 1) { | |
Write-Error "ERROR: file '$fileFullPath': Project file does not exist or invalid." | |
} | |
# 2 File to protect cannot be opened. | |
elseif ($p.ExitCode -eq 2) { | |
Write-Error "ERROR: file '$fileFullPath' cannot be opened." | |
} | |
# 3 File already protected. | |
elseif ($p.ExitCode -eq 3) { | |
Write-Output "INFO: file '$fileFullPath' is already protected." | |
} | |
# 4 Error in inserted SecureEngine macros. | |
elseif ($p.ExitCode -eq 4) { | |
Write-Error "ERROR: file '$fileFullPath' Error in inserted SecureEngine macros." | |
} | |
# 5 Fatal error while protecting file. | |
elseif ($p.ExitCode -eq 5) { | |
Write-Error "ERROR: file '$fileFullPath': Fatal error while protecting file." | |
} | |
# 6 Cannot write protected file to disk. | |
elseif ($p.ExitCode -eq 6) { | |
Write-Error "ERROR: file '$fileFullPath' Cannot write protected file to disk." | |
} | |
else { | |
Write-Error "ERROR: Failed to protect '$fileFullPath'. Exit code: $($p.ExitCode)" | |
# get themida-log-file if exist | |
if ((Test-Path -Path "$themidaLogFile" -ErrorAction SilentlyContinue) -eq $true) { | |
Write-Output (Get-Content -Path "$themidaLogFile" -Raw) | |
} | |
} | |
} | |
workflow ThemidaProtectFiles { | |
Param( | |
[string]$dllsFolder, | |
[string]$themida32Path, | |
[string]$txtWithFileList, | |
[string]$themidaProject, | |
[int]$totalProcesses) | |
Write-Output "Reading file $txtWithFileList..." | |
# protect files with themida | |
$filesList = Get-Content -Path $txtWithFileList | |
# Run 8 threads at max. it's possible that we can increase the number. | |
ForEach -Parallel -throttlelimit $totalProcesses ($file in $filesList) { | |
ProtectSingleFile -fileFullPath "$dllsFolder\$file" | |
} | |
} | |
ThemidaProtectFiles -dllsFolder "$dllsFolder" -themida32Path "$themida32Path" -txtWithFileList "$txtWithFileList" -themidaProject "$themidaProject" -totalProcesses $totalProcesses |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment