Skip to content
STAGE33R

Guides · Incident response and recovery · 7 steps · reviewed

Linux server compromised? What to do in the first hour (without destroying the evidence)

When a server is compromised, the first hour decides how much you can learn and how much you lose. The aim is to stop the damage, keep the evidence, find the way in, and rebuild from a clean source instead of trusting a cleaned server.

  1. Contain without powering off

    Powering off destroys running processes and memory. Cut the server off instead: from the provider panel, move it to a firewall group that allows only your own IP, or block outbound traffic. Keep your SSH session.

    sudo ufw default deny outgoing
    sudo ufw allow out to YOUR_IP
    sudo ufw status verbose
  2. Take a snapshot first

    Ask the provider for a full disk snapshot before you change anything. It is your evidence and your way back.

  3. See who is logged in and who logged in

    Look for logins from addresses you do not know, at odd hours, or for users you did not create.

    last -a | head -n 30
    sudo lastb | head -n 20
    w
    awk -F: '$3==0 {print $1}' /etc/passwd   # only root should be listed
  4. Look at running processes and connections

    Unknown processes, programs running from /tmp or /dev/shm, and outbound connections to unfamiliar addresses are the usual signs.

    ps auxf | less
    sudo ss -tupan | grep -v 127.0.0.1
    sudo ls -la /tmp /var/tmp /dev/shm
  5. Check persistence: cron, services and SSH keys

    Attackers add a way back in. Check all of these on every user, root included.

    sudo crontab -l; for u in $(cut -d: -f1 /etc/passwd); do sudo crontab -u $u -l 2>/dev/null; done
    systemctl list-unit-files --state=enabled --no-pager | tail -n 40
    sudo find /home /root -name authorized_keys -exec ls -la {} \; -exec cat {} \;
  6. Rotate every secret that lived on the server

    Assume they are all known: SSH keys, database passwords, API keys, .env files, certificates and tokens in CI. Change them from a clean machine.

  7. Rebuild from a clean image

    A cleaned server can still hide a backdoor. Build a new server, deploy the code from version control, restore data from a backup made before the incident and close the hole that was used.

Common mistakes

  • Deleting files or reinstalling before the snapshot: the evidence is gone.
  • Cleaning the malware but not the way in: the attacker returns within days.
  • Restoring the latest backup without checking it is older than the intrusion.

How to undo it

Containment is reversible: ufw default allow outgoing and remove the allow rules once the server is clean or replaced.

Sources

Commands are for Debian and Ubuntu unless noted. Test on a copy first and keep a backup: you are responsible for changes on your own systems.