Piping text into netcat using WScript.Exec - vbscript

I'm playing a CTF and looking to pipe some content into netcat in order to illicit a response from an application listening on a port.
Here's what I'm trying to do:
Dim oShell : Set oShell = CreateObject("WScript.Shell")
oShell.Run "C:\\users\\me\\Desktop\\my_app.exe"
WScript.Sleep 1000
oShell.Exec "echo hello > C:\\users\\me\\Desktop\\netcat\\nc.exe 127.0.0.1 4444"
What I get is an error
WshShell.Exec: The system cannot find the file specified.
Which I presume is about the echo command as removing it works fine. What am I doing wrong?

#m0atz as #omegastripes mentioned, you need to use StdIn. The following demonstrates how:
Dim wshShell, oExec, buffer
Set wshShell = CreateObject("WScript.Shell")
Set oExec = WshShell.Exec("C:\\users\\me\\Desktop\\netcat\\nc.exe 127.0.0.1 4444")
oExec.StdIn.Write "hello" & vbCrLf
buffer = ""
While Not oExec.StdOut.AtEndOfStream
buffer = buffer & oExec.StdOut.Read(1)
Wend
WScript.Echo buffer

Related

How do I convert the weekday command so that I can manually pick the day for each task rather than it detecting it automatically?

When I started on this I wasn't aware that I had to be able to select each daily task manually and pick each task whenever I want, I'm trying to figure out how to convert it into a manual entry so I don't have to rework the whole thing, bear in mind I'm very new to vbscript so if there's an obvious solution I apologize. I'm still working on the later days of the week to finish this.
dtmToday = Date()
dtmDayOfWeek = DatePart("w", dtmToday)
'Select case to pickup the value of day of the week and call procedure
Select Case dtmDayOfWeek
Case 1
Call Sunday()
Case 2
Call Monday()
Case 3
Call Tuesday()
Case 4
Call Wednesday()
Case 5
Call Thursday()
Case 6
Call Friday()
Case 7
Call Saturday()
End Select
'Sunday procedure will execute from select case
sub Sunday()
'defining variables
dim wshShell
dim path
dim fso
'setting up the environment to run vbscript
Set fso = CreateObject("Scripting.FileSystemObject")
Set WshShell = WScript.CreateObject("WScript.Shell")
' Execute the command and append to the text file
WShShell.run "cmd /c ping -n 10 youtube.com >> ping.txt", hidden
wscript.quit
End sub
'Monday procedure will execute from select case
sub Monday()
'defining variables
dim wshShell
dim path
dim fso
'setting up the environment to run vbscript
Set fso = CreateObject("Scripting.FileSystemObject")
Set WshShell = WScript.CreateObject("WScript.Shell")
' to execute the command and append to the text file using >> if you want to text to be overriden use >
WShShell.run "cmd /c netstat >> netstat.txt", hidden
wscript.quit
End sub
'Tuesday procedure will execute from select case
sub Tuesday()
'defining variables
dim wshShell
dim path
dim fso
'setting up the environment to run vbscript
Set fso = CreateObject("Scripting.FileSystemObject")
Set WshShell = WScript.CreateObject("WScript.Shell")
' to execute the command and append to the text file using >> if you want to text to be overriden use >
WShShell.run "cmd /c arp -a >> arp.txt", hidden
wscript.quit
End sub
sub Wednesday()
'defining variables
dim wshShell
dim path
dim fso
'setting up the environment to run vbscript
Set fso = CreateObject("Scripting.FileSystemObject")
Set WshShell = WScript.CreateObject("WScript.Shell")
WShShell.run "cmd /c nbstat -n >> nbstat.txt", hidden
wscript.quit
End Sub
sub Thursday()
'defining variables
dim wshShell
dim path
dim fso
'setting up the environment to run vbscript
Set fso = CreateObject("Scripting.FileSystemObject")
Set WshShell = WScript.CreateObject("WScript.Shell")
WShShell.run "cmd /c tracert -n 10 youtube.com >> nbstat.txt", hidden
wscript.quit
End Sub
You need first optimize your code to avoid heavy duplication as #Lankymart was mentioned it in his comment, by writing one function and call it when you need it, and to store all your commands into an array for easy access by their index.
So your code can be written like that :
Option Explicit
' We define our Global variables
Dim Title,ArrCommands,strcmd,dtmDayOfWeek,IndexCommand,UserInput
Title = "Run command line based on the Day Of Week"
'----------------------------------------------------------------------------------
' We define and store our commands lines into an array
ArrCommands = Array(_
"ping -n 10 youtube.com >> ping.txt",_
"netstat >> netstat.txt",_
"arp -a >> arp.txt",_
"Color 0A & Title Running nbtstat command & nbtstat -n",_
"Color 0A & Title Running Tracert command & tracert youtube.com",_
"Color 0A & Title Running Ipconfig command & Ipconfig /all",_
"Color 0A & Title Running netstat command & netstat -ano"_
)
'-------------------------------Main Program---------------------------------------
Do While Not IsDate(UserInput)
UserInput = InputBox("Type a date here example 24/06/2020",Title,"24/06/2020")
dtmDayOfWeek = MyWeekday(UserInput)
IndexCommand = dtmDayOfWeek - 1
Loop
MsgBox "Day of the Week = "& dtmDayOfWeek & vbCrlf &_
"The command will be executed is : "& ArrCommands(IndexCommand),vbInformation,Title
'Select case to pickup the value of day of the week
Select Case dtmDayOfWeek
Case 1
Call Execute(ArrCommands(IndexCommand),0)
Case 2
Call Execute(ArrCommands(IndexCommand),0)
Case 3
Call Execute(ArrCommands(IndexCommand),0)
Case 4
Call Execute(ArrCommands(IndexCommand),1)
Case 5
Call Execute(ArrCommands(IndexCommand),1)
Case 6
Call Execute(ArrCommands(IndexCommand),1)
Case 7
Call Execute(ArrCommands(IndexCommand),1)
End Select
'MsgBox "Command line is done",vbInformation,Title
'----------------------------------------------------------------------------------
Function MyWeekday(MyDate)
If MyDate = "" Then MyDate = Date()
If IsDate(MyDate) Then
MyWeekDay = Weekday(MyDate)
Exit Function
End If
End Function
'----------------------------------------------------------------------------------
Sub Execute(StrCmd,Console)
Dim ws,MyCmd
Set ws = CreateObject("wscript.Shell")
'The console = 0 means will be running in hidden mode
If Console = 0 Then
MyCmd = "CMD /C " & StrCmd & " "
ws.run MyCmd,Console,True
End If
'The console = 1 means will be running in not hidden mode
If Console = 1 Then
MyCmd = "CMD /K " & StrCmd & " "
ws.run MyCmd,Console,True
End If
End Sub
'----------------------------------------------------------------------------------

How can I redirect my vbscript output to a file using batch file?

I am new to Windows Scripting. I have a simple script for archiving using WinRAR CLI utility. I have to schedule this script using batch file. During archiving there are some errors and I want them to write in a simple text file or at least I can write entire output of archiving in a file. How can I change my code to do this?
Dim MyDate
Dim OutputFile
const WaitUntilFinished = true, DontWaitUntilFinished = false, ShowWindow = 1, DontShowWindow = 0
MyDate = Replace(Date, "/", "-")
OutputFile = "backup-" & mydate & ".rar"
Set objShell = WScript.CreateObject("WScript.Shell")
objShell.CurrentDirectory = "C:\Users\ABC\Desktop\"
objShell.Run "C:\windows\Rar.exe a .\VBScripts\backups\" & OutputFile & " software", ShowWindow, WaitUntilFinished
objShell.Popup "Archiving Completed Successfully!",5, "Scheduled Backup"
Set objShell = Nothing
Batch file is like this;
#echo off
start /wait C:\Users\ABC\Desktop\VBScripts\scheduled_backup.vbs
Change your command line to include redirection to a log file:
logfile = "C:\path\to\your.log"
objShell.Run "%COMSPEC% /c C:\windows\Rar.exe a .\VBScripts\backups\" & _
OutputFile & " software >""" & logfile & """", ShowWindow, WaitUntilFinished
Use this function instead of WScript.Shell.Run:
' Runs an external program and pipes it's output to
' the StdOut and StdErr streams of the current script.
' Returns the exit code of the external program.
Function Run (ByVal cmd)
Dim sh: Set sh = CreateObject("WScript.Shell")
Dim wsx: Set wsx = Sh.Exec(cmd)
If wsx.ProcessID = 0 And wsx.Status = 1 Then
' (The Win98 version of VBScript does not detect WshShell.Exec errors)
Err.Raise vbObjectError,,"WshShell.Exec failed."
End If
Do
Dim Status: Status = wsx.Status
WScript.StdOut.Write wsx.StdOut.ReadAll()
WScript.StdErr.Write wsx.StdErr.ReadAll()
If Status <> 0 Then Exit Do
WScript.Sleep 10
Loop
Run = wsx.ExitCode
End Function
Call script instead of start in your batch and use redirection:
script //nologo C:\Users\ABC\Desktop\VBScripts\scheduled_backup.vbs 2> errors.txt

how do i input a parameter explicitly from outside to the program in vbscript?

i have written a program which calculates the HASH of all files of a folder and saves it an an xml file. now my objective is that i want to give the folder to be evaluated, explicitly from outside the program
Dim WshShell, oExec, input
Set WshShell = CreateObject("WScript.Shell")
Set oExec =_
WshShell.exec("C:\Users\Administrator\Downloads\fciv.exe -md5 -r -xml _
d.xml C:\openssl")
input = ""
Do While oexec.status=0
WScript.Sleep 5000
Loop
this is the program. i tried adding inputbox command thinking i would be able to give the input explicitly. here is the modified program
Dim WshShell, oExec, strin
Set WshShell = CreateObject("WScript.Shell")
strin= inputbox("folder")
Set oExec =_
WshShell.exec("C:\Users\Administrator\Downloads\fciv.exe -md5 -r -xml ex.xml strin")
input = ""
Do While oexec.status=0*
WScript.Sleep 1000
Loop
this is not working :( pls help. what exactly shud i be using thr? also how do i give the same input using a cmd file???
You just need to use the string concatenation operator & to build the command string:
Dim WshShell, oExec, strin
Set WshShell = CreateObject("WScript.Shell")
strin= inputbox("folder")
Set oExec =_
WshShell.exec("C:\Users\Administrator\Downloads\fciv.exe -md5 -r -xml ex.xml """ & strin & """")
input = ""
Do While oexec.status=0
WScript.Sleep 1000
Loop
I took the liberty of adding the appropriate double-quotes in case the input path has spaces. You might want to also add in some validation of the path, eg:
dim fso: set fso = CreateObject("Scripting.FileSystemObject")
if not fso.FolderExists(strin) then
WScript.Echo strin & " not found"
WScript.Quit 1
end if

VB script + automate CLI command by VB script

I write the following VB script in order to run the CLI command - vpnclient.exe
my target is to automate the vpnclcient process and answer “y” when question appears,
I have WIN XP PC
During running the vpnclient.exe in CMD window we get then following question
Do you wish to continue? (y/n):
In my VB I write the “echo y” in order to answer on this question automatically
but question is still stuck in CMD window ,and I cant continue
please advice what chuld be wrong in my code and how to fix it?
MY VB script (vpnclient.exe – exist under VPN directory)
Dim oShell
Set oShell = WScript.CreateObject ("WScript.Shell")
oShell.run "cmd /K CD C:\Program Files\Cisco\VPN & ( echo y | vpnclient.exe connect ""site moon"" )"
Set oShell = Nothing
You can try by creating a file with the commands to be executed on the command line instead of echoing the password.
Here's an example where a text file is created first with the required command and then those commands are invoked from the file.
Public Function FTPDownload(serverName, ftpuser, ftppassword, dirPath, localpath, fileName)
Dim fso, myfile
Set fso = CreateObject("Scripting.FileSystemObject")
'Create FTP.EXE commands file
Set fso = CreateObject("Scripting.FileSystemObject")
Set myfile= fso.OpenTextFile("C:\Regression\Results\ftp_cmd.ini", 2, True)
myfile.WriteLine("open " &serverName )
myfile.WriteLine(ftpuser)
myfile.WriteLine(ftppassword)
myfile.WriteLine("lcd " & localpath )
myfile.WriteLine("cd " & dirPath)
myfile.WriteLine("prompt")
myfile.WriteLine("cr")
myfile.WriteLine("mget *" &fileName &"*" )
myfile.WriteLine("mdelete *" &fileName &"*" )
myfile.WriteLine("close")
myfile.WriteLine("quit")
myfile.Close
'====================The following code executes the FTP script. It creates a Shell object and run FTP program on top of it.===================
Set objShell = CreateObject( "WScript.Shell" )
objShell.Run ("ftp -i -s:" & chr(34) & "C:\Regression\Results\ftp_cmd.ini" & chr(34))
Set objShell = Nothing
End Function

Getting command line output in VBScript (without writing to files)

I'm using VBScript, and my goal is to be able to substitute a drive letter for a path of my choosing. I need the D drive, and if it's not available I need to check if it's already mapped to the right spot; then notify the user if it's not. I found this: http://technet.microsoft.com/en-us/library/ee156605.aspx and I'm trying to adapt their second example:
Set objShell = WScript.CreateObject("WScript.Shell")
Set objExecObject = objShell.Exec("cmd /c ping -n 3 -w 1000 157.59.0.1")
Do While Not objExecObject.StdOut.AtEndOfStream
strText = objExecObject.StdOut.ReadLine()
If Instr(strText, "Reply") > 0 Then
Wscript.Echo "Reply received."
Exit Do
End If
Loop
(my adaptations):
Set objShell = WScript.CreateObject("WScript.Shell")
Set objExecObject = objShell.Exec("cmd /c substr")
strText = ""
Do While Not objExecObject.StdOut.AtEndOfStream
strText = strText & objExecObject.StdOut.ReadLine()
Loop
Wscript.Echo strText
Then I'll probably search for the string that tells where the D drive is mapped. I've also tried objShell.Exec("subst"), but I still don't get any output. Does anyone have any ideas on what I might be doing wrong? Or is there a better way to tell about drive mappings? Thanks,
213897
Your script doesn't work because you've mistyped the command name - it's subst, not substr.

Resources