powershell(6)-Multithreading
powershell的多线程是我们在使用powershell进行渗透过程中必须使用到的功能!为什么呢?试想,当你到达对方内网,你需要列出用户,或者下载文件等等操作的时候你是选择等待几天还是几分钟搞定呢?我们通过内存和CPU的占用来提高效率,也就是我们通常算法上说的用空间来换取时间。机器配置高,有的用,而不用就是浪费。
powershell自带的Job
这里使用网上一个例子
1. # 不使用多线程
2. $start = Get-Date
3. $code1 = { Start-Sleep -Seconds 5; 'A' }
4. $code2 = { Start-Sleep -Seconds 5; 'B'}
5. $code3 = { Start-Sleep -Seconds 5; 'C'}
7. $result1,$result2,$result3= (& $code1),(& $code2),(& $code3)
9. $end =Get-Date
10. $timespan= $end - $start
11. $seconds = $timespan.TotalSeconds
13. Write-Host "总耗时 $seconds 秒."
1. # 使用多线程
2. $start = Get-Date
3. $code1 = { Start-Sleep -Seconds 5; 'A' }
4. $code2 = { Start-Sleep -Seconds 5; 'B'}
5. $code3 = { Start-Sleep -Seconds 5; 'C'}
7. $job1 = Start-Job -ScriptBlock $code1
8. $job2 = Start-Job -ScriptBlock $code2
9. $job3 = Start-Job -ScriptBlock $code3
11. $alljobs = Wait-Job $job1,$job2,$job3
12. $result1,$result2,$result3 = Receive-Job $alljobs
14. $end =Get-Date
16. $timespan= $end - $start
17. $seconds = $timespan.TotalSeconds
18. Write-Host "总耗时 $seconds 秒."
那么可以测试到这两个脚本确实感觉上是使用了多线程,因为第二个版本使用时间只有9s左右的时间,但如果分来执行是需要15s的,就如第一个版本。那么这里是真的使用了多线程么?其实真实情况是多进程,最简单的查看方式,打开任务管理器,再执行脚本你可以看到多出3个powershell.exe的进程。
那么我们可以用这个多进程么?是可以用,但是需要注意每个进程都需要跨进程交换数据,而且没有节流的机制,所以我们还是来看看真正的多线程吧。
多线程
直接来看一段代码
1. $code = { Start-Sleep -Seconds 2; "Hello" }
2. $newPowerShell = [PowerShell]::Create().AddScript($code)
3. $newPowerShell.Invoke()
这样我们通过powershell的API运行一个代码块,就算是在一个进程内执行了代码,不会创建新的进程。这是单线程,那么如何多线程呢?下面的代码就可以实现啦,那么测试过程中推荐windows的process explorer来查看进程对应的线程,可以清晰的看到创建的线程。
2. # 设置线程限制为4,那么如果一起启动超过4线程就需要排队等待
3. $throttleLimit = 4
4. # 创建线程池
5. $SessionState = [system.management.automation.runspaces.initialsessionstate]::CreateDefault()
6. $Pool = [runspacefactory]::CreateRunspacePool(1, $throttleLimit, $SessionState, $Host)
7. $Pool.Open()
9. # 代码块
10. $ScriptBlock = {
11. param($id)
13. Start-Sleep -Seconds 2
14. "Done processing ID $id"
15. }
17. $threads = @()
19. # 创建40个线程
20. $handles = for ($x = 1; $x -le 40; $x++) {
21. $powershell = [powershell]::Create().AddScript($ScriptBlock).AddArgument($x)
22. $powershell.RunspacePool = $Pool
23. $powershell.BeginInvoke()
24. $threads += $powershell
25. }
27. # 获取数据
28. do {
29. $i = 0
30. $done = $true
31. foreach ($handle in $handles) {
32. if ($handle -ne $null) {
33. if ($handle.IsCompleted) {
34. $threads[$i].EndInvoke($handle)
35. $threads[$i].Dispose()
36. $handles[$i] = $null
37. } else {
38. $done = $false
39. }
40. }
41. $i++
42. }
43. if (-not $done) { Start-Sleep -Milliseconds 500 }
44. } until ($done)
大家可以试一试下面的代码和单独执行get-hotfix的速度差别:
1. $throttleLimit = 40
2. $SessionState = [system.management.automation.runspaces.initialsessionstate]::CreateDefault()
3. $Pool = [runspacefactory]::CreateRunspacePool(1, $throttleLimit, $SessionState, $Host)
4. $Pool.Open()
6. $ScriptBlock = {
7. get-HotFix
8. }
9. $threads = @()
10. $handles = for ($x = 1; $x -le 40; $x++) {
11. $powershell = [powershell]::Create().AddScript($ScriptBlock)
12. $powershell.RunspacePool = $Pool
13. $powershell.BeginInvoke()
14. $threads += $powershell
15. }
17. do {
18. $i = 0
19. $done = $true
20. foreach ($handle in $handles) {
21. if ($handle -ne $null) {
22. if ($handle.IsCompleted) {
23. $threads[$i].EndInvoke($handle)
24. $threads[$i].Dispose()
25. $handles[$i] = $null
26. } else {
27. $done = $false
28. }
29. }
30. $i++
31. }
32. if (-not $done) { Start-Sleep -Milliseconds 500 }
33. } until ($done)
那么以后大家需要执行的代码就写在脚本块区域即可。这里和前面的爆破脚本结合起来就是一个完美的爆破脚本和信息收集脚本。
