Tag: forensics

  • Memory forensics for responders: what Volatility finds that disk forensics misses

    Memory forensics for responders: what Volatility finds that disk forensics misses

    The first time I watched someone pull a memory image off a compromised box before pulling the plug, I thought it was overkill. The disk was right there. Then the disk turned out to be clean. The implant had never been written to it.

    That story is why I care about memory forensics. Disk forensics tells you what was stored. Memory tells you what was running, what it was talking to, and what it had decrypted. For modern intrusions those are often different answers.

    What lives only in RAM

    Comparison chart of what a disk image and a memory image usually contain: memory covers running processes, injected code, command lines, network connections and kernel modules, disk covers installed files.

    The chart above is a hand-built illustration of typical evidence coverage, not measured data.

    Fileless and reflectively loaded malware is the obvious case. A loader that allocates executable memory, copies a payload in and jumps to it leaves nothing for a disk scanner to hash. Process hollowing works the same way: the binary on disk is still a perfectly normal svchost.exe, but the code running under that name isn’t.

    Other things that exist mainly or only in memory:

    • Command lines and environment variables of running processes, including arguments the attacker passed to a tool before deleting the script.
    • Network connections and listening sockets at the moment of capture, with the owning process.
    • Decrypted content. Packed payloads are plaintext once they run, and encrypted volumes are mounted with keys held in RAM.
    • Credentials in use, such as cached tickets and, on older or unprotected Windows setups, secrets in LSASS.
    • Kernel state: loaded drivers and modules, hooked system call tables, hidden processes.

    None of this replaces disk or log evidence. It covers what those two miss, and for kernel state it is the only place to look.

    Capture first

    Memory is the most volatile evidence you have. Every minute the machine runs you lose some of it, and a reboot loses all of it. Capture before you shut anything down. Network isolation is fine and usually right, since it doesn’t touch RAM.

    On Windows, WinPmem writes a raw image. On Linux there are two common options: LiME, a kernel module that has to be built for the running kernel, and AVML, a static binary from Microsoft that reads /proc/kcore or /dev/crash and writes LiME format without a module build.

    # Linux, AVML (run as root, write to external media, not the suspect disk)
    ./avml /mnt/evidence/host01.lime
    
    # Linux, LiME (module must be built for this exact kernel)
    insmod ./lime-$(uname -r).ko "path=/mnt/evidence/host01.lime format=lime"
    
    # Hash it and record who, when and how
    sha256sum /mnt/evidence/host01.lime | tee host01.lime.sha256

    Capturing changes memory. Your tool is now in RAM too. That’s acceptable, but write down what you ran and when.

    First pass with Volatility 3

    Volatility 3 is the current generation, a Python tool you run as vol or python3 vol.py. Windows images pull symbols from Microsoft’s symbol server on first use. Linux images need a symbol table (ISF) matching the exact kernel build, and that is the step that trips people up. If you manage a fleet, build and store symbol files for your standard kernels ahead of time. Doing it during an incident is miserable.

    Start with the process list, and run two plugins that disagree on purpose.

    vol -f host01.raw windows.pslist
    vol -f host01.raw windows.psscan
    vol -f host01.raw windows.pstree
    vol -f host01.raw windows.cmdline

    pslist walks the kernel’s active process list. psscan scans memory for process structures, so it also finds processes that were unlinked from the list or that already exited. A process in psscan and not in pslist deserves a long look, though an exited process isn’t automatically suspicious, so check the exit time first.

    pstree and cmdline give you parentage and arguments. Word spawning PowerShell with an encoded command is the classic, and I still see it more than anything fancier.

    Hunting injected code

    The plugin I reach for next is malfind. It looks for memory regions that are executable and not backed by a file on disk, which is what injected code and reflectively loaded payloads look like.

    vol -f host01.raw windows.malfind
    vol -f host01.raw -o ./dumps windows.malfind --dump
    vol -f host01.raw linux.malfind

    Expect false positives. Just-in-time compilers (browsers, .NET, Java) legitimately create executable anonymous memory, and some security products inject into every process. I treat malfind output as a list of leads: the region’s protection, the first bytes (an MZ header in a private region is hard to explain), and which process owns it. Dump the region and analyze it rather than trusting the plugin’s verdict.

    Network and persistence in memory

    vol -f host01.raw windows.netscan
    vol -f host01.raw windows.svcscan
    vol -f host01.raw windows.registry.hivelist
    vol -f host01.raw linux.sockstat
    vol -f host01.raw linux.lsmod

    netscan can show closed connections as well as live ones that are still resident in memory. That gives you a partial timeline of outbound traffic that firewall logs may not have if the attacker used an allowed port. Matching a remote address to a process you already flagged in the tree is the moment an investigation stops being a hunch.

    On Linux, kernel rootkits are where memory pays for itself. A module hidden from lsmod output can still be found by scanning memory, and a hooked system call table can show up with plugins like check_syscall, depending on kernel version and symbol support. Check the plugin list for your Volatility version, because Linux coverage moves quickly.

    Where it falls short

    Memory images are as large as the machine’s RAM, so you need space and a way to move them, and the capture is a single moment. If the attacker ran something an hour earlier and cleaned up, memory may have been overwritten since. Pages swapped to disk aren’t in the image unless you also collect the pagefile, and Windows hibernation and crash dump files are worth grabbing for the same reason.

    Defenses reduce what you find, too. Credential Guard moves secrets out of the LSASS process into an isolated one. That’s good for defenders even though it makes the forensics less satisfying. Some malware encrypts its payload while idle. And a kernel-level rootkit can lie to your capture tool if you acquire from inside the live OS, which is why hypervisor snapshots of VMs are so valuable: you capture from outside the guest.

    The practical version: if your servers are virtual machines, learn how to take a memory snapshot from the hypervisor before you need one. It is quicker than any agent, and the guest can’t interfere with it.

    What I’d set up now

    Choose a capture tool for each OS and test it on a lab machine. Store Linux symbol files for the kernels you run. Write a one-page procedure covering where the image goes, how it is hashed, and who can authorize a capture on a production host. Then infect a lab VM on purpose, capture it, and run the plugins above. That way the first time you read malfind output isn’t during a real incident.

  • auditd and what to log so an investigation is possible

    auditd and what to log so an investigation is possible

    Part 3 of 4 in the Hardening a Linux server series. Earlier parts: Part 1, SSH hardening and Part 2, systemd sandboxing.

    The first question in almost every Linux intrusion investigation is “what did they run?”, and on most servers the answer is “no idea”. Shell history is a file the attacker controls; clearing it is common enough to have its own MITRE ATT&CK entry (T1070.003). The application logs show the request that got them in and then nothing. The auth log shows a sudo or two. Everything in between is gone.

    The Linux audit subsystem fixes that, and it’s been in the kernel since 2.6. The kernel emits records for the events you ask about, auditd writes them to disk, and the records carry the one field that makes them worth having: the audit UID.

    Why auid is the whole point

    Pipeline diagram: user session with auid, kernel audit rules, kauditd backlog, auditd writing audit.log, plugins shipping events to an append-only central collector

    When a user logs in, pam_loginuid stamps their session with a login UID, auid in audit records. It survives sudo, su, setuid binaries and daemonizing. So when someone runs sudo -i and then deletes a log file, the event says uid=0 and also auid=1004, which is the human who logged in. Syslog can’t give you that.

    Services started by systemd at boot have no login UID. Unset shows up as 4294967295 (newer auditctl also accepts unset). Filtering on auid>=1000 -F auid!=4294967295 therefore means “actions traceable to a human session”, which is how you keep the volume sane.

    A rule set that answers real questions

    Rules live in /etc/audit/rules.d/*.rules and are compiled by augenrules --load. Here is a compact starting point. I’m not trying to be exhaustive; Florian Roth’s public auditd rule set on GitHub is a good, much longer reference.

    ## buffer and failure behaviour
    -D
    -b 8192
    --backlog_wait_time 60000
    -f 1
    
    ## identity and privilege configuration
    -w /etc/passwd -p wa -k identity
    -w /etc/shadow -p wa -k identity
    -w /etc/group -p wa -k identity
    -w /etc/sudoers -p wa -k priv_config
    -w /etc/sudoers.d/ -p wa -k priv_config
    -w /etc/ssh/sshd_config -p wa -k sshd_config
    -w /etc/ssh/sshd_config.d/ -p wa -k sshd_config
    
    ## persistence locations
    -w /etc/cron.d/ -p wa -k persistence
    -w /etc/crontab -p wa -k persistence
    -w /var/spool/cron/ -p wa -k persistence
    -w /etc/systemd/system/ -p wa -k persistence
    -w /root/.ssh/ -p wa -k persistence
    
    ## commands run by humans (both ABIs)
    -a always,exit -F arch=b64 -S execve -F auid>=1000 -F auid!=4294967295 -k user_exec
    -a always,exit -F arch=b32 -S execve -F auid>=1000 -F auid!=4294967295 -k user_exec
    
    ## kernel modules, time, tracing
    -a always,exit -F arch=b64 -S init_module -S finit_module -S delete_module -k modules
    -a always,exit -F arch=b64 -S adjtimex -S settimeofday -S clock_settime -k time_change
    -a always,exit -F arch=b64 -S ptrace -k tracing
    
    ## access denied to humans
    -a always,exit -F arch=b64 -S open -S openat -S truncate -S ftruncate -F exit=-EACCES -F auid>=1000 -F auid!=4294967295 -k access_denied
    -a always,exit -F arch=b64 -S open -S openat -S truncate -S ftruncate -F exit=-EPERM -F auid>=1000 -F auid!=4294967295 -k access_denied
    
    ## lock the configuration until reboot
    -e 2

    Every rule has a -k key. That’s what you search by later, and it’s what your SIEM will alert on. Name them for the question they answer.

    The arch=b32 execve rule is there on purpose. A 64-bit kernel with 32-bit compatibility still accepts 32-bit syscalls, and a rule that only matches b64 misses a statically linked 32-bit binary entirely. Older hardening benchmarks list both for this reason.

    Choices that matter more than the rules

    -e 2 makes the audit configuration immutable until the next reboot. Without it, an attacker who gets root runs auditctl -D and your logging stops silently. With it, they have to reboot the machine to change the rules, and a reboot is loud.

    -f sets what happens when the kernel can’t log: 0 is silent, 1 logs a kernel message, 2 panics the machine. Panic-on-audit-failure sounds strict and is occasionally a compliance requirement, but on most servers it hands an attacker who can flood the audit backlog a way to crash the box. I use 1 and alert on lost events instead. auditctl -s shows the lost counter, and it should stay at zero.

    -b and --backlog_wait_time size the kernel buffer and how long a process blocks when that buffer is full. Too small and you drop events during a burst, which is exactly when the attacker is busy.

    The other problem is volume. Full execve logging on a build server or a box running a lot of short-lived processes generates enormous logs. That’s why the rule above only covers human sessions. If a service account matters (the one your web app runs as, say), add a targeted rule with -F uid= for that account rather than logging every process on the machine.

    Reading it back

    Raw audit.log is famously unpleasant, one event split across several records. ausearch stitches them together and -i translates numeric IDs to names:

    ausearch -k user_exec --start today -i
    ausearch -k identity -ts 10/09/2026 06:00:00 -i
    aureport -x --summary
    aureport --login --failed

    For anything beyond a single host, send the records somewhere else. The audisp-remote plugin ships them over the network to a central auditd, or you can have a log shipper tail the file. Laurel, a plugin from the threathunters.io folks, rewrites the multi-record mess into one JSON line per event with the process tree attached, which SIEMs handle much better.

    Following a session

    This is where the keys and the auid field earn their keep. Say the persistence key fires because something wrote into /etc/cron.d/. The event shows the executable that did the write, the working directory, auid, and a ses= value, which is the kernel’s ID for that login session. That session number is the thread you pull:

    ausearch -k persistence --start recent -i
    ausearch --session 412 --raw | aureport -x -i
    ausearch -ua 1004 --start today -k user_exec -i

    The second command lists every executable that login session ran, in order, including the ones run through sudo. In practice that’s the difference between “someone added a cron job” and “this account logged in from this address, downloaded this file, ran it as root, then added a cron job”, which is the story an incident report needs.

    The things auditd doesn’t cover

    A few other things help too. Make the journal persistent (Storage=persistent in journald.conf), otherwise it lives in /run and disappears at reboot. Set LogLevel VERBOSE in sshd so logins record the key fingerprint, as covered in part 1. Make sure every host runs chrony or another NTP client, because an investigation across five machines with clocks four minutes apart turns into an argument about ordering.

    And send everything off the host quickly. Logs that sit only on the compromised machine are evidence the attacker can edit. The immutable flag stops them changing the rules. It doesn’t stop root from truncating /var/log/audit/audit.log after the fact. A remote copy that the server can append to and never delete from is what turns these logs into evidence.

    The last part of this series covers patching, which is the better way to avoid needing most of these investigations.

    Other parts of the Hardening a Linux server series: Part 1: SSH hardening beyond “disable root login”, Part 2: Least privilege with systemd sandboxing, Part 4: Patching without fear.