Persistence: find it, remove it, prove it is gone

Unit 3 of 5 in Adversary emulation and purple teaming.

You will be able to detect the common Linux persistence places with two kinds of sensor, remove a test change, and prove with a diff that the machine is back to its starting state.

Lesson

What persistence is

MITRE describes Persistence as techniques adversaries use to keep access to systems across restarts, changed credentials and other interruptions, including any access, action or configuration change that lets them maintain a foothold, such as replacing or hijacking legitimate code or adding startup code. The key for a defender is that persistence is a change. Something is added or modified in a place that runs later, and a change can be seen.

The four techniques in this unit are the scheduled job (cron, T1053.003), the new local account (T1136.001), the added SSH key (T1098.004) and the shell startup file (T1546.004). ATT&CK describes each: cron as the time-based scheduler whose crontab files store the schedule, a local account as one created to maintain access, the authorized_keys file as the list of keys allowed to sign in to an account, and shell configuration scripts as files run at different points of a login or session.

Watch the places where change happens

ATT&CK's detection strategies for these techniques share a shape: watch for creation or modification of the artefact, then look at what happens next. For cron, it describes detecting creation or modification of crontab entries by non-root users or from abnormal parent processes, followed by execution of uncommon binaries. For authorized_keys, it describes modifications through a shell or editor, correlated with suspicious process ancestry. For shell startup files, it describes modification of the startup scripts followed by anomalous execution or network activity at login.

The labs turn that into rules that watch the directories and files where the change lands and read the audit event for who did it. The original login ID (auid) is what shows who made the change even when it was made as root. Two different rules for the same technique can catch different things, for example one that watches the account files and one that watches the account-creation program, and keeping both is often right.

A rule must name its place

A file rule watches the path it names. The authorized_keys lab shows the limit: a rule for one user's file does not watch the files of other users, so a real deployment needs a rule per path or a different approach. ATT&CK also notes where the file usually lives: in a hidden folder in each user's home directory, and in a different place on ESXi hypervisors. That is one more reason a rule has to name its path.

The systemd and shell-startup tests add another lesson. A test whose cleanup you have not checked is a test that may leave something behind. The shell startup lab has you back up the files and record their checksums first, read the project's cleanup critically, and restore from the backup and prove it with the checksums.

A second sensor: the before-and-after snapshot

Audit rules only see what you wrote rules for. The last lab adds a second kind of sensor that needs no rule: a short script records the places persistence hides (scheduled jobs, accounts, keys, startup files), you run the four tests, take the snapshot again and diff the two. The same snapshot then proves cleanup worked, because after cleanup the diff is empty. You compare the two sensors by what each catches, and the report gives a result for every technique.

ATT&CK's mitigations for these techniques are worth reading for the report's fix section: review changes to the cron schedule, control who may use cron, and make shell startup files only changeable by certain administrators.

Key points

Sources

Practice in the labs

Five labs, one per technique plus a snapshot sensor. Each runs a documented test on your own VM, then removes it and proves it is gone.

Also try, in the simulators

Knowledge check

  1. Why can persistence usually be detected? Answer: It is a change to a place that runs later, and changes can be watched or diffed. Scheduled jobs, accounts, keys and startup files are things that are added or modified, which leaves evidence.
  2. Why does the authorized_keys lab say a per-file rule does not scale? Answer: The rule watches the path it names, so every user's file needs its own rule or a different sensor. A rule on one path does not see other paths; a before-and-after snapshot is one way to cover many.
  3. What proves that cleanup worked in the snapshot lab? Answer: An empty diff between the starting snapshot and the post-cleanup snapshot. A diff compares actual state; a cleanup command running does not prove everything was restored.
  4. Why keep two rules for the account-creation technique? Answer: One watches the account files and one watches the creating program; they catch different variations. The lab compares which rule caught the change and keeps both when they catch different things.

Back to the syllabus or see all classes.

Related