can I run vbscript commands directly from the comm

2020-04-26 04:23发布

问题:

In Python you are not obliged to use a file, you can specify -c "..." and gives the Python commands to the Python interpreter via a string on the command line.

Can I achieve the same result with vbscript? I've seen solutions that need you to use a batch script, But what if I am on a system with zero writing permissions?

Following the answer from @Syberdoor, I can run this:

mshta vbscript:Execute("dim result:result=InputBox(""message"",""title"",""input"")(window.close):echo result")

but it still doesn't print the result in the console.

回答1:

There is one trick you can possibly use and that is mshta.exe. You can execute code like this:

mshta vbscript:Execute("<your code here, delimit lines with : (colon)>:close")

This is of course a fantasticly insane hack and on a system where you are not even allowed to create a file I am not sure if mshta.exe would be allowed.

Maybe you can also find additional inspiration from this thread (the mshta solution is also posted there). Although mostly batch related it is imo a great compendium of several really crazy ways to fool windows into executing vbs code.



回答2:

No, the interpreters shipped with Windows (wscript.exe and cscript.exe) don't support that. If you can't create a file anywhere you're out of luck. You need some kind of wrapper script to transform the argument into something that VBScript interpreters can execute.

The naïve approach would be to create a VBScript with an ExecuteGlobal statement like this:

With WScript.Arguments.Named
  If .Exists("c") Then ExecuteGlobal .Item("c")
End With

However, that won't work correctly, because double quotes aren't preserved in the arguments. If you ran the above script like this:

C:\> vbsrunner.vbs /c:"WScript.Echo "foo""

it would effectively execute the statement WScript.Echo foo instead of WScript.Echo "foo", and I was unable to find a way to escape nested double quotes so that they're preserved in the argument.

What will work is a batch script that writes the argument to a temporary file, and then executes that file with a VBScript interpreter:

@echo off

setlocal

set "tempfile=%TEMP%\%RANDOM%.vbs"

>"%tempfile%" echo.%~1
cscript.exe //NoLogo "%tempfile%"
del /q "%tempfile%"

That way you can run VBScript statements on the command line like this:

C:\> vbsrunner.cmd "WScript.Echo "foo" : WScript.Echo "bar""
foo
bar

If you wanted to replicate Python's interactive mode you could use my vbsh, but that would still require the ability to create a file somewhere.



标签: vbscript wsh