-
Notifications
You must be signed in to change notification settings - Fork 366
Expand file tree
/
Copy pathpetozip.ts
More file actions
124 lines (100 loc) · 3.6 KB
/
Copy pathpetozip.ts
File metadata and controls
124 lines (100 loc) · 3.6 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
122
123
124
// file: petozip.ts
// npm install pe-library jszip buffer
import type { FileData, FileFormat, FormatHandler } from "../FormatHandler.ts";
import * as Pe from "pe-library";
import JSZip from "jszip";
import { Buffer } from "buffer";
import CommonFormats, { Category } from "src/CommonFormats.ts";
if (typeof window !== "undefined") {
(window as any).Buffer = Buffer;
}
class peToZipHandler implements FormatHandler {
public name: string = "petozip";
public supportedFormats: FileFormat[] = [
CommonFormats.EXE.builder("exe").allowFrom(),
{
name: "Dynamic-Link Library",
format: "dll",
extension: "dll",
mime: "application/vnd.microsoft.portable-executable",
from: true,
to: false,
internal: "dll",
category: Category.CODE,
lossless: false
},
CommonFormats.ZIP.builder("zip").allowTo().markLossless()
];
public ready: boolean = true;
async init() {
this.ready = true;
}
async doConvert(
inputFiles: FileData[],
inputFormat: FileFormat,
outputFormat: FileFormat
): Promise<FileData[]> {
if (outputFormat.format !== "zip") {
throw new TypeError(`Unsupported output format of ${outputFormat.format}. Only ZIP is supported.`);
}
const outputFiles: FileData[] = [];
for (const inputFile of inputFiles) {
try {
//initialize JSzip + buffer
const zip = new JSZip();
const buffer = Buffer.from(inputFile.bytes as Uint8Array);
// Get metadata from PE headers
//@ts-ignore
const peFile = Pe.NtExecutable.from(buffer);
const ntHeader = peFile.newHeader;
const subsystemValue = ntHeader.optionalHeader.subsystem;
const subsystemMap: Record<number, string> = {
1: "Native",
2: "Windows GUI",
3: "Windows Console",
7: "POSIX",
9: "Windows CE",
10: "EFI Application"
};
const metadata = {
originalFileName: inputFile.name,
architecture: peFile.is32bit() ? "x86 (32-bit)" : "x64 (64-bit)",
compileTimestamp: new Date(ntHeader.fileHeader.timeDateStamp * 1000).toISOString(),
subsystem: subsystemMap[subsystemValue] || `Unknown (${subsystemValue})`,
imageBase: peFile.getImageBase(),
sectionAlignment: peFile.getSectionAlignment(),
imports: [] as any[],
exports: [] as any[]
};
zip.file("metadata.json", JSON.stringify(metadata, null, 2));
// Extract binary
const allSections = peFile.getAllSections();
for (const section of allSections) {
const rawName = section.info.name.toString().replace(/\0/g, '');
const safeName = rawName.replace(/[^a-zA-Z0-9]/g, '');
const fileName = `section_${safeName || 'unnamed'}.bin`;
if (section.data) {
zip.file(fileName, section.data);
}
}
// generate final ZIP
const outputBytes = await zip.generateAsync({
type: "uint8array",
compression: "DEFLATE",
compressionOptions: { level: 9 }
});
const baseName = inputFile.name.split(".").slice(0, -1).join(".");
const newName = `${baseName}_pe_data.zip`;
outputFiles.push({
bytes: outputBytes,
name: newName
});
} catch (e: any) { // error handling
console.error(`[petozip] Error converting ${inputFile.name}:`, e);
throw new Error(`Failed to process PE file ${inputFile.name}: ${e.message}`);
}
}
return outputFiles;
}
}
export default peToZipHandler;