ssh端口转发

本文详细解析SSH端口转发的多种方式,包括本地转发、远程转发及动态转发,阐述如何利用SSH实现burpsuite公网抓包、共享本地web服务及本地流量开放访问等高级应用。同时,介绍了autossh的使用,确保SSH连接的持久化。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

基本配置

修改配置

vim /etc/ssh/sshd_config

在这里插入图片描述

一些参数

端口转发中几个常用的参数

-f	Requests ssh to go to background just before command execution.
-N	Do not execute a remote command.
-C	Requests compression of all data 
-g	Allows remote hosts to connect to local forwarded ports
-D	Specifies a local “dynamic” application-level port forwarding.
-R	Specifies that connections to the given TCP port or Unix socket on the remote (server) host are to be forwarded to the local side.
-L	Specifies that connections to the given TCP port or Unix socket on the local (client) host are to be forwarded to the given host and port, or Unix socket, on the remote side.

-f 后台执行,即让当前执行的该指令成为守护进程
-C 允许压缩数据
-N 不执行远程命令,即不与远程主机建立shell,只搭建隧道…
-g 网关功能,允许远程主机连接到本地转发端口。如果在多路连接上使用,则必须在主进程上指定此选项。
-R 远程端口转发
-L 本地端口转发
-D 动态端口转发

本地转发

本地转发,将本地指定端口的流量转发到指定目标的指定端口

这里的指定目标的指定端口可以是本地主机也可以是远程主机,目的都只有一个就是将本地流量转发到指定的主机上…

转发到本地主机
sudo ssh -fCNL *:80:localhost:7681 moddemod@localhost
转发到远程主机
sudo ssh -fCNL *:80:moddemod.top:1234 root@moddemod.top

远程转发(内网穿透)

远程转发,与本地转发相反,将指定目标主机指定端口流量转发到本地指定端口

同样,这里的指定目标主机指定端口可以是远程主机同样也可以是本地主机,都是一样的道理。

sudo ssh -fgCNR 0.0.0.0:8080:127.0.0.1:1070 root@192.168.0.77

远程主机8080端口流量转发到本地的1070端口远程主机的8080端口是任意IP都可以访问的

几种骚操作

  1. 实现burpsuite公网抓包【访问公网8080端口的流量转发到burpsuite监听端口进而进行抓包】
  2. 共享本地的web服务【都2020了,懂的都懂,分享一个很实用的场景参考文章
  3. 实现本地流量开放访问(kexue上网)【一种场景,你买了一个ji场账号但是限制了只能一台主机可在线kexue上网,(土豪应该不会看这些东西的吧),但是你还有手机,还有平板,还有其他设备以及还有虚拟机中的主机也需要,甚至你还有室友需要,这个时候,就非常有用了,你首先需要找到你的dai理软件监听的端口,然后将你们局域网的所有流量都转发到该端口上,是不是就ok了? 没错,这里不方便多说了…】

动态转发

Specifies a local dynamic application-level port forwarding. This works by allocating a socket to listen to port on the local side, optionally bound to the specified bind address. Whenever a connection is made to this port, the connection is forwarded over the secure channel, and the application protocol is then used to determine where to connect to from the remote machine. Currently the SOCKS4 and SOCKS5 protocols are supported, and ssh will act as a SOCKS server. Only root can forward privileged ports. Dynamic port forwardings can also be specified in the configuration file.

最简易的kexue上网服务器了,只要目标主机可以xx,即可不用指定远程主机端口就可进行端口转发,即通过socks5协议,关于原理去了解一下socks5协议就明白了…

ssh -fCND 8000 root@95.179.216.183

这个就不说太多了…
在这里插入图片描述

远程autossh持久化

远程转发的时候通常会因为超时而自动关闭我们建立的隧道,在渗透中有一些应用场景…
安装

sudo apt install autossh # ubuntu
yum install autossh # centos
sudo pacman -S autossh # archlinux
-M specifies monitor port.

这里一个特别的参数就是-M,指定一个本地主机端口监视 ssh 连接状态,如果因为超时关闭就会会自动重连,从而建立持久化的隧道…

autossh -M 6666 ssh -fgCNR 0.0.0.0:8080:127.0.0.1:1070 root@moddemod.top

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值