Backdooring Files
Last updated
Was this helpful?
Last updated
Was this helpful?
If you find any executable laying around the desktop
, the chances are high that the user might use it frequently. Suppose we find a shortcut
to PuTTY lying around. If we checked the shortcut's properties, we could see that it (usually) points to C:\Program Files\PuTTY\putty.exe
. From that point, we could download the executable to our attacker's machine and modify it to run any payload we wanted.
You can easily plant a payload of your preference in any .exe file with msfvenom
. The binary will still work as usual but execute an additional payload silently by adding an extra thread in your binary. To create a backdoored putty.exe, we can use the following command:
The resulting puttyX.exe
will execute a reverse_tcp meterpreter payload without the user noticing it.
this technique is highly detectable by AV solutions since msvenom signature is known already.
If we don't want to alter the executable, we can always tamper with the shortcut file itself. Instead of pointing directly to the expected executable, we can change it to point to a script that will run a backdoor and then execute the usual program normally.
Before hijacking the shortcut's target, let's create a simple Powershell script in C:\Windows\System32
or any other sneaky location. The script will execute a reverse shell and then run calc.exe from the original location on the shortcut's properties:
Finally, we'll change the shortcut to point to our script. Notice that the shortcut's icon might be automatically adjusted while doing so. Be sure to point the icon back to the original executable so that no visible changes appear to the user. We also want to run our script on a hidden window, for which we'll add the -windowstyle hidden
option to Powershell. The final target of the shortcut would be:
Let's start an nc listener to receive our reverse shell on our attacker's machine:
If you double-click the shortcut, you should get a connection back to your attacker's machine. Meanwhile, the user will get a calculator just as expected by them. You will probably notice a command prompt flashing up and disappearing immediately on your screen. A regular user might not mind too much about that, hopefully.
In addition to persisting through executables or shortcuts, we can hijack any file association to force the operating system to run a shell whenever the user opens a specific file type.
The default operating system file associations are kept inside the registry, where a key is stored for every single file type under HKLM\Software\Classes\
. Let's say we want to check which program is used to open .txt files; we can just go and check for the .txt
subkey and find which Programmatic ID (ProgID) is associated with it. A ProgID is simply an identifier to a program installed on the system. For .txt files, we will have the following ProgID:
We can then search for a subkey for the corresponding ProgID (also under HKLM\Software\Classes\
), in this case, txtfile
, where we will find a reference to the program in charge of handling .txt files. Most ProgID entries will have a subkey under shell\open\command
where the default command to be run for files with that extension is specified:
In this case, when you try to open a .txt file, the system will execute %SystemRoot%\system32\NOTEPAD.EXE %1
, where %1
represents the name of the opened file. If we want to hijack this extension, we could replace the command with a script that executes a backdoor and then opens the file as usual. First, let's create a ps1 script with the following content and save it to C:\Windows\backdoor2.ps1
:
Notice how in Powershell, we have to pass $args[0]
to notepad, as it will contain the name of the file to be opened, as given through %1
.
Now let's change the registry key to run our backdoor script in a hidden window:
Finally, create a listener for your reverse shell and try to open any .txt file on the victim machine (create one if needed). You should receive a reverse shell with the privileges of the user opening the file.