Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
18 changes: 18 additions & 0 deletions docs/handlers.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
| [`NETGEAR TRX V2`](#netgear-trx-v2) | ARCHIVE | :octicons-check-16: |
| [`NTFS`](#ntfs) | FILESYSTEM | :octicons-check-16: |
| [`PARTCLONE`](#partclone) | ARCHIVE | :octicons-check-16: |
| [`PE`](#pe) | EXECUTABLE | :octicons-check-16: |
| [`QNAP NAS`](#qnap-nas) | ARCHIVE | :octicons-check-16: |
| [`RAR`](#rar) | ARCHIVE | :octicons-alert-fill-12: |
| [`ROMFS`](#romfs) | FILESYSTEM | :octicons-check-16: |
Expand Down Expand Up @@ -810,6 +811,23 @@

- [Partclone GitHub Repository](https://github.com/Thomas-Tsai/partclone){ target="_blank" }
- [Clonezilla Official Documentation](https://clonezilla.org/){ target="_blank" }
## pe

!!! success "Fully supported"

=== "Description"

The PE (Portable Executable) is a binary file format used for executable code on 32-bit and 64-bit Windows operating systems as well as in UEFI environments.

---

- **Handler type:** Executable
- **Vendor:** Microsoft

=== "References"

- [PE Format](https://learn.microsoft.com/en-us/windows/win32/debug/pe-format){ target="_blank" }
- [Portable Executable Wikipedia](https://en.wikipedia.org/wiki/Portable_Executable){ target="_blank" }
## QNAP NAS

!!! success "Fully supported"
Expand Down
3 changes: 2 additions & 1 deletion python/unblob/handlers/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
zlib,
zstd,
)
from .executable import elf
from .executable import elf, pe
from .filesystem import (
cramfs,
extfs,
Expand Down Expand Up @@ -115,6 +115,7 @@
zstd.ZSTDHandler,
elf.ELF32Handler,
elf.ELF64Handler,
pe.PEHandler,
zlib.ZlibHandler,
engenius.EngeniusHandler,
ecc.AutelECCHandler,
Expand Down
104 changes: 104 additions & 0 deletions python/unblob/handlers/executable/pe.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
import io
from pathlib import Path
from typing import Optional

import lief
from structlog import get_logger

from unblob.extractors.command import Command

from ...models import (
Extractor,
ExtractResult,
File,
Handler,
HandlerDoc,
HandlerType,
HexString,
Reference,
ValidChunk,
)

lief.logging.disable()

logger = get_logger()


class PEExtractor(Extractor):
def extract(self, inpath: Path, outdir: Path) -> Optional[ExtractResult]:
binary = lief.PE.parse(inpath)
if binary and self.is_nsis(binary):
return Command("7z", "x", "-y", "{inpath}", "-o{outdir}").extract(
inpath, outdir
)
return None

def is_nsis(self, binary: lief.PE.Binary) -> bool:
# Test if binary appears to be a Nullsoft Installer self-extracting archive
# see https://github.com/file/file/blob/7ed3febfcd616804a2ec6495b3e5f9ccb6fc5f8f/magic/Magdir/msdos#L383

if binary.has_resources:
resource_manager = binary.resources_manager
if (
isinstance(resource_manager, lief.PE.ResourcesManager)
and resource_manager.has_manifest
):
manifest = (
resource_manager.manifest
if isinstance(resource_manager.manifest, str)
else resource_manager.manifest.decode(errors="ignore")
)
if "Nullsoft.NSIS.exehead" in manifest:
return True
return False


class PEHandler(Handler):
NAME = "pe"

PATTERNS = [
HexString(
"""
// MZ header
4d 5a
"""
),
HexString(
"""
// PE header
50 45 00 00
"""
),
]

EXTRACTOR = PEExtractor()

DOC = HandlerDoc(
name="pe",
description="The PE (Portable Executable) is a binary file format used for executable code on 32-bit and 64-bit Windows operating systems as well as in UEFI environments.",
handler_type=HandlerType.EXECUTABLE,
vendor="Microsoft",
references=[
Reference(
title="PE Format",
url="https://learn.microsoft.com/en-us/windows/win32/debug/pe-format",
),
Reference(
title="Portable Executable Wikipedia",
url="https://en.wikipedia.org/wiki/Portable_Executable",
),
],
limitations=[],
)

def calculate_chunk(self, file: File, start_offset: int) -> Optional[ValidChunk]:
file.seek(start_offset, io.SEEK_SET)

binary = lief.PE.parse(file[start_offset:])
if not binary:
return None

return ValidChunk(
start_offset=start_offset,
end_offset=start_offset + binary.original_size,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

original_size is the file size on disk, not the actual PE file size. Samples with suffix are carved with the suffix, which is incorrect. I'm looking into it.

)
3 changes: 3 additions & 0 deletions tests/integration/executable/pe/__input__/nsis-3.11-setup.exe
Git LFS file not shown
Git LFS file not shown
3 changes: 3 additions & 0 deletions tests/integration/executable/pe/__input__/zip2exe.exe
Git LFS file not shown
3 changes: 3 additions & 0 deletions tests/integration/executable/pe/__input__/zip2exe.exe.padded
Git LFS file not shown
Git LFS file not shown
Git LFS file not shown
Git LFS file not shown
Git LFS file not shown
Git LFS file not shown
Git LFS file not shown
Git LFS file not shown
Git LFS file not shown
Git LFS file not shown
Git LFS file not shown
Git LFS file not shown
Git LFS file not shown
Git LFS file not shown
Git LFS file not shown
Git LFS file not shown
Git LFS file not shown
Git LFS file not shown
Git LFS file not shown
Git LFS file not shown
Git LFS file not shown
Git LFS file not shown
Git LFS file not shown
Git LFS file not shown
Git LFS file not shown
Git LFS file not shown
Git LFS file not shown
Git LFS file not shown
Git LFS file not shown
Git LFS file not shown
Git LFS file not shown
Git LFS file not shown
Git LFS file not shown
Git LFS file not shown
Git LFS file not shown
Git LFS file not shown
Git LFS file not shown
Git LFS file not shown
Git LFS file not shown
Git LFS file not shown
Git LFS file not shown
Git LFS file not shown
Git LFS file not shown
Git LFS file not shown
Git LFS file not shown
Git LFS file not shown
Git LFS file not shown
Git LFS file not shown
Git LFS file not shown
Git LFS file not shown
Git LFS file not shown
Git LFS file not shown
Git LFS file not shown
Git LFS file not shown
Git LFS file not shown
Git LFS file not shown
Git LFS file not shown
Git LFS file not shown
Loading
Loading