# Powershell tricks::Powershell Remoting

Author: 乌云历史资料库 (@wooyun_archive)
Published: 2014-11-03T02:03:00Z
Canonical: https://wepostx.com/topics/613

> 乌云历史资料归档
>
> **原始作者：** DM_
> **原始编号：** superkieran-wooyundrops:382
> **原始发布时间：** 2014-11-03 10:03
> **声明：内容仅用于技术研究和个人使用，版权归 wooyun.org。**

---

## 0x01 简介

Powershell Remoting建立在windows WinRM服务之上，可以一对一或一对多远程控制，也可以建立HTTP 或 HTTPS的“listeners”，使用WS-MAM协议接收远程传递的命令。
Windows 远程管理(WinRM)是 WS-Management 协议的 Microsoft 实现，该协议为使用 Web 服务的本地计算机和远程计算机之间的通信提供了一种安全的方式。 也就是说在WS-MAN协议基础上，客户端运行环境可以多样化。 比如[openwsman](https://github.com/Openwsman/openwsman)。

![原文图片](/media/2016/06/5010cee24aa856e0f5618c05e9bb747e)

图片来源：v3 Secrets of PowerShell Remoting

## 0x02 远程管理

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

![原文图片](/media/2016/06/9f0dbc35dec09aa0babade4e239e9594)

在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

可以在参考[这里](http://technet.microsoft.com/en-us/library/ff520073(WS.10).aspx)判断winrm版本。
通过Enable-PSRemoting命令打开PS远程，默认是启动了Kerberos认证。这个方法只适合两台电脑在相同域或信任域内的指定电脑（名字可以带后缀）.但它不支持跨域、域外或IP地址。
如果要跨域、或指定IP地址执行时我们可以在客户端这里执行下面的代码，需要将所有或单一远程主机添加在信任表中。

```text
Set-Item WSMan:\localhost\Client\TrustedHosts -Value * -Force
```
删除所有远程信任主机

```text
Clear-Item WSMan:\localhost\Client\TrustedHosts
```
如果要删除单一远程主机，则可以执行：

```text
$newvalue = ((Get-ChildItem WSMan:\localhost\Client\TrustedHosts).Value).Replace("computer01,","") Set-Item WSMan:\localhost\Client\TrustedHosts $newvalue
```
更改computer01。
列出所有远程信任主机

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

```text
$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 }
```

![原文图片](/media/2016/06/9d6f9f2fd564fa3fac535d60a5756316)

使用help * -Parameter computername命令可以列出所有默认可以远程使用的命令。并且认证过程都可以像上面的代码一样传递$cred。
之后写个for循环就可以一对多的执行了。

![原文图片](/media/2016/06/b826ed569b5085553233d33533e3fcb0)

如果输出内容过于冗杂，还可以使用ConvertTo-Csv或者ConvertTo-Html将powershell对象的输出转换为html或者csv。
如果想一对一获取交互式powershell，可以像这样执行Enter-PSSession：

```text
Enter-PSSession -ComputerName 192.168.200.161 -Credential $cred
```

![原文图片](/media/2016/06/ccde942548c52a33695487293232498e)

## 0x03 多任务分发

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

```text
Get-Job -Id 1 | select -ExpandProperty childjobs
```
得到child job id之后，再用 receive-job 接收回显结果。

![原文图片](/media/2016/06/7658da2715588a1d9c04e9a465684d13)

## 0x04 域内信息搜集

基本的信息搜集(日志、进程、服务等)可以靠上面列出的命令来收集，但是远程执行invoke-command是需要凭证的，如果是在域内我们是不是可以先用nltest搜集下信任域？
在windows中有个System.DirectoryServices.ActiveDirectory命名空间，和windows域有关。 其下有个类Domain，其中GetAllTrustRelationships()方法可以获得信任域。
那么在powershell就可以这样执行：

```text
([System.DirectoryServices.ActiveDirectory.Domain]::GetCurrentDomain()).GetAllTrustRelationships()
```
获得域之前的信任关系。 如果需要自行开发脚本，也可以参考下面的文档。
除此之外，还记得之前metasploit笔记中那个local_admin_search模块吗？veil-powerview中也有通过相同的方式实现了这一过程。
两种不同的脚本都通过调用OpenSCManagerA API连接远程主机测试是否成功。

![原文图片](/media/2016/06/bf750eea410c69a0361385c30f964d7d)

Local_admin_search.rb

![原文图片](/media/2016/06/670c1f1f890b5f2d3114fe2a7b2c0511)

Invoke-CheckLocalAdminAccess
附[veil-powerview作者博客中](http://www.harmj0y.net/blog/penetesting/finding-local-admin-with-the-veil-framework/)的测试截图：

![原文图片](/media/2016/06/9869bbfa38e99f80a369e0432ceed5ae)

## 0x05 参考

- [http://www.harmj0y.net/blog/redteaming/trusts-you-might-have-missed/](http://www.harmj0y.net/blog/redteaming/trusts-you-might-have-missed/)

- [http://msdn.microsoft.com/en-us/library/system.directoryservices.activedirectory.domain(v=vs.110).aspx](http://msdn.microsoft.com/en-us/library/system.directoryservices.activedirectory.domain(v=vs.110).aspx)

- [https://www.blackhat.com/docs/us-14/materials/arsenal/us-14-Schroeder-The-Veil-Framework-Slides.pdf](https://www.blackhat.com/docs/us-14/materials/arsenal/us-14-Schroeder-The-Veil-Framework-Slides.pdf)

- [https://www.blackhat.com/docs/us-14/materials/arsenal/us-14-Schroeder-The-Veil-Framework-Slides.pdf](https://www.blackhat.com/docs/us-14/materials/arsenal/us-14-Schroeder-The-Veil-Framework-Slides.pdf)

- [v3 Secrets of PowerShell Remoting.pdf](http://powershell.org/wp/2012/08/06/ebook-secrets-of-powershell-remoting/)

## 0x06 powershell pentest project 学习推荐

整理的过程发现了很多牛人的博客和项目，在这里分享一下。
Powershell HID attack toolkit ：[https://github.com/samratashok/Kautilya](https://github.com/samratashok/Kautilya)
post exploitation ：[https://github.com/samratashok/nishang](https://github.com/samratashok/nishang)
Remote DLL inject ：[https://github.com/clymb3r](https://github.com/clymb3r)
aspx的Powershell webshell ：[https://github.com/samratashok/nishang/tree/master/Antak- WebShell](https://github.com/samratashok/nishang/tree/master/Antak-WebShell)
Veil Post exploitation ：[https://github.com/Veil-Framework/Veil-PowerView](https://github.com/Veil-Framework/Veil-PowerView)
A PowerShell Post-Exploitation Framework ：[https://github.com/mattifestation/PowerSploit](https://github.com/mattifestation/PowerSploit)
local privilege escalation : [https://github.com/HarmJ0y/PowerUp](https://github.com/HarmJ0y/PowerUp)

## Replies
