Recovering command history from memory: cmd, PowerShell and bash
6 min read
The single most useful question in an intrusion is "what did they type?". Shell history files are the obvious answer and the first thing an attacker deletes. Memory holds more: the shell's own in-memory history, the console host's heap, process command lines, and loose command text. Here is each source ramparser reads, how it reads it, and how far to trust it.
Windows: cmdscan on conhost heaps (experimental)
The Art of Memory Forensics describes cmdscan as a search for
_COMMAND_HISTORY structures in the console host process (csrss.exe
before Windows 7, conhost.exe after). The layout of that structure
changes between builds, so ramparser does not parse it yet, and the
plugin is labelled experimental.
It goes after the same evidence another way. For each conhost.exe,
it walks the process's _MMVAD tree, keeps private, non-executable
regions up to 8 MiB, and reads them through the process's page tables.
In those heaps it extracts UTF-16LE strings of 4 to 256 printable
characters and keeps the ones that start with a known shell command or
builtin: cmd, powershell, pwsh, net , wmic , schtasks,
reg , sc , certutil, bitsadmin, rundll32, mshta,
whoami, ipconfig, copy , del , cd and so on. Each row gives
the conhost PID and the command.
What you lose compared to a structure parse: the order of commands and which application the history belonged to. A command that does not start with one of the known names is not reported. When the plugin returns nothing, the UI says why: no conhost, or nothing matched, and the structure scan is not implemented. It needs the kernel PDB for the VAD walk.
consoles: presence, not screen buffers
Volatility's consoles rebuilds _CONSOLE_INFORMATION screen
buffers, including command output. ramparser's consoles plugin is
much narrower. It is an on-demand string-marker sweep for console
subsystem residue: conhost.exe and OpenConsole.exe names, \Device\ConDrv paths,
console API names, and history-related field names. It tells you
console activity left traces. It does not rebuild screen contents.
cmdhistory: a sweep for attacker command lines
cmdhistory (on demand) ignores structures altogether. It sweeps
physical memory for about thirty-five high-signal command prefixes at
a line boundary (the previous byte is a NUL or a newline):
- PowerShell launches (
powershell -,pwsh -),Invoke-WebRequest,Invoke-Expression,DownloadString - Defender tampering:
Add-MpPreference,Set-MpPreference - Lateral movement:
wmic process call create,wmic /node:,psexec - Recon and accounts:
whoami /priv,net user /add,net group "Domain Admins" - Persistence and LOLBins:
schtasks /create,bitsadmin /transfer,certutil -urlcache,regsvr32 /s /n /u,mshta,reg add - Anti-forensics:
vssadmin delete,wevtutil cl
Each hit is captured up to the end of the line or 512 bytes and deduplicated, with its physical offset and a family label. The sweep matches single-byte text only, and it has no process context, so a row can come from a script in the file cache, a PowerShell transcript or a log buffer. That is why the Activity view does not list these rows as commands. They show up as a "Command fragments" trace: proof that such command text was in memory, to confirm with other sources.
Process command lines and environment
The most reliable "what was run" record is still the command line of
each process. With the kernel PDB, ramparser reads
PEB → ProcessParameters (_RTL_USER_PROCESS_PARAMETERS) in each
process's own address space: CommandLine, the image path, and the
Environment block. On Linux with a kernel symbol file, the snapshot
reads mm_struct.arg_start/arg_end and env_start/env_end from the
task's user memory. On Linux, a paged-out argument or environment
page yields an empty value rather than garbage.
The command line appears in the process view and as the detail of the "started" event on the timeline, often in full when no console history survives.
Linux: bash history from the heap
This is the linux_bash technique from the book, and it is the
strongest source in this post. bash keeps its history as HIST_ENTRY
structures:
HIST_ENTRY { char *line; char *timestamp; histdata_t data; }
bash timestamps every entry with a string such as #1638763368, even
when history timestamps are never written to disk. ramparser uses that
as an anchor. For every process whose executable is bash:
- It reads the
[heap]mappings (up to 256 MiB) through the process's page tables. - It finds
#+ 10 digits + NUL strings, preceded by a NUL, with an epoch between 2000 and 2100. - It finds 8-byte-aligned pointers to those strings. Each one is a
timestampfield, and the pointer just before it isline. - It reads
lineas a printable C string (up to 4 KiB), then sorts by time and drops duplicates.
The result is a list of commands with times and a PID, recovered even
when ~/.bash_history was never written or was deleted.
There is one subtlety the tool handles explicitly. When bash starts, it loads ~/.bash_history, and every loaded line
gets the same load timestamp. When five or more entries of one shell
share a stamp, ramparser labels them "loaded from ~/.bash_history at
shell start" and gives them no execution time. Only commands typed in
that session get a time.
Linux: the bash hash table
linux_bash_hash recovers the table behind hash -l: for each command
name, the file bash resolved and how many times it was used. ramparser
looks for bash's hashed_filenames HASH_TABLE on the heap (64
buckets, a bucket array inside the heap). It walks every chain and
keeps the table only if the chains hold exactly nentries items, each
with a readable name and an absolute path.
history -c does not touch this table. When a command resolved to /tmp,
/var/tmp, /dev/shm, /run/user/N, a dot-directory or a
(deleted) file, the process gets the reason "Ran programs from a
temporary or hidden folder", with name → path (N×) as evidence.
Alongside the structured recovery, a bash_history sweep runs
automatically. It looks for command-shaped lines (sudo , ssh ,
scp , wget , curl , chmod …) at line starts across physical
memory, and it is the only bash source without kernel symbols. It
gives a physical offset and no PID or time.
macOS
On Intel macOS images with a Volatility 3 symbol file, the structured
profile runs the same HIST_ENTRY recovery on the malloc regions of
each bash process. Alongside it, an automatic line-boundary sweep
looks for
macOS-flavoured commands: launchctl, dscl, csrutil,
defaults write, kextload, spctl, codesign, security, plus
ssh, scp and curl. The structured recovery only runs on
processes named bash. zsh, the default shell since Catalina, is
covered by the sweep alone.
Notepad buffers
The notepad plugin (on demand) is a marker sweep: Untitled-,
NotepadDocument, Notepad++, .scintilla, WordPad and a few
others. For each marker it captures a printable preview of up to about
190 characters. It is a pointer to where an open document was, useful
for notes or pasted credentials, not a rebuilt document.
Activity and timeline
The Activity view's Commands list merges bash in-memory history, the
bash hash table, cmdscan rows, the bash_history sweep, and Linux
audit EXECVE records (from the kernel log, with the CWD record of
the same event, or from journald). Duplicates are counted, not
repeated, and each command says where it came from.
Every command is tagged: downloads from a bare IP, curl | sh,
encoded content, chmod +x, execution from /tmp or %TEMP%,
persistence (crontab, schtasks /create, reg add …\Run), trace
removal (history -c, unset HISTFILE, wevtutil cl), recon, remote
access. Critical and suspicious commands are ranked first, with a
search box and a flagged-only filter.
Commands with a real execution time (bash entries typed in the session
and audit records) are also placed on the timeline in the "Program
runs" lane as ran …, next to process starts, network connections and
log records.