Skip to content

Taking a forensic backup of a compromised cPanel account before cleanup

Before you clean a hacked cPanel account, capture an off-server forensic copy. It preserves the entry point, supports the incident record, and lets you recover mistaken deletions. Here is how to do it safely.

The instinct when a site is hacked is to start deleting immediately. Resist it for five minutes and take a forensic backup first. A snapshot of the infected account — files, database, logs, and timestamps intact — is what lets you trace how the attacker got in, prove a timeline for an incident record (which matters under PIPEDA and Quebec Law 25 if any personal data was exposed), and recover anything you delete by mistake during cleanup. Cleanup destroys evidence; the backup preserves it. This is the cheap insurance step that an experienced operator never skips.

What’s happening

Cleaning a compromise overwrites and deletes exactly the artifacts you would need to understand it: the backdoor’s modification time, the injected code, the rogue cron, the log lines around the breach. Once they are gone, you cannot answer “how did they get in?” — and if you cannot answer that, you cannot be confident you closed the hole. A forensic backup taken before cleanup freezes the crime scene.

The backup must go off the server (a compromised host may itself be untrustworthy, and you do not want the attacker deleting your evidence) and must preserve metadata — modification times especially, since the timeline is the single most useful forensic signal. cPanel’s own pkgacct produces a complete account archive (files, databases, mail, settings) and is the simplest one-shot capture. Critically, a forensic copy is not something you restore from to “fix” the site — it contains the malware. Its purpose is evidence and recovery of individual files, not a clean rollback.

How to verify

Capture the account and the evidence you specifically need for forensics — timeline, logs, and the persistence layer — before any change:

# Full account archive (files, DBs, settings) — the primary capture
/scripts/pkgacct <cpaneluser>
ls -lh /home/cpmove-<cpaneluser>.tar.gz

# A tar that PRESERVES timestamps (-p) for timeline forensics
tar -czpf /root/forensic-<cpaneluser>-$(date +%F).tar.gz \
  -C /home <cpaneluser>/public_html <cpaneluser>/logs <cpaneluser>/etc

Snapshot the volatile evidence that an account archive may not fully capture — server-side logs and the cron/persistence state:

# Preserve the relevant logs (these get rotated/overwritten)
cp -a /home/<cpaneluser>/logs/ /root/forensic-<cpaneluser>-logs/
cp -a /usr/local/apache/logs/error_log /usr/local/apache/logs/modsec_audit.log /root/forensic-<cpaneluser>-logs/

# Capture the cron and a timeline of recent changes
crontab -l -u <cpaneluser> > /root/forensic-<cpaneluser>-cron.txt
find /home/<cpaneluser>/public_html -type f -printf '%TY-%Tm-%Td %TH:%TM %p\n' \
  | sort > /root/forensic-<cpaneluser>-timeline.txt

Confirm the copy is complete and then move it off the server before you start cleaning:

# Verify the archive integrity, then copy it OFF this host
tar -tzf /root/forensic-<cpaneluser>-$(date +%F).tar.gz >/dev/null && echo "archive OK"
sha256sum /root/forensic-<cpaneluser>-*.tar.gz > /root/forensic-<cpaneluser>.sha256
# scp/rsync the tarball + checksum to a separate machine, then proceed

A verified tarball with preserved timestamps, plus copies of the logs and a file timeline, is the forensic baseline. The sha256sum lets you prove the evidence was not altered after capture.

The fix

  1. Take the backup before touching anything — this is the whole point: capture first, clean second. The moment you delete a backdoor or replace core files, the evidence of the entry point is gone. Five minutes of capture preserves your ability to actually close the hole.
  2. Use pkgacct for the complete account — /scripts/pkgacct <cpaneluser> archives files, databases, mail, and settings in one operation, producing a cpmove/cpacct tarball. It is the simplest way to capture everything the account contains without missing a subsystem.
  3. Preserve timestamps explicitly — the modification-time timeline is the most valuable forensic signal, so use tar -p (preserve permissions/times) and avoid any copy that resets mtimes. Generate a sorted file timeline (find -printf) as a readable artifact alongside the archive.
  4. Copy the evidence off the server — a compromised host cannot be trusted to hold its own evidence, and the attacker may delete it. scp/rsync the forensic tarball, the logs, and the timeline to a separate machine or secure storage. Evidence left only on the breached server is evidence at risk.
  5. Capture the volatile bits a backup may miss — server-level logs (error_log, modsec_audit.log, /var/log/secure, lfd.log), the user’s crontab, and the file timeline rotate or change quickly. Snapshot them deliberately; they often contain the clearest record of the breach and are not always inside an account archive.
  6. Checksum the capture — run sha256sum over the archive and store the hash separately. If the incident ever needs to be defended (a client dispute, a regulator question under Law 25), an unaltered, hashed evidence set is far more credible than files you have been editing.
  7. Do not restore from the forensic copy to fix the site — this backup contains the malware; restoring it reinstates the compromise. Use it only to read evidence and to recover specific files you deleted by mistake. The clean site comes from replacing application code, not from this archive.
  8. Label and retain it for the incident record — name the capture with the account and date, note what you observed, and keep it for as long as your incident-handling and any regulatory obligation require. Under PIPEDA / Law 25, if personal data may have been exposed, the timeline and scope this backup preserves are what an assessment and any breach notification rest on.

The discipline is simple: freeze the scene, move the evidence to safety, then clean. Skipping it saves five minutes and costs you the ability to know what happened. Stack Harbor preserves forensic evidence as the first step of incident response under cPanel/WHM management — start a conversation when an account has been compromised.