Showing posts with label security. Show all posts
Showing posts with label security. Show all posts

Easily generate random strings in pure batch script

Using pure native Batch, this script generates random strings of any length and using any characters except ! % ^ & < >. This script should work on all Windows operating systems from XP onwards.

The code is pretty self-explanatory, so here goes:

@echo off
setlocal ENABLEEXTENSIONS ENABLEDELAYEDEXPANSION

:: This is used to specify the required length of the random string
set len=12

:: This specifies the characters that will be used to generate the random string
set charpool=0123456789ABCDEF 

:: This specifies the length of the character pool above used to generate the string
set len_charpool=16


set gen_str=

:: Loop %len% times
for /L %%b IN (1, 1, %len%) do (

  :: %RANDOM% / !RANDOM! is replaced with a random variable between 0 and 32768
  :: This is used as our source of randomness so we use some simple math to 
  :: restrict the random range to be within the length of len_charpool
  set /A rnd_index=!RANDOM! * %len_charpool% / 32768

  :: Use for to allow us to expand and use the variable with batch's substring
  :: functionality, and append the substring at the random index determined above
  :: to the gen_str variable. See set /? for more information.
  for /F %%i in ('echo %%charpool:~!rnd_index!^,1%%') do set gen_str=!gen_str!%%i
)


:: The random string has been generated and stored in %gen_str%
echo %gen_str%


Here's a screenshot of the script in operation:

Configure a Windows computer to route traffic

Open up a privileged (running as administrator) command prompt, and type the following:

reg add HKLM\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters /T REG_DWORD /v IPEnableRouter /d 1

Restart computer.

The computer can now be used as a "default gateway" and will forward packets to it's own gateway. Additionally you can set the gateway of your new router by specifying a default route using the windows route command:

route delete 0.0.0.0
route add 0.0.0.0 mask 0.0.0.0 [Internet gateway IP]


Before you change your routes though it's a good idea to make note of your existing routes:

route print

This is scratching the very surface of Windows routing. Windows supports a full set of advanced routing options and routing protocols using the <code>netsh routing</code> command.


Scenario I:
Given access to multiple computers on a LAN one could enable routing on a windows XP/7/8 computer as above, and then use it as a kind of anonymising "proxy" by using it as your default gateway on your own device. I say device, because you could use this with anything -- mobile phone, tablet, laptop, network scanner, IP camera, etc... Everything the device does will appear to come from your "Windows router", and that computer will be the only one that knows that the traffic is actually coming from your device. Additionally, Windows doesn't keep logs of forwarded packets as proxies usually do, so once you've accessed some data on the Internet, there's no logs or information of any kind on the router-computer to implicate you. Pretty neat hey? But I should note that this can be changed -- for example if this scenario is suspected, someone could install logging software or a packet sniffer on the router-computer and then see your device communicating with it.

Scenario II:
This could be used to share an internet connection that one computer has access to, for example a USB-tethered phone. You could enabled routing on the tethered computer and set up other computers on the local network to use that computer as the default gateway.

Administrator to System privilege escallation

Windows XP. There comes a time in every Windows users life when he wants more. Administrator access isn't enough. He wants to have the same level of access that the System has.

Maybe it's to kill a stubborn process which looks like a virus but actually turns out to be a critical System process, or maybe it's just so they can laugh maniacally.

Whatever the reason, I have created a script to give Administrator users SYSTEM level access to their Windows XP computers:


@echo off
echo.
at>nul
if "%errorlevel%"=="1" (
  echo You do not have sufficient priviliges to execute this command.
  echo You MUST be running as an administrator ^(with access to "at.exe"^).
  goto end
)
echo WARNING: This command will be executed with SYSTEM priviliges!
echo.
set /P cmd=Enter command to run (eg,"cmd", "taskmgr"): 

set rnd=r%random%n%random%d
echo.|time>%rnd%
set /P time=<%rnd%
del /F /Q %rnd%
for /F "usebackq tokens=5,6,7 delims=:. " %%i in ('%time%') do (
  set our=%%i
  set min=%%j
  set sec=%%k
)
set /A tot=%min%+1
set /A dif=60-%sec%
at %our%:%tot% /interactive %cmd% >nul
if "%errorlevel%"=="0" (
  echo The command will be executed in %dif% seconds.
) else (
  echo An unknown error has occured.
)

:end
echo Press any key to exit.
pause>nul
Save the code as a .bat file, and run!

This script uses the old at.exe trick to escalate your privs but it fills in the parameters automatically, so all you need to do is say what command you want to execute with your system privs.

PS: You DO need administrator access to run this script, so it's useless to most baddies. This is only useful if you want to increase from administrator to system.

Batch script port scanner

Sometimes you're on a network, on a machine with no tools and you need to scan the network.

If I truely have no access to tools such as nmap, I'll open up the command prompt and type in this little script to scan the network for live machines:
for /L %i in (1,1,254) do @ping -n1 -w1 192.168.0.%i |find "time="
(Where 192 168 and 0 are the first 3 octets of the local /24 IP address.)

This is a very simple ping-sweet script which will ping every host in the 192.168.0.0/24 (192.168.0.1-192.168.0.254) range and display replies.

So one day I was a little frustrated how long this script takes to scan 254 hosts, so I decided to create a multithreaded version, also in batch script. Because I'm cool like that.

Hold onto your socks...


@echo off

:: Number of scanning threads
set NUMTH=8

set ipfile=t%random%m%random%p
set scanfx=s%random%c%random%n
set /A thinc=256/%NUMTH% >nul

ping -4 -n 1 -w 1 %computername% |find "statistics" >%ipfile%
for /F "tokens=4,5,6,7 delims=:. " %%i in (%ipfile%) do (
  if not "%%i"=="" (
    echo Local IP address: %%i.%%j.%%k.%%l
    echo Scanning range  : %%i.%%j.%%k.0/24

    echo for /L %%%%a in ^(1,1,32^) do @ping -n 1 -w 1  %%i.%%j.%%k.%%%%a ^|find "time=" ^>^>%%0.log >%scanfx%1.bat
    echo for /L %%%%a in ^(33,1,64^) do @ping -n 1 -w 1  %%i.%%j.%%k.%%%%a ^|find "time=" ^>^>%%0.log >%scanfx%2.bat
    echo for /L %%%%a in ^(65,1,96^) do @ping -n 1 -w 1  %%i.%%j.%%k.%%%%a ^|find "time=" ^>^>%%0.log >%scanfx%3.bat
    echo for /L %%%%a in ^(97,1,128^) do @ping -n 1 -w 1  %%i.%%j.%%k.%%%%a ^|find "time=" ^>^>%%0.log >%scanfx%4.bat
    echo for /L %%%%a in ^(129,1,160^) do @ping -n 1 -w 1  %%i.%%j.%%k.%%%%a ^|find "time=" ^>^>%%0.log >%scanfx%5.bat
    echo for /L %%%%a in ^(161,1,192^) do @ping -n 1 -w 1  %%i.%%j.%%k.%%%%a ^|find "time=" ^>^>%%0.log >%scanfx%6.bat
    echo for /L %%%%a in ^(193,1,224^) do @ping -n 1 -w 1  %%i.%%j.%%k.%%%%a ^|find "time=" ^>^>%%0.log >%scanfx%7.bat
    echo for /L %%%%a in ^(225,1,254^) do @ping -n 1 -w 1  %%i.%%j.%%k.%%%%a ^|find "time=" ^>^>%%0.log >%scanfx%8.bat


  )
)

for /L %%i in (1,1,%NUMTH%) do echo @echo 1 ^>%%0.txt ^&exit >>%scanfx%%%i.bat

for /L %%i in (1,1,%NUMTH%) do start /MIN %scanfx%%%i.bat
echo Scanning...

:: Wait for all threads to finish
:waitthread
ping -n 2 127.0.0.1 >nul
for /L %%i in (1,1,%NUMTH%) do if not exist %scanfx%%%i.bat.txt goto waitthread

:: Copy the scan logs to a single file
copy %scanfx%*.bat.log %scanfx%.scan.log >nul

:: Clean up, delete temp files
del /F /Q %ipfile%
for /L %%i in (1,1,%NUMTH%) do @del /F /Q %scanfx%%%i.bat
for /L %%i in (1,1,%NUMTH%) do @del /F /Q %scanfx%%%i.bat.txt
for /L %%i in (1,1,%NUMTH%) do @del /F /Q %scanfx%%%i.bat.log

start "" %windir%\notepad.exe %scanfx%.scan.log
::type %scanfx%.scan.log |more

:: Wait for notepad to open before killing the file
:waitnotepad
ping -n 2 127.0.0.1 >nul
tasklist /FI "WINDOWTITLE eq %scanfx%.scan.log*" |find "notepad.exe" >nul
if not "%errorlevel%"=="0" goto waitnotepad

del /F /Q %scanfx%.scan.log

Oh shit, he didn't. Oh yes I did.

That right there is a multithreaded BATCH ping scanner. Not only that but it automatically detects the local IP address and uses that to determine the scan range.

Everything happens automatically, you just run it and it'll ping 254 addresses, and show you the results in a notepad window.

Save the code as a .bat file, and run!

Now, to figure out how to probe ports using batch script...