[系统安全] 四十一.Powershell恶意代码检测系列 (2)Powershell基础语法和注册表操作
只有更深入的了解PowerShell基础及用法,才能更好地检测恶意代码
一.Powershell操作符
二.Powershell条件语句
1.if条件判断
2.switch语句
三.Powershell循环语句
1.foreach循环
2.while循环
3.break和continue关键词
4.for循环
5.switch循环
四.Powershell数组
1.数组定义
2.访问数组
五.Powershell函数
1.自定义函数及调用
2.函数返回值
六.Powershell字符串及交互
1.定义文本及转义字符
2.用户交互
3.格式化字符串
4.字符串操作
七.Powershell注册表操作
八.总结
作者的github资源:
逆向分析:
https://github.com/eastmountyxz/
SystemSecurity-ReverseAnalysis
网络安全:
https://github.com/eastmountyxz/
NetworkSecuritySelf-study
一.Powershell操作符
-eq 等于
-ne 不等于
-gt 大于
-lt 小于
-le 小于等于
-contains 包含
-notcontains 不包含
67 -eq 5050 -eq 501gb -gt 1tb(1,2,3) -contains 1(1,2,3) -contains 2(1,2,3) -contains 4
-not
$a=89 -gt 50$a-not $a
-and 与运算
-or 或运算
-not 非运算
-xor 异或运算
$true -and $true$true -and $false$true -or $false$false -or $false-not $true$true -xor $true
1,5,8,0,9 -ne 0二.Powershell条件语句
1.if条件判断
$num=100if($num -gt 90) {"大于90"} else {"小于等于90"}if($num -gt 100) {"大于100"} else {"小于等于100"}
if($num -gt 100) {"大于100"} elseif ($num -eq 100) {"等于100"} else {"小于100"}2.switch语句
$num=56if($num -gt 50 -and $num -lt 60){"大于50并且小于60"}elseif ($num -eq 50){"等于50"}else{"小于50"}
$num=56switch($num){{$_ -lt 50} {"此数值小于50"}{$_ -eq 50} {"此数值等于50"}{$_ -gt 50} {"此数值大于50"}}
三.Powershell循环语句
1.foreach循环
$arr=1..10foreach ($n in $arr){ $n*$n }
$arr=1..10foreach ($n in $arr){if (($n%2) -eq 0 ){$n}}
foreach ($file in dir c:\python34){if($file.length -gt 1kb){$file.name$file.length}}
$path_value = dir c:\python34foreach ($file in $path_value){if($file.length -gt 1kb){$file.name$file.length}}
2.while循环
$i=1$s=0while($i -lt 100){$s = $s + $i$i = $i + 1}$s$i
$num=15do{$num$num=$num-1}while($num -gt 10)
$i=1while($i -lt 6){if($i -eq 4){break}else{$i$i++}}
$i=1while($i -lt 6){if($i -eq 4){$i++continue}else{$i$i++}}
4.for循环
$sum=0for($i=1;$i -le 100;$i++){$sum=$sum+$i}$sum
5.switch循环
$num=1..10switch($num){default{"number=$_"}}$num=1..10switch($num){{($_ % 2) -eq 0} {"$_ 数值是偶数"}{($_ % 2) -ne 0} {"$_ 数值是奇数"}}
四.Powershell数组
1.数组定义
$arr=1,2,3,4,5$arr=1..5
$arr -is [array]$arr=1,3.14,"yangxiuzhang"$arr$arr -is [array]
$arr=@()$arr$arr -is [array]
$arr=,"hello"$arr$arr -is [array]$arr=1$arr$arr -is [array]
$arr=ipconfig$arr$arr -is [array]
2.访问数组
$arr=1,"hello world",(get-date)$arr
$arr[0]$arr[0,1]$arr[-1]//提取部分元素$arr[0..2]
$num = $arr[0..2]$num.count
$arr[($arr.count)..0]$arr=1,"hello world",(get-date)$arr+="csdn"$arr$arr.count
五.Powershell函数
1.自定义函数及调用
function myping(){ping www.baidu.com}myping
function myping($site){ping $site}myping www.baidu.com
function myinfo($name,$age){$info="I am $name, and i am $age years old."write-host $info}myinfo yxz,28
2.函数返回值
function add($num1,$num2){$sum=$num1+$num2return $sum}add 2 4function fun($num1,$num2){$sum=$num1+$num2$sum.gettype()$sum.gettype().fullnamereturn $sum}fun 2.2 4.3//多个返回值function other($num1,$num2,$num3){$value=$num1,$num2,$num3return $value}other 2.2 4 6
六.Powershell字符串及交互
1.定义文本及转义字符
"hello world $(get-date)""hello world $(5*7)""hello, my name is 'yangxiuzhang'"
`n 换行
`r 回车符
`t tab键
`b 退格符
`’ 单引号
"hello,`n my name is `'yangxiuzhang`'"2.用户交互
$input = read-host "请输入您的姓名""您好!您输入的姓名是:$input"
3.格式化字符串
$name="yangxiuzhang"$age=25$body="strong"$height=1.72"My name is $name, i am $age years old, and my body is $body, my height is $height"
"My name is {0}, i am {1} years old, and my body is {2}, my height is {3}" -f $name,$age,$body,$height4.字符串操作
$str="c:\windows\system32\demo.txt"$str.split("\")//数组类型,可以通过数组下标访问$str.split("\").gettype()
$str="https://blog.csdn.net/Eastmount/102781411/logo.png"$str.split("/")[-1]
$str.endswith("png")$str.contains("csdn")
$str="https://blog.csdn.net/Eastmount/102781411/logo.png"$str.compareto("window")$str.compareto("https://blog.csdn.net/Eastmount/102781411/logo.png")
//获取下标$str.indexof("s")//字符插入$str.insert(4,"yxz")//字符串替换$str.replace("n","N")
七.Powershell注册表操作
HKEY_CLASSES_ROOT:定义文档的类型\类以及与类型关联的信息以及COM组件的配置数据
HKEY_CURRENT_USER:包含当前登录到Windows的用户的配置信息
HKEY_LOCAL_MACHINE:包含与计算机相关的配置信息,不管用户是否登录
HKEY_USERS:包含有关默认用户配置的信息
HKEY_CURRENT_CONFIG:包含有关非用户特定的硬件的配置信息
cd hkcu:dir
cd systemdir
cd HKLM:get-itempropertyset-itemproperty八.总结
一.Powershell操作符
二.Powershell条件语句
三.Powershell循环语句
四.Powershell数组
五.Powershell函数
六.Powershell字符串及交互
七.Powershell注册表操作
[1] https://www.bilibili.com/video/av66327436 [推荐B站老师视频]
[2]《安全之路Web渗透技术及实战案例解析》陈小兵老师
[3] https://baike.baidu.com/item/Windows Power Shell/693789
[4] https://www.pstips.net/powershell-piping-and-routing.html
[5] https://www.pstips.net/using-the-powershell-pipeline.html
[6] 微软官方PowerShell文档
[7] C# 系统应用之注册表使用详解 - Eastmount
前文回顾(下面的超链接可以点击喔):
[系统安全] 四十一.Powershell恶意代码检测系列 (2)Powershell基础语法和注册表操作