pe-library provides parsing and generating Portable Executable (known as Windows Executables) binaries.
Warning
This library treats raw binary data for executables. Specifying untrusted binary (such as unchecked user inputs) may increase security risks.
import * as PE from 'pe-library';
import * as fs from 'fs';
// load and parse data
let data = fs.readFileSync('MyApp.exe');
// (the Node.js Buffer instance can be specified directly to NtExecutable.from)
let exe = PE.NtExecutable.from(data);
// get section data
let exportSection = exe.getSectionByEntry(PE.Format.ImageDirectoryEntry.Export);
// read binary data stored in exportSection.data ...
// to write binary: use exe.setSectionByEntry
// write to buffer
let newBin = exe.generate();
fs.writeFileSync('MyApp_modified.exe', new Buffer(newBin));Starting from v1.0.0, CommonJS support is changed; you must use Node.js v20.19.0 or later, or use
pe-library/cjsto use from CommonJS file.
For CommonJS with Node.js v20.19.0 or later:
const PE = require('pe-library');
...For CommonJS with using pe-library/cjs:
const { load } = require('pe-library/cjs');
const fs = require('fs');
load().then((PE) => {
// load and parse data
let data = fs.readFileSync('MyApp.exe');
// (the Node.js Buffer instance can be specified directly to NtExecutable.from)
let exe = PE.NtExecutable.from(data);
// get section data
let exportSection = exe.getSectionByEntry(
PE.Format.ImageDirectoryEntry.Export
);
// read binary data stored in exportSection.data ...
// to write binary: use exe.setSectionByEntry
// write to buffer
let newBin = exe.generate();
fs.writeFileSync('MyApp_modified.exe', new Buffer(newBin));
});// you can use PE namespace for type-reference only
import { type PE, load } from 'pe-library/cjs';
load().then((pe: typeof PE) => {
...
});- All programs / source codes / binaries in this package, EXCEPT FOLLOWINGS, are licensed with MIT License.
- The followings are licensed with 0-BSD license:
- tools/dos-stub/dos-stub.asm
- The bit code, generated from tools/dos-stub/dos-stub.asm, written in src/main/util/generate.ts as
DOS_STUB_PROGRAM