BAT&注册表重定向劫持

RunJS

常用引导,有时启动某个应用需要环境变量可以这样启动应用,会对启动的进程生效,即被继承

 

set PATH=D:\Developer\sdk\platform-tools;%PATH%
D:
chdir D:\Developer\Cygwin\bin
REM 推荐注释REM,次之::。
@echo off
set AndBug=D:\Developer\Cygwin\home\Sansan\AndBug
set PATH=D:\Developer\sdk\platform-tools;%PATH%

if defined PYTHONPATH ( set PathDelimiter=; )
set PYTHONPATH=%AndBug%\lib%PathDelimiter%%PYTHONPATH%

D:
chdir %AndBug%

C:\Python27\python.exe andbug

获取目录举例

:: Demo.bat/Demo.cmd文件
@echo off
setlocal EnableDelayedExpansion
echo 批处理文件%~df0
echo %%0 = %0
echo 当前正在运行的批处理文件所在路径:
echo ^^!cd^^! = !cd!
echo 当前目录是:%cd%
echo %%cd%% = %cd%
echo 当前的盘符及路径:%~dp0
echo %%~dp0 = %~dp0
echo 当前的盘符及路径的短文件名格式:
echo %%~sdp0 = %~sdp0
:: set "abc=%cd%"
:: 批处理文件C:\Masm64\Masm64\Examples\Console\d.cmd
REM %0 = "C:\Masm64\Masm64\Examples\Console\d.cmd"
:: 当前正在运行的批处理文件所在路径:
REM !cd! = C:\Masm64\Masm64\Examples\Console
:: 当前目录是:C:\Masm64\Masm64\Examples\Console
REM %cd% = C:\Masm64\Masm64\Examples\Console
:: 当前的盘符及路径:C:\Masm64\Masm64\Examples\Console\
REM %~dp0 = C:\Masm64\Masm64\Examples\Console\
:: 当前的盘符及路径的短文件名格式:
REM %~sdp0 = C:\Masm64\Masm64\Examples\Console\
:: 转义
::1、代码XXX中如含><&| 需在每个这类符号前加转义符 ^
::2、符号%不能用^而是双写自己,如echo %%windir%% 输出字符串%windir%。当然如果是想把本次批处理中的某变量值传递到新批处理中(即在新批处理中此处已是常量)另当别论,从某个意义上说,这种情况用批处理建批处理有一定实际意义——只有运行本批处理,才能得到运行另一批处理需要的数据
::3、如果你写这句代码处在某括号之中,那么括号也很特殊,可能也要加 ^ 来转义其他的我没仔细想,也许还有,例如符号 ! 
::总之可能对本批处理会起到改变命令作用的都要转义
::4、如果代码中有转义符本身,则它也要转义,等于是双写 ::5、如果写入另一批处理的代码较多,且排除了上一条所说的传递变量值的情况,可用另一方法不用转义就照代码需要格式写,方法是在本批处理最后加一句 ::more +8 %0>123.bat&exit (这里的数字8表示到本行共有多少行,据实改) ::表示把本批处理第8行之后的内容写入新批处理并退出(即不执行之后的代码)再把要写入的所有代码写在本批处理的最后 pause

%0即代表该批处理的全称(包括驱动器盘符、路径、文件名和扩展类型)

组合修饰符来得到多重结果 %~df0

~I - 删除任何引号("),扩充 %I
%~fI - 将 %I 扩充到一个完全合格的路径名
%~dI - 仅将 %I 扩充到一个驱动器号
In addition, substitution of FOR variable references has been enhanced.
You can now use the following optional syntax:
    %~I         - expands %I removing any surrounding quotes (")
    %~fI        - expands %I to a fully qualified path name
    %~dI        - expands %I to a drive letter only
    %~pI        - expands %I to a path only
    %~nI        - expands %I to a file name only
    %~xI        - expands %I to a file extension only
    %~sI        - expanded path contains short names only
    %~aI        - expands %I to file attributes of file
    %~tI        - expands %I to date/time of file
    %~zI        - expands %I to size of file
    %~$PATH:I   - searches the directories listed in the PATH
                   environment variable and expands %I to the
                   fully qualified name of the first one found.
                   If the environment variable name is not
                   defined or the file is not found by the
                   search, then this modifier expands to the
                   empty string
The modifiers can be combined to get compound results:
    %~dpI       - expands %I to a drive letter and path only
    %~nxI       - expands %I to a file name and extension only
    %~fsI       - expands %I to a full path name with short names only
    %~dp$PATH:I - searches the directories listed in the PATH
                   environment variable for %I and expands to the
                   drive letter and path of the first one found.
    %~ftzaI     - expands %I to a DIR like output line
In the above examples %I and PATH can be replaced by other valid
values.  The %~ syntax is terminated by a valid FOR variable name.
Picking upper case variable names like %I makes it more readable and
avoids confusion with the modifiers, which are not case sensitive.
View Code

 

如果这个批处理没有以管理员身份运行则弹窗请求权限,再执行后续需要高权限的代码(运行高权限代码检查errorlevel值判断权限,不是则创建临时vbs来请求权限再重新运行此批处理)

@echo off  
 
:: BatchGotAdmin  
::-------------------------------------  
REM Check for permissions  
>nul 2>&1 "%SYSTEMROOT%\system32\cacls.exe" "%SYSTEMROOT%\system32\config\system"

SET getAdminFile="%temp%\getadmin.vbs"

REM If error flag set, we do not have admin.  
if '%errorlevel%' NEQ '0' (  
    echo Requesting administrative privileges...  
    goto UACPrompt  
) else ( goto gotAdmin )  

REM getadmin.vbs:
REM Set UAC = CreateObject("Shell.Application") 
REM UAC.ShellExecute "ThisBatFilePath.BAT", "", "", "runas", 1 
:UACPrompt
    echo Set UAC = CreateObject^("Shell.Application"^) > %getAdminFile%
    echo UAC.ShellExecute "%~s0", "", "", "runas", 1 >> %getAdminFile%
    %getAdminFile%
    exit /B

:gotAdmin
    if exist %getAdminFile% ( del %getAdminFile% )
    pushd "%CD%"
    CD /D "%~dp0"
::--------------------------------------

echo I have the administrative privilege.
::cmd 利用DISM离线安装.NET Framework 3.5
Dism /online /enable-feature /featurename:NetFX3 /All /Source:H:\sources\sxs /LimitAccess
pause>nul

 

Windows设定计划任务

在20:02执行一次关机(有60秒提示)命令,会返回ID数值: at 20:02 shutdown -s -t 60

删除指定计划:at 2 /delete

查看计划任务at

C:\Windows\system32>at
状态 ID 日期 时间 命令行
-------------------------------------------------------------------------------
14 今天 21:22 shutdown -s -t 60

 

其保存位置在,win7是C:\Windows\System32\Tasks

Task information is stored in %WINDIR%\System32\Tasks (or C:\Windows\System32\Tasks). They appear to be stored as a single file per task, in an XML type format.

They are then referenced from the registry in HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Schedule\TaskCache

::MKLINK [[/D] | [/H] | [/J]] Link Target
::
::       /D      创建目录符号链接。默认为文件符号链接。
::       /H      创建硬链接,而不是符号链接。
::       /J      创建目录联接。
::       Link    指定新的符号链接名称。
::       Target  指定新链接引用的路径(相对或绝对)。


::cd %cd%
set DIR_PATH_CONTAINS_THIS_BAT=%~p0
::MKLINK /J %~n1 %1
::IF EXIST %cd%\libs\x86 MKLINK /J x86 %cd%\libs\x86
::IF EXIST %cd%\libs\armeabi MKLINK /J armeabi %cd%\libs\armeabi
::IF EXIST %cd%\libs\armeabi-v7a MKLINK /J armeabi-v7a %cd%\libs\armeabi-v7a


for /d %%d in (%*) do MKLINK /J %DIR_PATH_CONTAINS_THIS_BAT%%%~nd %%d
pause

@echo off
set /p "fp=[拖放文件到此并按回车] "
for /f "delims=" %%a in ("%fp%") do (
  echo 后缀名为: %%~xa
)
pause


@echo off
IF "%~1"=="" ECHO 无任何对象! & exit
for /d "delims=" %%a in ("%~1") do (
  echo 后缀名为: %%~xa
)
pause

 

 拖放处理,制作mklink
 

^-分行与转义符

1.分行
  可以把一个命令写成多行哦
例:
@echo 1^
2^
3^
4
@pause>nul
复制上面的命令,执行结果是什么?这就是分行符的作用
2.转义
当我们试图输出一个&的时候,我们会发现输出不了...因为它被当成分行符处理了
这时就要请出转义符^了,它可以剥夺后面第一个符号的特殊地位,变为普通符号(这其实是一个很神奇的功能,之所以神奇...赘不详述)
例:
@echo ^&
@pause>nul
这样就输出了&
附:有时%也能起到转义的效果
 

批处理判断变量值是否为空的终极方法

很多时候,我们需要检查某个变量的值是否为空,以便做后续处理。
  对于这个问题,很多人会毫不犹豫地选择 if "%str%"=="" …… 的句式,在早期的批处理中,还可以看到这样的语句:if [%str%]==[],有人把这两种写法称为“防空字符”。
  可是,这样做真的能在各种复杂的情况下判断某个变量的值是否为空吗?
  答案是否定的。
  先来看 if [%str%]==[],当%str%为空值的时候,它无疑能成功地捕获到这一情况,但是,如果%str%中含有特殊字符,比如>、&之类,这条语句就会报错。
  为了兼容这几个特殊字符,人们绞尽脑汁,想到了 if "%str%"=="" 语句,于是,>、&、|之类的特殊字符也能顺利通过验证了,这是防空字符的一大进步。
  但是,这条语句也不是完美的,因为有一个特殊字符它完全没办法处理!
  这个特殊字符是什么呢?
  它就是双引号"本身!
  我们执行一下如下代码:

@echo off
set str="
if "%str%"=="" (echo yes) else echo no
pause

CMD.EXE报错了:命令语法错误。
  原来,上面这条if语句执行的是 if """=="" ……,引号起到分组字符串的作用,而引号对是就近匹配的:第一个引号和第二个引号配成一对,第三个引号和第四个引号配成一对,第五个引号和第六个引号配成一对……,这条if语句最终的执行的是 if "==……,但是引号始终要作为分组字符来使用的,而单个的引号没有找到下一个可匹配的引号,所以就出现了语法错误。
  当把 %str% 的值改为两个引号的时候,上面这段代码的执行结果居然显示为no,想想这是为什么。
  if "%str%"==""……能兼容大部分特殊字符,但是偏偏不能兼容引号本身,那么,判断某个变量的值是否为空,是不是就没有更好的办法了呢?
  答案是肯定的,那就是:用 if defined str 语句来判断变量值是否为空。
  请执行以下代码:

@echo off
:Main
cls
set str=
set /p "str=请输入任意字符,或直接回车:"
if defined str (
    echo 变量 str 的值不为空
) else echo 变量 str 为空值
pause
goto Main

 


呵,鸡蛋里挑点骨头 ^_^
用 if defined 判断某个变量是否被定义过时需注意以下2点:
1、变量名若含空格,不能直接输入空格,有空格时需用变量来代替,且必须是用双!!来引用这个含空格的变量
也就是必须开启延迟变量,或者将需判断的变量名赋值给 for 的 %%i 变量。
2、若判断的变量名需要使用变量的字符截取功能时,则与上面的正好相反。
即:不能使用!!来引用变量,即使是在for中也是一样。

在功能上:
1、defined “如果已定义环境变量,DEFINED 条件的作用跟 EXISTS 的一样,除了它取得一个环境变量,返回的结果是 true。”
它只有 真 与 假 两种情况,因此,判断变量是否为空,当然最基本的办法就是 if not defined var (echo not) else echo yes
2、相比较之下,if "%input%"=="" 则对空格比较敏感些,它可以将空格独立出来处理。
在互动时输入任意字符进行判断,输入空格表示值不存在,这样更符合平时的使用习惯。
文本是否为空的判断也一样,一个文本中只有一堆空格而没有其他任何字符,我们通常认为这个文本是空的,而非defined认为的文本不为空。
综上,defined在比较苛刻的有与无的判断时使用,而 if "%input%"=="" 则在空格也是空值进行判断时使用。

 

如何判断字符串的长度

@echo off
set "str=Hello, bat! <^_^>%%""
call :strlen len
echo %len%
pause&goto :eof

:strlen
setlocal EnableDelayedExpansion&set n=0
:strlen_loop
if "!str:~%n%,1!" neq "" set /a n+=1&goto strlen_loop
endlocal&set "%~1=%n%"&goto :eof

 

感觉这类代码不存在完美不完美,通用型代码往往效率不够高,针对型代码局限性太大。
还是应该具体情况具体分析,根据已知条件的多少来写针对性的代码是最适合的。

求字符串长度,简短高效的批处理代码(多种算法)

set "length=0"
for /l %%a in (0 1 8190) do if "!var:~%%a,1!" neq "" set "length=%%a"

 

http://bbs.bathome.net/viewthread.php?tid=17450

代码如下, 例如输入的内容为 | * 等特殊字符时, 会直接出错, 有什么办法可以在不需要输入预处理符号的情况下, 对输入包含特殊字符的内容进行处理

要求: 在输入非0-9的字符时, 进行提示. 另外, 变量为空值时不会退出批处理

for /f "eol=0 delims=0123456789" %%a in ("%num:"=""%") do echo 请输入纯数字!
::将数字设为分隔符,若能取到第一节,则存在非数字字符

 adb

::adb shell su -c "/data/local/x86/inject com.android.ainject /data/local/x86/librservice.so"
::adb shell su -c "/data/local/armeabi/inject com.android.ainject /data/local/armeabi/librservice.so"

::增加环境变量到PATH
@set PATH=F:\Android\sdk\platform-tools;%PATH%
::设置工程libs目录
@set LIBS=F:\Android\workspace\AInject\libs
::设置ADB路径,暂时未使用,因为直接使用PATH
@set ADB=F:\Android\sdk\platform-tools\adb.exe
::架构armeabi、armeabi-v7a、x86等,默认armeabi

::@set PATH=D:\Developer\sdk\platform-tools;%PATH%
::@set ADB=D:\Developer\sdk\platform-tools\adb.exe
::@set LIBS=D:\Developer\workspace\AndroidInject\libs

@set ARCH=%1
@if not defined ARCH (set ARCH=armeabi) else echo yes
@echo %ARCH%

@cd %LIBS%
adb shell su -c "mkdir /data/local/%ARCH%"
adb shell su -c "chmod 777 /data/local/%ARCH%"
adb push %ARCH%\inject /data/local/%ARCH%
adb push %ARCH%\librservice.so /data/local/%ARCH%
adb shell su -c "chmod 777 /data/local/%ARCH%/inject"
adb shell su -c "chmod 777 /data/local/%ARCH%/librservice.so"
adb shell su -c "ls -l /data/local/%ARCH%"

 aliases 

http://superuser.com/questions/49170/create-an-alias-in-windows-xp

http://technet.microsoft.com/en-us/library/cc779439(WS.10).aspx

::创建一个bat文件,内容如下

doskey g++=g++ -static-libgcc -static-libstdc++ $*
@doskey ls=dir /b $*
@doskey l=dir /od/p/q/tw $*

::解释:doskey就相当于Linux中的alias,等于号左边是其右边的别名,$*表示这个命令还可能有其他参数,@表示执行这条命令时不显示这条命令本身


::将这个文件保存到任意一个目录下,但是最好是英文路径,我选的是C:\Users\XX(我的用户名)\cmd_autoexe.bat

::打开cmd,输入cmd /?来找到autorun的注册表项,你大概看到的也是这样的(所以这一步基本上可以跳过):

::HKEY_LOCAL_MACHINE\Software\Microsoft\Command Processor\AutoRun
::   and/or
::HKEY_CURRENT_USER\Software\Microsoft\Command Processor\AutoRun

::不过,可能没有AutoRun这个键,可以自己添加:右键——新建——字符串值,输入“AutoRun”,然后选中——右键——修改,把路径输入进去,大概像上图那样,然后关掉就可以了(这里我只改了CURRENT_USER的,没改HKEY_LOCAL_MACHINE,因为我觉得这样基本就够用了)

doskey 记录以前敲过的dos命令,可以用F7来显示,用"↑↓"来选择,用F9来输入 选择的命令号. 
doskey/restall 重新装入一次,以前的命令行撤消. 
doskey/history 显示内存中所有的命令,可以">"显示到其它文件中,缩写"/H". doskey dir=cls 则击入dir等同于cls一样. 
doskey/macros 可显示所有的宏定义,可使用">"重定义到文件中,可缩写"/M". doskey dir= 可撤消对dir 的宏定义. 
oskey p=dir$tdir*.exe/p$tdir c:\t$tdir c:\t$* $t为命令的区分符,而$*为命令的结束符

 

 

If /D was NOT specified on the command line, then when CMD.EXE starts, it looks for the following REG_SZ/REG_EXPAND_SZ registry variables, and if
either or both are present, they are executed first.

  • HKEY_LOCAL_MACHINE\Software\Microsoft\Command Processor\AutoRun

and/or

  • HKEY_CURRENT_USER\Software\Microsoft\Command Processor\AutoRun

Command Extensions are enabled by default. You may also disable extensions for a particular invocation by using the /E:OFF switch. You can enable or disable extensions for all invocations of CMD.EXE on a machine and/or user logon session by setting either or both of the following REG_DWORD values in the registry using REGEDIT.EXE:

  • HKEY_LOCAL_MACHINE\Software\Microsoft\Command Processor\EnableExtensions

 and/or

  • HKEY_CURRENT_USER\Software\Microsoft\Command Processor\EnableExtensions

to either 0x1 or 0x0. The user specific setting takes precedence over the machine setting. The command line switches take precedence over the registry settings.

In a batch file, the SETLOCAL ENABLEEXTENSIONS or DISABLEEXTENSIONS arguments takes precedence over the /E:ON or /E:OFF switch. See SETLOCAL /? for details.

The command extensions involve changes and/or additions to the following commands:

  • DEL or ERASE
  • COLOR
  • CD or CHDIR
  • MD or MKDIR
  • PROMPT
  • PUSHD
  • POPD
  • SET
  • SETLOCAL

 

 dx编译打包dex文件

::
::Convert a set of classfiles into a dex file, optionally embedded in a jar/zip. Output name must end with one of: .dex .jar .zip .apk or be a directory.
::
@set ASDKX=F:\Android\sdk\platforms\android-8
@set projectpath=F:\Android\workspace\AInject
@set dx=F:\Android\sdk\build-tools\android-4.4W\dx.bat

::控制输出文件的后缀名
@set filename_posix=%1
if not defined filename_posix set filename_posix=jar
@set output=%projectpath%\libs\dex_classes.%filename_posix%

::<directory> ok
@set classfiles=F:\Android\workspace\AInject\bin\classes
::::<file>.class but error
::::@set classfiles=F:\Android\workspace\AInject\bin\classes\com\android\ainject\Main.class

::::=============javac=============
::::javac -d <目录> 指定存放生成的类文件的位置 -target 1.8 VM版本,电脑上装的环境版本
::::javac -encoding utf-8 -target 1.5 -d bin\ %projectpath%\*.java -bootclasspath %ASDKX%\android.jar

::@set plugin_classdir=F:\Android\workspace\AInject\bin\plugin_classes
::if not exist %plugin_classdir% mkdir %plugin_classdir%
::javac -encoding utf-8 -source 1.6 -target 1.8 -d %plugin_classdir% F:\Android\workspace\AInject\src\com\android\ainject\Main.java -bootclasspath %ASDKX%\android.jar -cp F:\Android\workspace\AndroidSDK17\bin\androidsdk17.jar
::%dx% --dex --output=%output% %plugin_classdir% 
::::===============================


%dx% --dex --output=%output% %classfiles% 



::@set MainDexList=F:\Android\workspace\AInject\libs\class_file_names_for_MainDexList.txt
::for example. MainDexList.txt 
::  line 1. android/support/multidex/MultiDex.class
::  line 2. com/android/ainject/Main.class

::%dx% --dex --output=%output% --multi-dex --main-dex-list=%MainDexList% --minimal-main-dex %classfiles% 

 

 

注册表

  1. 删除注册表中的项:

    Windows Registry Editor Version 5.00

    [-HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Run]

    保存为reg文件双击导如即可删除此项,注意有个“-”号

  2. 删除注册表中的值:

    Windows Registry Editor Version 5.00

    [HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Run] 
    "SoundMan"=- 

    这样可以删除此值

  3. 设置注册表中的值:
    HKEY_CURRENT_USER\Software\Policies\Microsoft\Internet Explorer\Control Panel
    HomePage=dword:00000000
 

C:\Users\ss>reg add /?

REG ADD KeyName [/v ValueName | /ve] [/t Type] [/s Separator] [/d Data] [/f]
[/reg:32 | /reg:64]

KeyName [\\Machine\]FullKey
Machine Name of remote machine - omitting defaults to the
current machine. Only HKLM and HKU are available on remote
machines.
FullKey ROOTKEY\SubKey
ROOTKEY [ HKLM | HKCU | HKCR | HKU | HKCC ]
SubKey The full name of a registry key under the selected ROOTKEY.

/v The value name, under the selected Key, to add.

/ve adds an empty value name (Default) for the key.

/t RegKey data types
[ REG_SZ | REG_MULTI_SZ | REG_EXPAND_SZ |
REG_DWORD | REG_QWORD | REG_BINARY | REG_NONE ]
If omitted, REG_SZ is assumed.

/s Specify one character that you use as the separator in your data
string for REG_MULTI_SZ. If omitted, use "\0" as the separator.

/d The data to assign to the registry ValueName being added.

/f Force overwriting the existing registry entry without prompt.

/reg:32 Specifies the key should be accessed using the 32-bit registry view.

/reg:64 Specifies the key should be accessed using the 64-bit registry view.

Examples:

REG ADD \\ABC\HKLM\Software\MyCo
Adds a key HKLM\Software\MyCo on remote machine ABC

REG ADD HKLM\Software\MyCo /v Data /t REG_BINARY /d fe340ead
Adds a value (name: Data, type: REG_BINARY, data: fe340ead)

REG ADD HKLM\Software\MyCo /v MRU /t REG_MULTI_SZ /d fax\0mail
Adds a value (name: MRU, type: REG_MULTI_SZ, data: fax\0mail\0\0)

REG ADD HKLM\Software\MyCo /v Path /t REG_EXPAND_SZ /d ^%systemroot^%
Adds a value (name: Path, type: REG_EXPAND_SZ, data: %systemroot%)
Notice: Use the caret symbol ( ^ ) inside the expand string

 eeee
rem Update the Image File Execution key.
reg add    "HKLM\Software\Microsoft\Windows NT\CurrentVersion\Image File Execution Options\SearchIndexer.exe"      /v TracingFlags /t REG_DWORD /d 1 /f
reg add    "HKLM\Software\Microsoft\Windows NT\CurrentVersion\Image File Execution Options\SearchProtocolHost.exe" /v TracingFlags /t REG_DWORD /d 1 /f
reg add    "HKLM\Software\Microsoft\Windows NT\CurrentVersion\Image File Execution Options\SearchFilterHost.exe"   /v TracingFlags /t REG_DWORD /d 1 /f
reg query  "HKLM\Software\Microsoft\Windows NT\CurrentVersion\Image File Execution Options" /s /v TracingFlags

rem Clean up registry flags.

reg delete "HKLM\Software\Microsoft\Windows NT\CurrentVersion\Image File Execution Options\SearchIndexer.exe"      /v TracingFlags /f
reg delete "HKLM\Software\Microsoft\Windows NT\CurrentVersion\Image File Execution Options\SearchProtocolHost.exe" /v TracingFlags /f
reg delete "HKLM\Software\Microsoft\Windows NT\CurrentVersion\Image File Execution Options\SearchFilterHost.exe"   /v TracingFlags /f
reg query  "HKLM\Software\Microsoft\Windows NT\CurrentVersion\Image File Execution Options" /s /v TracingFlags
 Debugger参数作用:原来收到的执行请求的文件名和参数则被转化为Debugger指定程序的整个命令行参数。

 Debugger参数的这种特殊作用,它又被称为“重定向”(Redirection),而利用它进行的攻击,又被称为“重定向劫持”(Redirection Hijack),它和“映像劫持”(Image Hijack,或IFEO Hijack)只是称呼不同,实际上都是一样的技术手段。

结果:

 

posted @ 2014-10-18 22:15  山岚的一缺  阅读(1548)  评论(2编辑  收藏  举报
喜欢
评论
收藏
顶部