Safe RAR Extraction: Path Traversal, Archive Bombs and Malware
Give a containment checklist covering paths, links, expansion ratios, executables and scanning.
Why RAR Extraction Requires Explicit Containment
RAR archives encode structural metadata that extraction tools obey by default. The RARLAB Technical Note states that RAR 5.0 uses the stored file name to create the extracted file, including any path components. An archive can therefore instruct the extractor to write outside the intended directory. Three properties make this dangerous:
- Absolute path storage. A filename entry such as
C:\Windows\System32\malware.dllor/etc/cron.d/backdoorbypasses the user-chosen destination folder if the tool does not strip the prefix. - Parent directory traversal. Sequences like
..\..\..\Users\Public\autorun.infescape the extraction root. - Symbolic and hard link support. RAR can store symlinks that point to arbitrary filesystem locations. On Unix, extracting a symlink that targets
/etc/passwdand then writing through it can overwrite system files.
A second risk is the expansion ratio. Solid compression treats all files as a single stream, so a 100 KB archive header can conceal gigabytes of decompressed data. Selective extraction is impossible without processing the entire payload.
Third, RAR archives preserve executable bits and can contain scripts, ELF binaries, or Mach-O objects. Extraction tools that offer a post-extraction run option turn a passive archive into an execution vector.
Inspect the Archive Structure Without Decompressing
List contents before touching any file data.
unrar l archive.rar
Examine every line for:
- Absolute paths: Any entry beginning with
/or a Windows drive letter (C:\,D:\). - Traversal sequences:
..\or../anywhere in the stored path. - Symlink indicators: The listing shows
->for symbolic links. A link targeting/etc/shadoworC:\Windows\System32is a redirection attack. - Unexpected executables: Files with extensions
.exe,.sh,.bat,.cmd,.scr,.pifinside directories named to suggest benign content.
If any of these appear, do not extract on a production machine. Delete the archive or move it to an isolated virtual machine with no network access.
Calculate and Enforce an Expansion Ratio Limit
A zip bomb or RAR bomb exploits extreme compressibility. Use unrar v to obtain per-file uncompressed sizes:
unrar v archive.rar
Compute the ratio: total uncompressed bytes divided by total compressed bytes. A ratio above 50:1 is hostile unless you have verified the source. For example, a 1 MB archive claiming 500 MB uncompressed yields a 500:1 ratio.
Solid archives compound the problem. Because all files share one compression stream, extracting even a single small file forces decompression of the entire bomb. Set a hard reject threshold: refuse any archive where uncompressed size exceeds 50× the compressed size. This limit is conservative for text and multimedia but catches deliberate expansion attacks.
Handle Symbolic Links and Hard Links Explicitly
When the listing contains symlinks, never extract as root or with elevated privileges. Use unrar x -o- to prevent overwriting existing files. The -o- flag instructs the extractor to skip any file that already exists, which provides a safety net against traversal overwrites.
On Windows, RAR stores shortcuts as .lnk files. These are less dangerous than Unix symlinks but can still point to executables. Inspect any .lnk file with a shortcut parser before opening.
Scan for Executables Before and After Extraction
Scan the RAR file itself with a tool that understands the container format.
clamscan --infected archive.rar
After extraction into a sandbox directory, scan the output recursively:
clamscan --recursive --infected ~/sandbox/extract_here/
On Windows, use the antivirus right-click scan on both the archive and the sandbox folder. If any file is flagged, delete the sandbox and the original archive. Do not rely on the file extension to identify executable content. Run file on each extracted item:
file extracted_sample.jpg
If the output reports ELF 64-bit LSB executable or Mach-O 64-bit executable, the file is not an image regardless of its name.
Extract into a Disposable Sandbox Directory
Create an empty directory on a non-system volume.
mkdir -p ~/sandbox/extract_here
cd ~/sandbox/extract_here
unrar x -o- ../archive.rar
After extraction, verify that no files appeared outside the target:
ls -laR ~/sandbox/
If files exist in ~/sandbox/ but not inside extract_here/, the archive used absolute paths and the tool honored them. On Windows, use a path like D:\sandbox\. If the archive attempts to write to C:\, the operation will fail only if the tool strips the drive prefix—do not assume this behavior is consistent across tools.
Disable Post-Extraction Execution
WinRAR and some graphical unarchivers include a “Run after extraction” option. Ensure it is unchecked. Do not double-click any extracted file until the individual scan completes. On macOS, quarantine flags (com.apple.quarantine) may block execution, but this depends on the download source and is not a reliable control.
What Extraction Cannot Recover
Intentionally corrupted RAR files designed to crash extractors cannot be repaired by extraction tools. The RAR recovery record (.rev files) protects against bit rot, not malicious corruption. Password-protected archives with long, random keys are computationally infeasible to brute-force. Converting a RAR to another format such as ZIP does not strip path-traversal entries; the malicious filenames are part of the archive’s file tree and survive conversion. The only safe path is inspection and rejection at the RAR level.
Verify Archive Identity Before Trusting the Filename
A file named invoice.pdf that begins with the RAR signature bytes 0x52 0x61 0x72 0x21 (Rar!) is a RAR archive, not a PDF. Use file on Unix or inspect the header with a hex editor on Windows. Never rely on the extension or icon.