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
- Persistence is a change in a place that runs later; changes can be watched and diffed.
- Rules name their path: one user's file is not every user's file.
- Back up before a test, read the cleanup critically, restore and prove with checksums or an empty diff.
- Two sensors (rules and a snapshot diff) catch different things; the report compares them.
Sources
- MITRE ATT&CK: Persistence (TA0003)
- MITRE ATT&CK: Scheduled Task/Job, Cron (T1053.003)
- MITRE ATT&CK: Create Account, Local Account (T1136.001)
- MITRE ATT&CK: Account Manipulation, SSH Authorized Keys (T1098.004)
- MITRE ATT&CK: Event Triggered Execution, Unix Shell Configuration Modification (T1546.004)
- Linux man page: auditctl(8)
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.
- Catch cron persistence (T1053.003), then remove it and prove it is gone (Purple Persistence 1 of 5): You write rules for the places cron jobs live, read the original login user in the event and prove removal by the rule no longer firing and the file being gone.
- Catch a new local account (T1136.001), then remove it and prove it is gone (Purple Persistence 2 of 5): You compare a rule on the account files with a rule on the account-creation program and keep both because they catch different things.
- Catch SSH authorized_keys changes (T1098.004) and see the limit of per-file rules (Purple Persistence 3 of 5): You write a file rule for one user's key file and see why a per-file rule does not scale to many users.
- Catch shell startup file changes (T1546.004), and test the cleanup before you trust it (Purple Persistence 4 of 5): You back up the files, record checksums, run the tests and restore from the backup instead of trusting the cleanup alone.
- Find persistence without an audit rule: diff a before and after snapshot (Purple Persistence 5 of 5): You write a snapshot script, run the four tests in one sitting, read the diff and prove cleanup with an empty diff.
Also try, in the simulators
- Find requests to a script page nobody links to (Detection Lab task): In a real breach, small control pages were left on web servers. You write the rule that finds requests to a script page nobody links to.
Knowledge check
- 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.
- 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.
- 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.
- 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
- Adversary emulation and purple teaming
- Catch cron persistence (T1053.003), then remove it and prove it is gone (Purple Persistence 1 of 5)
- Catch a new local account (T1136.001), then remove it and prove it is gone (Purple Persistence 2 of 5)
- Catch SSH authorized_keys changes (T1098.004) and see the limit of per-file rules (Purple Persistence 3 of 5)
- Catch shell startup file changes (T1546.004), and test the cleanup before you trust it (Purple Persistence 4 of 5)
- Find persistence without an audit rule: diff a before and after snapshot (Purple Persistence 5 of 5)
- All classes
- Free cyber labs