第11章 命令行使用

第11章:命令行使用

11.1 命令行概述

.NET Reactor 提供强大的命令行界面(CLI),支持自动化和批处理操作。

11.2 基本语法

dotNET_Reactor.Console.exe [options] -file <input> [-targetfile <output>]

11.3 常用参数

11.3.1 输入输出

# 指定输入文件
-file MyApp.exe

# 指定输出文件
-targetfile MyApp.Protected.exe

# 使用项目文件
-project MyProject.nrproj

# 静默模式
-quiet

# 详细输出
-verbose

11.3.2 保护选项

# 启用混淆
-obfuscation 1

# 设置混淆级别
-obfuscation_level standard

# 启用字符串加密
-stringencryption 1

# 启用 NecroBit
-necrobit 1

# 启用反调试
-antidebug 1

# 启用防篡改
-antitampering 1

11.4 批处理脚本

11.4.1 Windows 批处理

@echo off
setlocal

set REACTOR="C:\Program Files (x86)\Eziriz\.NET Reactor\dotNET_Reactor.Console.exe"
set INPUT_DIR=bin\Release
set OUTPUT_DIR=bin\Protected

REM 创建输出目录
if not exist "%OUTPUT_DIR%" mkdir "%OUTPUT_DIR%"

REM 保护所有 DLL 和 EXE
for %%F in ("%INPUT_DIR%\*.exe" "%INPUT_DIR%\*.dll") do (
    echo Protecting %%F...
    %REACTOR% -file "%%F" -targetfile "%OUTPUT_DIR%\%%~nxF" -quiet
    
    if errorlevel 1 (
        echo Failed to protect %%F
        exit /b 1
    )
)

echo All files protected successfully!
endlocal

11.4.2 PowerShell 脚本

# Protect-Assemblies.ps1

param(
    [Parameter(Mandatory=$true)]
    [string]$InputPath,
    
    [Parameter(Mandatory=$true)]
    [string]$OutputPath,
    
    [Parameter(Mandatory=$false)]
    [string]$ProjectFile = "",
    
    [Parameter(Mandatory=$false)]
    [switch]$NecroBit,
    
    [Parameter(Mandatory=$false)]
    [switch]$Verbose
)

$reactorPath = "C:\Program Files (x86)\Eziriz\.NET Reactor\dotNET_Reactor.Console.exe"

# 获取所有程序集
$assemblies = Get-ChildItem -Path $InputPath -Filter "*.exe", "*.dll"

foreach ($assembly in $assemblies) {
    Write-Host "Protecting $($assembly.Name)..." -ForegroundColor Cyan
    
    $args = @(
        "-file", $assembly.FullName,
        "-targetfile", (Join-Path $OutputPath $assembly.Name)
    )
    
    if ($ProjectFile) {
        $args += @("-project", $ProjectFile)
    }
    
    if ($NecroBit) {
        $args += "-necrobit", "1"
    }
    
    if (-not $Verbose) {
        $args += "-quiet"
    }
    
    & $reactorPath $args
    
    if ($LASTEXITCODE -ne 0) {
        Write-Error "Failed to protect $($assembly.Name)"
        exit 1
    }
}

Write-Host "All assemblies protected successfully!" -ForegroundColor Green

11.5 CI/CD 集成

11.5.1 Azure DevOps

# azure-pipelines.yml
steps:
- task: PowerShell@2
  displayName: 'Protect Assemblies'
  inputs:
    targetType: 'filePath'
    filePath: 'scripts/Protect-Assemblies.ps1'
    arguments: >
      -InputPath "$(Build.BinariesDirectory)"
      -OutputPath "$(Build.ArtifactStagingDirectory)/protected"
      -ProjectFile "protection.nrproj"
      -NecroBit

11.5.2 GitHub Actions

# .github/workflows/protect.yml
- name: Protect with .NET Reactor
  run: |
    $reactor = "C:\Program Files (x86)\Eziriz\.NET Reactor\dotNET_Reactor.Console.exe"
    & $reactor -file "bin\Release\MyApp.exe" `
               -targetfile "protected\MyApp.exe" `
               -project "protection.nrproj" `
               -quiet
  shell: powershell

11.5.3 Jenkins Pipeline

// Jenkinsfile
stage('Protect Assemblies') {
    steps {
        bat '''
            "C:\\Program Files (x86)\\Eziriz\\.NET Reactor\\dotNET_Reactor.Console.exe" ^
            -file "bin\\Release\\MyApp.exe" ^
            -targetfile "protected\\MyApp.exe" ^
            -project "protection.nrproj" ^
            -quiet
        '''
    }
}

11.6 高级用法

11.6.1 条件保护

# 根据环境变量决定保护级别
if [ "$BUILD_CONFIGURATION" = "Release" ]; then
    NECROBIT="-necrobit 1"
else
    NECROBIT=""
fi

dotNET_Reactor.Console.exe \
    -file MyApp.exe \
    -obfuscation 1 \
    $NECROBIT \
    -targetfile MyApp.Protected.exe

11.6.2 并行处理

# 并行保护多个程序集
$assemblies = Get-ChildItem -Path "bin\Release" -Filter "*.dll"

$assemblies | ForEach-Object -Parallel {
    $reactor = "C:\Program Files (x86)\Eziriz\.NET Reactor\dotNET_Reactor.Console.exe"
    & $reactor -file $_.FullName -targetfile "protected\$($_.Name)" -quiet
} -ThrottleLimit 4

11.7 错误处理

11.7.1 退出代码

0  - 成功
1  - 一般错误
2  - 文件未找到
3  - 许可证无效
4  - 配置错误
5  - 保护失败

11.7.2 日志记录

# 启用日志
dotNET_Reactor.Console.exe \
    -file MyApp.exe \
    -log protection.log \
    -verbose

# 检查日志
if grep -q "ERROR" protection.log; then
    echo "Protection failed!"
    exit 1
fi

11.8 本章小结

本章介绍了 .NET Reactor 的命令行使用方法,包括:

  • 基本命令语法
  • 常用参数选项
  • 批处理脚本编写
  • CI/CD 集成方案
  • 错误处理机制

掌握命令行工具可以实现自动化保护流程,提高开发效率。

posted @ 2025-12-20 13:37  我才是银古  阅读(1)  评论(0)    收藏  举报