Skip to content

Instantly share code, notes, and snippets.

@rc1
Created April 26, 2012 20:28
Show Gist options
  • Select an option

  • Save rc1/2502802 to your computer and use it in GitHub Desktop.

Select an option

Save rc1/2502802 to your computer and use it in GitHub Desktop.
cleaning hacked files on dream host with node
#!/usr/bin/env node
var walk = require("./node-walk/lib/walk.js"),
fs = require('fs'),
path = require('path');
walker = walk.walk("./../", { followLinks: false });
var counter = {
clean : 0,
infected : 0,
cleaned : 0
};
walker.on("file", function (root, fileStats, next) {
if (path.extname(fileStats.name) === ".php") {
fs.readFile(root+"/"+fileStats.name, function (err, data) {
var dataAsString = data.toString();
if (dataAsString.indexOf("<?php /**/ eval(base64_decode(") >= 0) {
console.log("infected file", fileStats.name);
counter.infected++;
fs.writeFile(root+"/"+fileStats.name+".bk", data, function(err) {
if(err) {
console.log("- could not fix", err);
next();
} else {
console.log("- file backed up to:", root+"/"+fileStats.name+".bk");
var cleaned = dataAsString.replace(/(<\?php \/\*\*\/ eval\(base64_decode\("[a-zA-Z0-9\/+\-"\);?>]*)/gm, "");
fs.writeFile(root+"/"+fileStats.name, cleaned, function (err, data) {
if(err) {
console.log("- could not save cleaned file", err);
} else {
console.log("- cleaned");
counter.cleaned++;
}
next();
});
}
});
} else {
console.log("clean file", fileStats.name);
counter.clean++;
next();
}
});
} else {
next();
}
});
walker.on("end", function () {
console.log("all done");
console.log("total files:", counter.clean+counter.infected, "infected:", counter.infected, "clean:", counter.clean, "cleaned:", counter.cleaned);
console.log(counter.infected-counter.cleaned, "infected files remain");
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment