豆子今天无聊在github上看看有什么有意思的PowerShell脚本,无意中发现了PowerSploit这个项目,仔细看了看,这个模块是针对入侵测试写的,里面有大量相关的黑客脚本,随便找了一个试试看。

比如说这个,可以用来记录键盘的输入内容,完整的脚本我就不贴出来了。

https://github.com/PowerShellMafia/PowerSploit/blob/dev/Exfiltration/Get-Keystrokes.ps1

具体实现的功能先不去考虑,我很好奇他是怎么在后台执行的。可以看见脚本末尾这个作者使用的是runspace,他创建了一个runspace,然后传入脚本块和对应的参数,然后触发;

1
2
3
4
5
6
7
# Setup KeyLogger's runspace
    $PowerShell [PowerShell]::Create()
    [void]$PowerShell.AddScript($Script)
    [void]$PowerShell.AddArgument($LogPath)
    if ($PSBoundParameters.Timeout) { [void]$PowerShell.AddArgument($Timeout) }
    # Start KeyLogger
    [void]$PowerShell.BeginInvoke()

这种方式看起来很眼熟啊,豆子之前学习多线程的时候,就是使用runspace来替代后台的job,因为runspace的性能效率要高的多;

http://beanxyz.blog.51cto.com/5570417/1760880

事实上,我看了一下 这个黑客脚本之前也是使用的job,最新的版本改成了runspace,可见知识是相通的~

执行试试看

1
Get-Keystrokes -LogPath C:\temp\key.log

然后随便输入一下命令,查看一下对应的日志文件是否有记录 ,果然成功记录了

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
PS C:\Windows\System32\WindowsPowerShell\v1.0> gc C:\temp\key.log
"TypedKey","WindowTitle","Time"
"l","Administrator: Windows PowerShell ISE","9/06/2016 10:59:48 AM"
"s","Administrator: Windows PowerShell ISE","9/06/2016 10:59:48 AM"
"<Enter>","Administrator: Windows PowerShell ISE","9/06/2016 10:59:48 AM"
"g","Administrator: Windows PowerShell ISE","9/06/2016 10:59:50 AM"
"c","Administrator: Windows PowerShell ISE","9/06/2016 10:59:50 AM"
"< >","Administrator: Windows PowerShell ISE","9/06/2016 10:59:50 AM"
"c","Administrator: Windows PowerShell ISE","9/06/2016 10:59:51 AM"
"<Shift>","Administrator: Windows PowerShell ISE","9/06/2016 10:59:51 AM"
":","Administrator: Windows PowerShell ISE","9/06/2016 10:59:51 AM"
"\","Administrator: Windows PowerShell ISE","9/06/2016 10:59:51 AM"
"t","Administrator: Windows PowerShell ISE","9/06/2016 10:59:52 AM"
"e","Administrator: Windows PowerShell ISE","9/06/2016 10:59:52 AM"
"m","Administrator: Windows PowerShell ISE","9/06/2016 10:59:52 AM"
"p","Administrator: Windows PowerShell ISE","9/06/2016 10:59:52 AM"
"\","Administrator: Windows PowerShell ISE","9/06/2016 10:59:52 AM"
"k","Administrator: Windows PowerShell ISE","9/06/2016 10:59:53 AM"
"e","Administrator: Windows PowerShell ISE","9/06/2016 10:59:53 AM"
"y","Administrator: Windows PowerShell ISE","9/06/2016 10:59:53 AM"
"<Enter>","Administrator: Windows PowerShell ISE","9/06/2016 10:59:54 AM"
"<Enter>","Administrator: Windows PowerShell ISE","9/06/2016 10:59:54 AM"

如果我不管他,我所有的键盘操作都会被记录下来,那怎么停止这个监听?

查看一下runspace,我估计第二个最新的runspace应该是我刚刚创建的

1
2
3
4
5
PS C:\Windows\System32\WindowsPowerShell\v1.0> Get-Runspace
 Id Name            ComputerName    Type          State         Availability   
 -- ----            ------------    ----          -----         ------------   
  1 Runspace1       localhost       Local         Opened        Busy           
  2 Runspace2       localhost       Local         Opened        Busy

查看一下有啥属性和方法,发现可以close掉他

1
2
3
4
5
6
7
8
9
10
PS C:\Windows\System32\WindowsPowerShell\v1.0> Get-Runspace 2 | gm
   TypeName: System.Management.Automation.Runspaces.LocalRunspace
Name                         MemberType Definition                                                                                                                                                                                  
----                         ---------- ----------                                                                                                                                                                                  
AvailabilityChanged          Event      System.EventHandler`1[System.Management.Automation.Runspaces.RunspaceAvailabilityEventArgs] AvailabilityChanged(System.Object, System.Management.Automation.Runspaces.RunspaceAvailabilit...
StateChanged                 Event      System.EventHandler`1[System.Management.Automation.Runspaces.RunspaceStateEventArgs] StateChanged(System.Object, System.Management.Automation.Runspaces.RunspaceStateEventArgs)             
ClearBaseTransaction         Method     void ClearBaseTransaction()                                                                                                                                                                 
Close                        Method     void Close()                                                                                                                                                                                
CloseAsync                   Method     void CloseAsync()                                                                                                                                                                           
Connect                      Method     void Connect()

执行试试

1
PS C:\Windows\System32\WindowsPowerShell\v1.0> (Get-Runspace 2).close()

成功停止这个runspace,后面没有继续写入了。

现在我根据同样的方法,自己写了一个类似的小程序试试。我打算写一个后台程序,每隔30秒就弹出一个对话框,告诉我注意休息~

1
2
3
4
5
6
7
8
9
10
11
12
13
$scriptblock={
while($true){
$MessageboxTitle = “Health Reminder”
$Messageboxbody = “Please have a break, my lord”
$MessageIcon [System.Windows.MessageBoxImage]::Information
$ButtonType [System.Windows.MessageBoxButton]::OK
[System.Windows.MessageBox]::Show($Messageboxbody,$MessageboxTitle,$ButtonType,$messageicon)
Start-Sleep -Seconds 30
}
}
$job=[powershell]::create()
$job.addscript($scriptblock)
$job.begininvoke()

经测试,每隔30秒就会跳出这个对话框,成功!

本文转自 beanxyz 51CTO博客,原文链接:http://blog.51cto.com/beanxyz/1787607,如需转载请自行联系原作者

从Powershell 入侵脚本学到的如何执行后台runspace~相关推荐

  1. 从Powershell ***脚本学到的如何执行后台runspace~

    豆子今天无聊在github上看看有什么有意思的PowerShell脚本,无意中发现了PowerSploit这个项目,仔细看了看,这个模块是针对***测试写的,里面有大量相关的***脚本,随便找了一个试 ...

  2. powershell禁用计算机,PowerShell 因为在此系统中禁止执行脚本 解决方法

    在Powershell直接脚本时会出现: 无法加载文件 ******.ps1,因为在此系统中禁止执行脚本.有关详细信息,请参阅 "get-help about_signing". ...

  3. PowerShell因为在此系统中禁止执行脚本

    PowerShell因为在此系统中禁止执行脚本 解决方法: 在powershell中执行 set-ExecutionPolicy RemoteSigned

  4. PowerShell因为在此系统中禁止执行脚本解决方法

    在Powershell直接脚本时会出现: 无法加载文件 ******.ps1,因为在此系统中禁止执行脚本.有关详细信息,请参阅 "get-help about_signing".  ...

  5. 如何运行PowerShell的脚本文件

    运行PowerShell脚本有两种方式. 在运行任何脚本文件之前, 你都必须首先设置一个恰当的Execution Policy. PowerShell脚本跟Windows CMD一样, 和MS-DOS ...

  6. Powershell运行脚本异常:无法加载文件...因为在此系统上禁止运行脚本

    Powershell运行脚本异常:无法加载文件...因为在此系统上禁止运行脚本 前言 异常原因 解决方法 前言   最近购买的新笔记本在打开vscode时于终端输出该项异常,故记录异常原因及解决方案, ...

  7. powershell导入脚本失败,禁止运行脚本,无法远程连接服务器

    报错: Import-Module : 无法加载文件 C:\Users\t\Desktop\nishang-master\nishang.psm1,因为在此系统上禁止运行脚本.有关详细 信息,请参阅 ...

  8. windows 10 powershell 启用脚本执行功能

    默认windows 10 powershell 的安全策略是可以执行单步命令,但不能运行脚本.尝试运行脚本时会出现如下错误: File cannot be loaded because running ...

  9. shell 脚本初步,启动可执行 jar 文件

    可能很多同学在看到这篇文章的时候是第一次接触 shell 脚本.所以我们首先需要了解什么是 shell 脚本. 在 Windows 里我们经常会看到一种扩展名为 .bat 的文件,它称为批处理文件.批 ...

最新文章

  1. 257.二叉树的所有路径
  2. Android高通平台调试Camera驱动全纪录
  3. java注释模板_Java注释模板设置
  4. linux tunl0 ip,Centos7下lvs负载均衡lvs-tun模式配置
  5. ad文件服务器部署,AD 集成 – 配置 ADFS 登陆 - Seafile 服务器用户手册
  6. android studio 单元测试用法,基于Android Studio2.1.1 进行单元测试完整教程
  7. 多用户企业文件管理系统源码_设计项目管理系统,可以,并有必要
  8. 百分点制造行业大数据解决方案
  9. 2019年春季学期《软件工程》教学总结
  10. linux 打包大文件,tar打包处理大文件的解压缩方法
  11. TM1640操作源码--LED驱动IC
  12. Delphi——数组(静态数组和动态数组)、地址和指针
  13. Ubuntu16.04安装x11vnc服务并设置自动启动
  14. 阿里云--短信服务---开通步骤
  15. 全栈工程师如何逆袭?
  16. C++数独求解器与生成器
  17. CHIL-ORACLE-修改密码
  18. Installing Db2 on-prem on CentOS 7
  19. Python代码制作Wifi万能钥匙,成功获取到隔壁邻居的Wifi密码
  20. (纪中)1747. 马蹄印【DFS】

热门文章

  1. Winform中实现将照片剪贴到系统剪切板中(附代码下载)
  2. Winform中设置Dialog的显示位置居中
  3. 新建springBoot项目提示:The type org.springframework.context.ConfigurableApplicationContext cannot be resol
  4. Windows下配置Tomcat使用https协议
  5. Vue使用vue-pull-refresh插件实现下拉刷新
  6. Python中遍历字符串和字典
  7. Atom中安装atom-terminal插件来打开命令行运行vue项目
  8. 如何结合PICgo,Typora以及阿里云对象存储OSS搭建自己图床写博客
  9. dispatch作用 react_「React系列」手把手带你撸后台系统(Redux与路由鉴权)
  10. linux部署vue项目_Vue项目部署的最佳实践