Powershell tricks--Powershell Remoting

路人甲 2014-04-04 20:00:00

0x01 简介


Powershell Remoting建立在windows WinRM服务之上,可以一对一或一对多远程控制,也可以建立HTTP 或 HTTPS的“listeners”,使用WS-MAM协议接收远程传递的命令。

Windows 远程管理(WinRM)是 WS-Management 协议的 Microsoft 实现,该协议为使用 Web 服务的本地计算机和远程计算机之间的通信提供了一种安全的方式。 也就是说在WS-MAN协议基础上,客户端运行环境可以多样化。 比如openwsman

图片来源:v3 Secrets of PowerShell Remoting

0x02 远程管理


Powershell Remoting在windows server 2008以前默认是不开启的,需要通过administrator用户执行Enable-PSRemoting命令开启。

在windows server 2012中,Powershell Remoting默认开启。

在windows下,powershell默认使用winrm进行远程管理,winrm版本不同默认的监听端口也不同。如下:

The default ports for winrm 1.1 are http port 80 and https port 443

The default ports for winrm 2.x are http port 5985 and https port 5986

可以在参考这里判断winrm版本。

通过Enable-PSRemoting命令打开PS远程,默认是启动了Kerberos认证。这个方法只适合两台电脑在相同域或信任域内的指定电脑(名字可以带后缀).但它不支持跨域、域外或IP地址。

如果要跨域、或指定IP地址执行时我们可以在客户端这里执行下面的代码,需要将所有或单一远程主机添加在信任表中。

Set-Item WSMan:\localhost\Client\TrustedHosts -Value * -Force

删除所有远程信任主机

Clear-Item WSMan:\localhost\Client\TrustedHosts

如果要删除单一远程主机,则可以执行:

$newvalue = ((Get-ChildItem WSMan:\localhost\Client\TrustedHosts).Value).Replace("computer01,","")
Set-Item WSMan:\localhost\Client\TrustedHosts $newvalue

更改computer01。

列出所有远程信任主机

Get-Item WSMan:\localhost\Client\TrustedHosts

在使用远程执行时如果只提供用户名,那么则会弹窗输入密码。此时我们可以建立PSCredential对象将用户名和密码保存在里面。然后再传递给-Credential参数。-ScriptBlock参数后跟要执行的代码。

$UserName = "admin3"
$serverpass = "admin123!@"

$Password = ConvertTo-SecureString $serverpass -AsPlainText Force
$cred = New-Object System.Management.Automation.PSCredential($UserName,$Password)

invoke-command -ComputerName localhost -Credential $cred -ScriptBlock { ipconfig }

使用help * -Parameter computername命令可以列出所有默认可以远程使用的命令。并且认证过程都可以像上面的代码一样传递$cred。

之后写个for循环就可以一对多的执行了。

如果输出内容过于冗杂,还可以使用ConvertTo-Csv或者ConvertTo-Html将powershell对象的输出转换为html或者csv。

如果想一对一获取交互式powershell,可以像这样执行Enter-PSSession

Enter-PSSession -ComputerName 192.168.200.161 -Credential $cred

0x03 多任务分发


在使用invoke-command的时候,computername可为多个参数。在执行的时候可以使用-Asjob参数将执行过程放在后台。 接收回显的时候可以使用get-job查看job id,然后用receive-job接收全部回显结果。 但是如果我只是想查看某个远程主机的执行结果呢? 那么就可以像下面这样做:

Get-Job -Id 1 | select -ExpandProperty childjobs

得到child job id之后,再用receive-job接收回显结果。

0x04 域内信息搜集


基本的信息搜集(日志、进程、服务等)可以靠上面列出的命令来收集,但是远程执行invoke-command是需要凭证的,如果是在域内我们是不是可以先用nltest搜集下信任域?

在windows中有个System.DirectoryServices.ActiveDirectory命名空间,和windows域有关。 其下有个类Domain,其中GetAllTrustRelationships()方法可以获得信任域。

那么在powershell就可以这样执行:

([System.DirectoryServices.ActiveDirectory.Domain]::GetCurrentDomain()).GetAllTrustRelationships()

获得域之前的信任关系。 如果需要自行开发脚本,也可以参考下面的文档。

除此之外,还记得之前metasploit笔记中那个local_admin_search模块吗?veil-powerview中也有通过相同的方式实现了这一过程。

两种不同的脚本都通过调用OpenSCManagerA API连接远程主机测试是否成功。

Local_admin_search.rb

Invoke-CheckLocalAdminAccess

veil-powerview作者博客中的测试截图:

0x05 参考


0x06 powershell pentest project 学习推荐


整理的过程发现了很多牛人的博客和项目,在这里分享一下。

Powershell HID attack toolkithttps://github.com/samratashok/Kautilya

post exploitationhttps://github.com/samratashok/nishang

Remote DLL injecthttps://github.com/clymb3r

aspx的Powershell webshellhttps://github.com/samratashok/nishang/tree/master/Antak- WebShell

Veil Post exploitationhttps://github.com/Veil-Framework/Veil-PowerView

A PowerShell Post-Exploitation Frameworkhttps://github.com/mattifestation/PowerSploit

local privilege escalation:https://github.com/HarmJ0y/PowerUp

评论

路人甲

真正的路人甲.

twitter weibo github wechat

随机分类

神器分享 文章:71 篇
memcache安全 文章:1 篇
密码学 文章:13 篇
企业安全 文章:40 篇
SQL注入 文章:39 篇

扫码关注公众号

WeChat Offical Account QRCode

最新评论

Yukong

🐮皮

H

HHHeey

好的,谢谢师傅的解答

Article_kelp

a类中的变量secret_class_var = "secret"是在merge

H

HHHeey

secret_var = 1 def test(): pass

H

hgsmonkey

tql!!!

目录