sed, replace first line

sed, replace first line

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Sed, Replace First Line: A Strategy for Emergency Site Recovery

Removing a malicious payload injected into every .php file on your server is often the first step in emergency recovery when you cannot immediately restore from a clean backup. In this specific scenario, the attacker has bypassed standard protections by injecting code at the very beginning of the PHP execution flow. The goal here isn't a full security audit yet; it's to strip the site down to a functional state so you can run wget and create a static copy of your content before taking the server offline permanently.

The malicious snippet targets the global $sessdt_o variable, checking for cookies and eventually redirecting users or executing arbitrary code via eval. Because this logic sits at the top of every file, a simple text search-and-replace is insufficient. You need to target only the first occurrence of a specific pattern without disturbing the legitimate code that follows.

The most reliable way to achieve this with standard Unix tools is using sed in substitution mode combined with the 0,/pattern/ address. This syntax tells sed to apply the replacement command only to the first line that matches the pattern within each file. Here is the specific logic we need: identify the start of the injection and replace everything up to the end of the legitimate PHP opening or a specific marker, effectively neutralizing the script.

A robust approach involves defining the payload's unique signature. The code starts with global $sessdt_o;. If you are confident that this exact string does not appear later in your application logic (which is common for obfuscated malware), you can target it directly. However, a safer strategy is to replace the entire block of injected PHP until you hit a closing tag or a known safe delimiter if one exists. Since the injection wraps around legitimate code, simply deleting the first few lines might break syntax if the original file started with <?php.

Consider this sed command structure designed to handle the specific payload described:

find . -name "*.php" -type f -exec sed -i '0,/global $sessdt_o;/{s/.*//}' {} \;

This command finds every PHP file and runs sed -i. The address 0,/global $sessdt_o;/ matches the line containing the injection. The substitution part s/.*// deletes everything from the start of that line to the end of the line. This effectively removes the entire first line of the injection. If the injection spans multiple lines, you would need a more complex script or a tool like awk to delete until a specific safe point is reached.

One critical trade-off here is the risk of leaving remnants. If the original file had comments or code immediately following the injection on the same line (rare but possible), blindly deleting the line might shift indentation or break logic. Additionally, running this across an entire directory tree with find . can be slow on large repositories. For a quick recovery mission to enable static scraping, performance is secondary to correctness.

Another pitfall is that sed -i behavior differs between macOS/BSD and Linux environments. On macOS, you must specify an empty string as the backup extension (e.g., -i '') to avoid errors like "no such file or directory." Always verify your environment before executing destructive edits on a live system.

Once the payload is stripped, the site should theoretically return to its pre-infection state regarding this specific vector. This allows wget -r to fetch assets without triggering the redirect scripts or cookie injections that would otherwise block or corrupt your local static copy. Remember, however, that removing the script does not fix the underlying vulnerability in your Drupal installation. The immediate goal is data preservation; the long-term fix requires a clean reinstall from an uninfected source.

Our network