|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Diagnostics; |
| 4 | +using System.IO; |
| 5 | +using System.Management; |
| 6 | + |
| 7 | +namespace DLLirant.NET.Classes |
| 8 | +{ |
| 9 | + internal class CodeGenerator |
| 10 | + { |
| 11 | + public void GenerateDLL(string dllmain, List<string> importedFunctions = null) |
| 12 | + { |
| 13 | + string code = |
| 14 | + "#include <windows.h>\r\n" + |
| 15 | + "#include <stdio.h>\r\n\r\n" + |
| 16 | + |
| 17 | + "#pragma comment (lib, \"User32.lib\")\r\n\r\n" + |
| 18 | + "int Main() {\r\n" + |
| 19 | + "\tFILE* fptr;\r\n" + |
| 20 | + "\tfopen_s(&fptr, \"C:\\\\DLLirant\\\\output.txt\", \"w\");\r\n" + |
| 21 | + "\tfprintf(fptr, \"%s\", \"It works !\");\r\n" + |
| 22 | + "\tfclose(fptr);\r\n" + |
| 23 | + "\tMessageBoxW(0, L\"DLL Hijack found!\", L\"DLL Hijack\", 0);\r\n" + |
| 24 | + "\treturn 1;\r\n" + |
| 25 | + "}\r\n\r\n" + |
| 26 | + "BOOL APIENTRY DllMain(HMODULE hModule, DWORD ul_reason_for_call, LPVOID lpReserved)\r\n" + |
| 27 | + "{\r\n" + |
| 28 | + "\tswitch (ul_reason_for_call) {\r\n" + |
| 29 | + "\t\tcase DLL_PROCESS_ATTACH:\r\n" + |
| 30 | + "\t\t\t" + dllmain + "\r\n" + |
| 31 | + "\t\t\tbreak;\r\n" + |
| 32 | + "\t\tcase DLL_THREAD_ATTACH:\r\n" + |
| 33 | + "\t\tcase DLL_THREAD_DETACH:\r\n" + |
| 34 | + "\t\tcase DLL_PROCESS_DETACH:\r\n" + |
| 35 | + "\t\t\tbreak;\r\n" + |
| 36 | + "\t}\r\n"+ |
| 37 | + "\treturn TRUE;\r\n"+ |
| 38 | + "}\r\n\r\n"; |
| 39 | + |
| 40 | + if (importedFunctions != null) { code += string.Join("\n", importedFunctions.ToArray()); }; |
| 41 | + |
| 42 | + using (StreamWriter writer = new StreamWriter("output/dllmain.cpp")) |
| 43 | + { |
| 44 | + writer.WriteLine(code); |
| 45 | + } |
| 46 | + |
| 47 | + ExecuteCommand("cmd.exe", "/C clang++.exe dllmain.cpp -o DLLirantDLL.dll -shared"); |
| 48 | + } |
| 49 | + |
| 50 | + public bool StartExecutable(string path) |
| 51 | + { |
| 52 | + ExecuteCommand(path); |
| 53 | + |
| 54 | + if (File.Exists("C:\\DLLirant\\output.txt")) { |
| 55 | + return true; |
| 56 | + } |
| 57 | + return false; |
| 58 | + } |
| 59 | + |
| 60 | + public void ExecuteCommand(string path, string arguments = null) |
| 61 | + { |
| 62 | + Process process = new Process(); |
| 63 | + ProcessStartInfo startInfo = new ProcessStartInfo(); |
| 64 | + startInfo.WindowStyle = ProcessWindowStyle.Hidden; |
| 65 | + startInfo.FileName = path; |
| 66 | + if (arguments != null) |
| 67 | + { |
| 68 | + startInfo.Arguments = arguments; |
| 69 | + } |
| 70 | + startInfo.WorkingDirectory = $"{Directory.GetCurrentDirectory()}\\output"; |
| 71 | + process.StartInfo = startInfo; |
| 72 | + process.Start(); |
| 73 | + process.WaitForExit(10000); |
| 74 | + KillProcessAndChildrens(process.Id); |
| 75 | + } |
| 76 | + |
| 77 | + private static void KillProcessAndChildrens(int pid) |
| 78 | + { |
| 79 | + ManagementObjectSearcher processSearcher = new ManagementObjectSearcher |
| 80 | + ("Select * From Win32_Process Where ParentProcessID=" + pid); |
| 81 | + ManagementObjectCollection processCollection = processSearcher.Get(); |
| 82 | + |
| 83 | + // We must kill child processes first! |
| 84 | + if (processCollection != null) |
| 85 | + { |
| 86 | + foreach (ManagementObject mo in processCollection) |
| 87 | + { |
| 88 | + KillProcessAndChildrens(Convert.ToInt32(mo["ProcessID"])); |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + // Then kill parents. |
| 93 | + try |
| 94 | + { |
| 95 | + Process proc = Process.GetProcessById(pid); |
| 96 | + if (!proc.HasExited) proc.Kill(); |
| 97 | + } |
| 98 | + catch (ArgumentException) |
| 99 | + { |
| 100 | + // Process already exited. |
| 101 | + } |
| 102 | + } |
| 103 | + } |
| 104 | +} |
0 commit comments