Files
reverse-engineering-anything/docs/guides/artifacts/mql-programs.md
T
morluto a37994b3e8 feat(artifacts): inspect MetaTrader EX4 and EX5 program headers and resources (#16501)
* feat(artifacts): inspect MetaTrader 5 EX5 program headers and resources

Recognize compiled MQL5 programs by their EX5 magic and inventory them
through inspect_artifact and extract_artifact. Report the program type,
compiler build, copyright/link/version property slots, nonzero header
fields of unknown meaning, and the resource table. List the opaque body
and resource payloads as stored byte ranges; nothing is decrypted,
decompressed or decompiled.

Refs #16495

* feat(artifacts): read MetaTrader 4 EX4 programs with the EX5 layout

EX4 (magic EX-) shares the EX5 header, property slots and resource
table, so both containers become one mql-program format with a
container field. The version slot is 64 bytes and the opaque body
starts at 0x2E8 in both containers; the earlier 0x3A8 header length
counted 192 opaque bytes as part of the version slot. EX4 type code 3
is a library; build is reported under a neutral name because its EX4
meaning is not established.

* fix(artifacts): bound repeated MQL resource ranges and let tar outrank printable magics

Resource records may share one payload range, and each occurrence is
hashed and extractable, so the reader now charges every record against
artifact_limits.expanded_bytes, as the DTBO reader does.

A checksum-valid tar header now runs before the recognizers that match a
printable magic at offset 0. A tar whose first member was named EX-...,
GDPC... or UnityFS was claimed as an MQL program, Godot PCK or UnityFS.

Slot text keeps a leading U+FEFF, which is declared content rather than
an encoding mark.

* fix(artifacts): refuse an invalid tar header only after leading-magic formats

Only a checksum-valid tar header outranks the printable leading magics. A
Godot pack can hold ustar at 257 inside its own content without a valid
tar checksum; refusing that header first rejected the valid pack.

* fix(artifacts): compare MetaTrader resource declarations

A resource's name, kind, record index and unpacked size live in the
resource table, outside the stored payload that content comparison
hashes, so a renamed resource compared as unchanged. Compare them as
the resource's declaration, as tar, ar and UnityFS members do.

* fix(artifacts): keep unreadable MetaTrader resource names unknown in comparison
2026-12-03 19:16:36 -05:00

5.6 KiB

MetaTrader compiled programs (EX4 and EX5)

inspect-artifact and extract-artifact recognize a MetaTrader 4 compiled MQL4 program (.ex4, magic EX-) and a MetaTrader 5 compiled MQL5 program (.ex5, magic EX5), under any file name. Both share one header and resource table layout and report format mql-program.

REA reads the plaintext header and resource table offline. It reports the program type, the build value, the copyright, link and version properties, and where the code body and embedded resources are stored. It does not decrypt, decompress, disassemble or decompile the body or the resources, and it does not run the program.

rea inspect-artifact ./Expert.ex5 --json
rea extract-artifact ./Indicator.ex4 --json

MetaQuotes does not publish this layout. REA's reading was established from 28 .ex4 programs (build value 2421) and 170 .ex5 programs (builds 3802, 3808 and 6275), with the matching .mq4 and .mq5 sources. Fields whose meaning is not established are reported raw rather than named.

Program facts

Root format_metadata has kind mql-program:

Field Meaning
container ex4 or ex5, from the magic
program_type From the type byte at offset 3; see below. An unobserved code is unknown
program_type_code The type byte as stored
build The u16 at offset 0x06
properties copyright, link and version slots, each with its offset, state and text
body offset and length of the opaque span from 0x2E8 to the resource table, or to the end of the file
resource_count Declared resource records
resource_section offset and length of the resource table and payloads, or null without resources
unknown_header_fields Nonzero header values of unknown meaning, with their offset, width and value; omitted fields are zero

Type codes 1, 2 and 4 are script, expert and indicator in both containers. Code 3 is library in EX4; no EX5 library was examined, so code 3 in EX5 is unknown.

For EX5, build matched the build of the MetaTrader 5 terminal that compiled the samples. For EX4 it was 2421 while the MetaTrader 4 terminal reported build 1476, so its meaning there is not established.

copyright and link are 256-byte UTF-16LE slots at 0xA8 and 0x1A8; version is a 64-byte slot at 0x2A8. A slot's state is declared, empty when the program did not declare it, or malformed when the slot is not well-formed UTF-16LE; text is null unless the state is declared. The header blocks at 0x20 to 0x2F and 0x78 to 0xA7 are opaque and are not reported.

#property description, inputs, function names and other source facts are not in the readable header. Recovering them would require decrypting the body, which REA does not do.

Stored ranges

Inventory lists the body as body.stored and each resource payload as resources/NNNN.stored, in table order. Each entry carries kind mql-program-entry metadata:

Field Meaning
role body or resource
table_index Position in the resource table; null for the body
name Resource name as declared, such as res\Close.bmp; null for the body or when ill-formed
resource_kind Record field 0x0C; 2 for every bitmap observed
record_index Record field 0x10; its ordering is unknown
unpacked_size Declared size before the stored encoding

compare-artifacts compares each resource's name, resource_kind, record_index and unpacked_size, which the resource table declares outside the stored payload, so a changed declaration is reported even when the payload bytes match. The body declares nothing beyond its bytes.

extract-artifact writes the stored bytes unchanged. A resource payload is not the original file: its stored size is smaller than unpacked_size and its encoding is unknown, so it is named .stored rather than after the resource.

Failures and limitations

A file shorter than the 0x2E8-byte header, or a resource table that starts inside the header, ends past the file, or whose declared section does not end at the end of the file, is refused with its offset and field. A payload range that ends past the file is omitted and reported as a limitation, and coverage becomes unknown. A program declaring more than 65,536 resources is refused at the fixed MQL program resource record allowance; see analysis settings, limits, and recovery.