kunzj的个人空间 https://blog.eetop.cn/?200421 [收藏] [复制] [分享] [RSS]

日志

批处理 和VB脚本 VBS

已有 7411 次阅读| 2011-10-27 06:45

1.批处理调用VBS,并获取VBS的返回值

批处理 yztest.cmd:

@echo off
::take care of below command, never use it in this command, for this will disable char !.
::setlocal enabledelayedexpansion

for /f "delims=" %%a in ('cscript. //nologo yztest.vbs "hello"') do ( set strFromVBS=%%a )

VBS yztest.vbs:

set bjArgs=WSCRIPT.Arguments
strFromBAT = objArgs(0)

strFromBAT= replace(strFromBAT,"^","^^")
strFromBAT= replace(strFromBAT,"&","^&")
WScript.Echo strFromBAT

注:

1.注意在批处理端不要使用setlocal enabledelayedexpansion,否则从vbs返回的字符!不能显示。

2.注意在VBS端返回的字符需要做转义处理,否则字符^,&会丢失。


通过vbs脚本实现批处理后台运行

2011-06-14 13:47
转载自 分享
最终编辑 寺庄2

使用Windows脚本来调批处理

一个非常简单的小脚本
假设你的批处理是C:\xxx.bat
那么在记事本里写入

set ws=WScript.CreateObject("WScript.Shell")
ws.Run "C:\xxx.bat",0

保存为一个以vbs为扩展名的文件
运行它就可以在后台运行你的批处理了

批处理隐藏运行的10种思路

1.基础

HideRun.vbs
--------------------------------------------------------------------------------
CreateObject("WScript.Shell").Run "cmd /cD:\test.bat",0
其中D:\test.bat是你的批处理路径


HideRun.bat
--------------------------------------------------------------------------------
echo CreateObject("WScript.Shell").Run "cmd /cD:\test.bat",0>$tmp.vbs
cscript.exe /e:vbscript. $tmp.vbs
del $tmp.vbs
这个批处理其实不能使其批处理本身隐藏,但是下面大部分隐藏调用批处理的原理和基础。


HideRun.js
--------------------------------------------------------------------------------
new ActiveXObject('WScript.Shell').Run('cmd /cD:\Test.bat',0);
用Javascript有什么好处呢?js的字符串变量可以用单引号,从而方便命令行作为参数调用,而且js很好的支持多行语句用 ; 分隔写成一行。要注意的是:js要区分大小写,方法必须用括号,结尾必须有分号。所以就成了下面的命令:
--------------------------------------------------------------------------------
mshta "javascript.:new ActiveXObject('WScript.Shell').Run('cmd /cD:\test.bat',0);window.close()"

2.用快捷方式

如果要使一个批处理本身隐藏,可以参考附件里的一个快捷方式,修改附件中的相关路径即可隐藏启动你的批处理。可以用vbs来建立一个 .lnk,其实用批处理也行(先echo一个vbs出来)

3.利用系统服务

可以用sc建立一个系统服务然后启动这个服务来启动批处理。缺点是启动服务较慢,需要管理员权限
查考这个帖子,
http://www.cn-dos.net/forum/view ... =%E6%9C%8D%E5%8A%A1
asbai 兄的大作,极大的方便了我们的使用。
CODE:   [Copy to clipboard]
--------------------------------------------------------------------------------

runassrv add /cmdline:"C:\Windows\System32\cmd.exe /cD:\test.bat" /name:"mysrv"
net start mysrv
4.利用at计划任务

用at可以建立一个计划任务,在不输入 /interactive 参数可以后台运行。但是建使用at必须有管理员权限
CODE:   [Copy to clipboard]
--------------------------------------------------------------------------------

at 09:10 "cmd /cD:\Test.bat"
然后在 9:10 系统就会自动后台以SYSTEM权限运行这个bat

5.利用ftype文件关联

综合上面的技术,使所有批处理都隐藏运行
CODE:   [Copy to clipboard]
--------------------------------------------------------------------------------

ftype batfile=C:\Windows\System32\mshta "javascript.:new ActiveXObject('WScript.Shell').Run('cmd /c%1',0);window.close();"
大家可以讨论下下面的思路,目前没有明确的方法,但是理论上是可行的

6.rundll32

其实这个方法只是理论上估计的,这里提出来占个位置,留个记号,等待高手研究
rundll32可以调用 dll 里的API,如果有个dll可以隐藏run一个exe就可以实现隐藏启动批处理,呵呵。目前我也没找到方法。

7.其他用户

Windows 2k/XP支持多用户,如果能在后台登陆另一个账户的桌面然后运行一个批处理,就能完全达到隐藏的目的

8.bat2vbs

这个方法只是一个不是很成熟的思路。
查考这个帖子:
http://www.cn-dos.net/forum/view ... p;highlight=exe2bat
这使我们产生了一个想法:把bat转换成vbs,然后vbs生成一个临时bat文件,然后WScript.Shell.Run隐藏启动这个临时bat

9. .NET编译

参考这个帖子:
http://www.cn-dos.net/forum/view ... hlight=script%2Bnet
里面提到了一个 .NET Warpper,我们完全可以利用系统自带的组件把bat编译到 exe 当中。如果bat不涉及交互,exe自然就安静的运行了。

10.注入汇编

最后向大家推出的今天最隆重的,ASCII Assembly Code专家 Herbert Kleebauer 的又一力作:showwin.exe


   Quote:
showwin.exe let you minimize/maximize/hide the command window
within a batch program (requires W2k or better).


Usage:   showwin.exe number


0 SW_HIDE
    Hides the window and activates another window.


1 SW_SHOWNORMAL
    Activates and displays a window. If the window is minimized or maximized,
    the system restores it to its original size and position. An application
    should specify this flag when displaying the window for the first time.


2 SW_SHOWMINIMIZED
    Activates the window and displays it as a minimized window.


3 SW_MAXIMIZE
    Maximizes the specified window.


3 SW_SHOWMAXIMIZED
    Activates the window and displays it as a maximized window.


4 SW_SHOWNOACTIVATE
    Displays a window in its most recent size and position. This value is
    similar to SW_SHOWNORMAL, except the window is not actived.


5 SW_SHOW
    Activates the window and displays it in its current size and position.


6 SW_MINIMIZE
    Minimizes the specified window and activates the next top-level window
    in the Z order.


7 SW_SHOWMINNOACTIVE
    Displays the window as a minimized window. This value is similar to
    SW_SHOWMINIMIZED, except the window is not activated.


8 SW_SHOWNA
    Displays the window in its current size and position. This value is
    similar to SW_SHOW, except the window is not activated.


9 SW_RESTORE
    Activates and displays the window. If the window is minimized or maximized,
    the system restores it to its original size and position. An application
    should specify this flag when restoring a minimized window.


10 SW_SHOWDEFAULT
    Sets the show state based on the SW_ value specified in the STARTUPINFO
    structure passed to the CreateProcess function by the program that
    started the application.


11 SW_FORCEMINIMIZE
    Windows 2000/XP: Minimizes a window, even if the thread that owns the
    window is not responding. This flag should only be used when minimizing
    windows from a different thread.


---------------------------------------------------------------------------


A simple example:


:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::-:
@echo off
echo Bj@jzh`0X-`/PPPPPPa(DE(DM(DO(Dh(Ls(Lu(LX(LeZRR]EEEUYRX2Dx=>showwin.com
echo 0DxFP,0Xx.t0P,=XtGsB4o@$?PIyU WwX0GwUY Wv;ovBX2Gv0ExGIuht6>>showwin.com
echo T}{zE~@gwkBG@OEKcUt`E}@mqqBsy?seHBEaPhxr?@zAB`LrPEyoDt@Cj?>>showwin.com
echo pky_jN@QEKpEt@ij?jySjN@REKpEt@jj?jyGjN@SEKkjtlGuNw?p@pjirz>>showwin.com
echo FEvAUSQ?_YLTQ@@?~QCo_F~RDU@?aU?@MQ_AMy1GHs?Gw`LbLK@shM`S_d>>showwin.com
echo bD_nsDddlTr_sPdlnTcnaTv_xP_ngD_rhDhsrT_kkDhrtT_fmDRNCTILk?>>showwin.com
echo staThg_So_rPfnqTl`qTdq_ShtpTrdqThV_Sqrm@ILm?O?cOKFDP?@@?dx>>showwin.com
echo OdFUu?N?_B@J@??KD?HUA?QOGB_QO?F?SCAQO?UDF?UCE?Q_F?DUA?CUB?>>showwin.com
echo OFO?FOS?DUO?IUO?FOW?RU{OWFO?mYU?wdsTeQs@OQ@?QAQ?LUA?_F_og~>>showwin.com
echo UODUO?FOSAFOeAUOyAO_DCSHUOOQO?OCFGuHUOGFO?TUO?DRTTqrQ@kcmS>>showwin.com
echo gFkPFUO?ngRThVvTncmTJFvPMQDTrKDDcmqOFkkDFOSAFOeAUOyAdFFSnB>>showwin.com
echo sT`llTKcmTdmhTFQ@PBsdTrmnTdknTmhVTvncTwDSSOshTbnqTrrdTT~?K>>showwin.com
echo ?OGOQp?o??Gds?wOw?PGAtaCHQvNntQv_w?A?it\=upkNQD??OuQsQG[i?>>showwin.com
echo RwrqosHy?Mjmv\@IuBlpUrHdjNAslF~mH}OKT?U??PT~OL?O?O?i?COT~B>>showwin.com
echo U?OCU?YF0x>>showwin.com


showwin.com>showwin.exe
del showwin.com


dir


:: wait 4 seconds
ping -n 4 localhost >nul


:: hide window
showwin.exe 0


dir /b


:: wait 4 seconds
ping -n 4 localhost >nul


:: show window
showwin.exe 5


:: wait 4 seconds
ping -n 4 localhost >nul


del showwin.exe 

用批处理cmd或脚本vbs创建lnk快捷方式
2010-07-27 10:38

以创建“记事本”快捷方式为例子


方法一:使用cmd

echo [InternetShortcut] >>a.url
echo URL=c:\windows\system32\notepad.exe >>a.url
echo IconIndex=0 >>a.url
echo IconFile=C:\windows\system32\shell32.dll >>a.url


方法二:使用vbs(一)
Dim WSHShell, fs
Set WSHShell = WScript.CreateObject("WScript.Shell")
Set fs = WScript.CreateObject("Scripting.FileSystemObject")
Function MakeDesktopShortcut( name, target )
Dim Shortcut,DesktopPath,StartupPath
DesktopPath = WSHShell.SpecialFolders("Desktop")
Set Shortcut = WSHShell.CreateShortcut(DesktopPath & "\" & name & ".lnk")
Shortcut.TargetPath = target
StartupPath = fs.GetParentFolderName( target )
If fs.FolderExists( StartupPath ) then
Shortcut.WorkingDirectory = StartupPath
End If
Shortcut.Save
End Function

MakeDesktopShortcut "Shortcut to Notepad", "C:\Windows\Notepad.exe"


方法三:使用vbs(二)

set WshShell = WScript.CreateObject("WScript.Shell")
      strDesktop = WshShell.SpecialFolders("Desktop") '获得桌面目录
      set ShellLink = WshShell.CreateShortcut(strDesktop & "\qq.lnk") '快捷方式存放目录及名称
      oShellLink.TargetPath = "C:\Program Files\Tencent\QQ\CoralQQ.exe"   '指向的可执行文件
      oShellLink.WindowStyle. = 1 '运行方式
      oShellLink.Hotkey = "CTRL+SHIFT+F"    '快捷键
      oShellLink.IconLocation = "C:\Program Files\Tencent\QQ\QQ.exe, 0" '图标
      oShellLink.Description = "qq"    '备注
      oShellLink.WorkingDirectory = "C:\Program Files\Tencent\QQ\"   '起始目录
      oShellLink.Save                                     '创建快捷方式


windows下通过批处理和vbs脚本自动telnet主机并执行命令
2009-12-24 06:43

'建立Shell对象
set sh=WScript.CreateObject("WScript.Shell")
WScript.Sleep 500
'向telnet发送命令
sh.SendKeys "open 202.1.123.32 23{ENTER}"
WScript.Sleep 500
sh.SendKeys "hehe{ENTER}"
WScript.Sleep 500
sh.SendKeys "xlm{ENTER}"
WScript.Sleep 500
sh.SendKeys "telnet 218.23.33.146{ENTER}"
WScript.Sleep 500
sh.SendKeys "test{ENTER}"
WScript.Sleep 500
sh.SendKeys "haha{ENTER}"
sh.SendKeys "ksh{ENTER}"
WScript.Sleep 500
sh.SendKeys "set -o vi{ENTER}"
WScript.Sleep 500
sh.SendKeys "stty erase ^H{ENTER}"
-----------------------------------------------------------------------

' 对于SendKeys这个命令可以send什么,可以看下面的列表:
' BACKSPACE {BACKSPACE}, {BS}, or {BKSP}
' BREAK {BREAK}
' CAPS LOCK {CAPSLOCK}
' DEL or DELETE {DELETE} or {DEL}
' DOWN ARROW {DOWN}
' END {END}
' ENTER {ENTER} or ~
' ESC {ESC}
' HELP {HELP}
' HOME {HOME}
' INS or INSERT {INSERT} or {INS}
' LEFT ARROW {LEFT}
' NUM LOCK {NUMLOCK}
' PAGE DOWN {PGDN}
' PAGE UP {PGUP}
' PRINT SCREEN {PRTSC}
' RIGHT ARROW {RIGHT}
' SCROLL LOCK {SCROLLLOCK}
' TAB {TAB}
' UP ARROW {UP}
' F1 {F1}
' F2 {F2}
' F3 {F3}
' F4 {F4}
' F5 {F5}
' F6 {F6}
' F7 {F7}
' F8 {F8}
' F9 {F9}
' F10 {F10}
' F11 {F11}
' F12 {F12}
' F13 {F13}
' F14 {F14}
' F15 {F15}
' F16 {F16}
' SHIFT +
' CTRL ^
' ALT %

再写auto.bat批处理脚本:
-----------------------------------------------------------------------
rem 启动telnet
start telnet.exe
rem batch telnet
cscript. /nologo auto.vbs
-----------------------------------------------------------------------

两文件放于同一目录下,直接点击auto.bat就可以自动telnet主机并执行一些unix命令了。


脚本实现telnet自动登陆

2011-07-31 13:03
转载自 分享
最终编辑 小~C
layout:fixed;width:100%">


点赞

评论 (0 个评论)

facelist

您需要登录后才可以评论 登录 | 注册

  • 0

    周排名
  • 0

    月排名
  • 0

    总排名
  • 0

    关注
  • 1

    粉丝
  • 0

    好友
  • 0

    获赞
  • 1

    评论
  • 309

    访问数
关闭

站长推荐 上一条 /2 下一条


手机版| 小黑屋| 关于我们| 联系我们| 用户协议&隐私声明| 版权投诉通道| EETOP 创芯网
( 京ICP备:10050787号 京公网安备:11010502037710 ) |网站地图

GMT+8, 2025-12-21 03:34 , Processed in 0.038747 second(s), 13 queries , Gzip On, Redis On.

eetop公众号 创芯大讲堂 创芯人才网
返回顶部