-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathcompress_idr.ps1
More file actions
121 lines (98 loc) · 4.98 KB
/
Copy pathcompress_idr.ps1
File metadata and controls
121 lines (98 loc) · 4.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
[CmdletBinding()]
param(
[string]$InputSys = "$PSScriptRoot\rsc\kvc.sys",
[string]$OutputFile = "$PSScriptRoot\src\IDR_DRV1",
[string]$InputBbs = "$PSScriptRoot\rsc\bbs.exe",
[string]$OutputFile2 = "$PSScriptRoot\src\IDR_DRV2",
[string]$SetupManager = "$PSScriptRoot\src\SetupManager.c"
)
Set-StrictMode -Version 3.0
$ErrorActionPreference = "Stop"
Add-Type -TypeDefinition @"
using System;
using System.Runtime.InteropServices;
public static class Lznt1 {
const ushort COMPRESSION_FORMAT_LZNT1 = 2;
const ushort COMPRESSION_ENGINE_MAXIMUM = 0x0100;
[DllImport("ntdll.dll")]
static extern int RtlGetCompressionWorkSpaceSize(
ushort CompressionFormatAndEngine,
out uint CompressBufferWorkSpaceSize,
out uint CompressFragmentWorkSpaceSize);
[DllImport("ntdll.dll")]
static extern int RtlCompressBuffer(
ushort CompressionFormatAndEngine,
byte[] UncompressedBuffer,
uint UncompressedBufferSize,
byte[] CompressedBuffer,
uint CompressedBufferSize,
uint UncompressedChunkSize,
out uint FinalCompressedSize,
IntPtr WorkSpace);
public static byte[] Compress(byte[] input) {
ushort format = COMPRESSION_FORMAT_LZNT1 | COMPRESSION_ENGINE_MAXIMUM;
uint wsSize, fragSize;
int status = RtlGetCompressionWorkSpaceSize(format, out wsSize, out fragSize);
if (status != 0) throw new Exception(string.Format("RtlGetCompressionWorkSpaceSize: 0x{0:X8}", (uint)status));
IntPtr ws = Marshal.AllocHGlobal((int)wsSize);
try {
byte[] outBuf = new byte[input.Length + 4096];
uint finalSize;
status = RtlCompressBuffer(format, input, (uint)input.Length,
outBuf, (uint)outBuf.Length,
4096, out finalSize, ws);
if (status != 0) throw new Exception(string.Format("RtlCompressBuffer: 0x{0:X8}", (uint)status));
byte[] result = new byte[finalSize];
Array.Copy(outBuf, result, (int)finalSize);
return result;
} finally {
Marshal.FreeHGlobal(ws);
}
}
}
"@
[byte[]]$XorKey = 0xA0, 0xE2, 0x80, 0x8B, 0xE2, 0x80, 0x8C
# ---------------------------------------------------------------------------
# IDR_DRV1 -- kvc.sys
# ---------------------------------------------------------------------------
$raw = [System.IO.File]::ReadAllBytes((Resolve-Path $InputSys).Path)
$compressed = [Lznt1]::Compress($raw)
for ($i = 0; $i -lt $compressed.Length; $i++) {
$compressed[$i] = $compressed[$i] -bxor $XorKey[$i % $XorKey.Length]
}
[System.IO.File]::WriteAllBytes($OutputFile, $compressed)
$ratio = [math]::Round((1 - $compressed.Length / $raw.Length) * 100, 1)
Write-Host "--- IDR_DRV1 (kvc.sys) ---" -ForegroundColor Cyan
Write-Host "Input : $($raw.Length) bytes ($InputSys)" -ForegroundColor Cyan
Write-Host "Output : $($compressed.Length) bytes ($OutputFile) [-$ratio%]" -ForegroundColor Green
Write-Host ""
# ---------------------------------------------------------------------------
# IDR_DRV2 -- bbs.exe
# ---------------------------------------------------------------------------
$rawBbs = [System.IO.File]::ReadAllBytes((Resolve-Path $InputBbs).Path)
$compressedBbs = [Lznt1]::Compress($rawBbs)
for ($i = 0; $i -lt $compressedBbs.Length; $i++) {
$compressedBbs[$i] = $compressedBbs[$i] -bxor $XorKey[$i % $XorKey.Length]
}
[System.IO.File]::WriteAllBytes($OutputFile2, $compressedBbs)
$ratioBbs = [math]::Round((1 - $compressedBbs.Length / $rawBbs.Length) * 100, 1)
Write-Host "--- IDR_DRV2 (bbs.exe) ---" -ForegroundColor Cyan
Write-Host "Input : $($rawBbs.Length) bytes ($InputBbs)" -ForegroundColor Cyan
Write-Host "Output : $($compressedBbs.Length) bytes ($OutputFile2) [-$ratioBbs%]" -ForegroundColor Green
Write-Host ""
# ---------------------------------------------------------------------------
# Patch SetupManager.c — replace only the numeric value on each #define line,
# preserving the existing whitespace alignment between the name and the value.
# ---------------------------------------------------------------------------
$smPath = (Resolve-Path $SetupManager).Path
$content = [System.IO.File]::ReadAllText($smPath)
$content = $content -replace '(#define kvc_SIZE\s+)\d+', "`${1}$($compressed.Length)"
$content = $content -replace '(#define kvc_UNCOMPRESSED_SIZE\s+)\d+', "`${1}$($raw.Length)"
$content = $content -replace '(#define bbs_SIZE\s+)\d+', "`${1}$($compressedBbs.Length)"
$content = $content -replace '(#define bbs_UNCOMPRESSED_SIZE\s+)\d+', "`${1}$($rawBbs.Length)"
[System.IO.File]::WriteAllText($smPath, $content)
Write-Host "SetupManager.c updated:" -ForegroundColor Yellow
Write-Host " kvc_SIZE = $($compressed.Length)"
Write-Host " kvc_UNCOMPRESSED_SIZE = $($raw.Length)"
Write-Host " bbs_SIZE = $($compressedBbs.Length)"
Write-Host " bbs_UNCOMPRESSED_SIZE = $($rawBbs.Length)"