惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Forbes - Security
Forbes - Security
GbyAI
GbyAI
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
S
SegmentFault 最新的问题
Y
Y Combinator Blog
Recorded Future
Recorded Future
博客园 - Franky
I
InfoQ
T
The Blog of Author Tim Ferriss
Recent Announcements
Recent Announcements
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
博客园_首页
阮一峰的网络日志
阮一峰的网络日志
T
Tailwind CSS Blog
Cyberwarzone
Cyberwarzone
The Register - Security
The Register - Security
H
Hackread – Cybersecurity News, Data Breaches, AI and More
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
雷峰网
雷峰网
P
Palo Alto Networks Blog
G
GRAHAM CLULEY
Cloudbric
Cloudbric
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
MongoDB | Blog
MongoDB | Blog
F
Full Disclosure
Google DeepMind News
Google DeepMind News
Recent Commits to openclaw:main
Recent Commits to openclaw:main
C
Check Point Blog
爱范儿
爱范儿
The GitHub Blog
The GitHub Blog
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
W
WeLiveSecurity
T
Threat Research - Cisco Blogs
U
Unit 42
N
Netflix TechBlog - Medium
The Cloudflare Blog
Spread Privacy
Spread Privacy
Microsoft Azure Blog
Microsoft Azure Blog
美团技术团队
T
Troy Hunt's Blog
Engineering at Meta
Engineering at Meta
H
Heimdal Security Blog
TaoSecurity Blog
TaoSecurity Blog
C
Cybersecurity and Infrastructure Security Agency CISA
T
Tenable Blog
B
Blog
S
Securelist
H
Hacker News: Front Page
Google Online Security Blog
Google Online Security Blog
G
Google Developers Blog

博客园 - longbigfish

部署(https证书) https证书问题(本地) 参数更新 Ubuntu 24安装Neo4j详细教程 protect 紧急 手机 刷脏页的两种模式 python中的多线程陷阱与pytorch分布式执行机制 git之复合指令和submodule rpc编程示例 mpi编程 cifs远程挂载 使用脚本进入一个命令行控制台,并预设执行的命令列表 cifs挂载远程文件出现 No such device or address错误 longtable 跨越多个页面时,如何在跨页时自动断行并加上横线及去掉页眉 matplotlib中文显示-微软雅黑 latex编译过程-关于嵌入所有字体 python做图笔记 linux启动全过程 连接并同步windows下的git仓库
反向ssh
longbigfish · 2018-03-17 · via 博客园 - longbigfish

参考 https://www.thegeekstuff.com/2013/11/reverse-ssh-tunnel/

https://www.howtoforge.com/reverse-ssh-tunneling

1. 背景

机器A的ip 192.168.0.100,机器B的ip 123.123.123.123

机器A是一个内网机器,可以访问外网。机器B是一个公网机器。也就是说,机器A可以ssh上机器B,但是机器B不能连接机器A。

另一种情况:机器A虽然也是有公网ip,但是由于安全原因,无法被外网访问。

现在,我们想通过机器B去访问机器A。

假设机器A和B都装有ssh服务端,服务端口分别为1001和1002

2. 方案

  ssh的 -R参数

step1:

首先,在机器A上运行  

ssh -R 7000:localhost:1001 B_user@123.123.123.123 -p 1002

这样机器A就主动和机器B建立了一个ssh连接。只要机器该建立连接的tty不关闭,此连接一直存在。当然,我们可以将该连接放入后台,同样保证该连接一直存在。

(使用-fn参数?)

一般来说,机器A连接B只需要ssh username@123.123.123.123 就可以了。所以, -R 7000:localhost:1001 是做什么用呢?正是为了做反向连接。

这个-R参数在连接B机器的同时,对B机器做了这样的请求:在localhost端口7000上监听,并将在此接收到的数据包通过我们这个ssh连接转发给我。B按照该请求执行是ssh协议允许的,在B机器上7000端口监听的仍然是sshd进程,它可以决定将数据包用sshd下的哪个线程进行处理。当然,此后发往B机器上7000端口的数据都会倍sshd进程收到,并由A与B的ssh通道发送给A。

另外,A机器收到这个数据后,认为这个数据是发给1001号端口的,即A机器上的ssh服务。整个过程都是在ssh服务框架下完成的。

step2:

在B机器上任开一个tty,执行

ssh A_user@localhost -p 7000

 即可连接到A机器了。这个命令的意思是,使用用户名A_user连接本机的7000端口。由于端口为7000的数据包会被通过step1里建立的连接发送给A机器,所以这个连接请求也就最终发给A机器了。

在B机器上使用lsof -i4 -n 可以看到ssh 再127.0.0.1:7000上监听。

在机器A上使用nmap -A 123.123.123.123/32 -p 7000 探测7000端口是关闭,因为B机器监听的地址是本地地址。

但即使将上面命令的localhost改为123.123.123.123,链接后也发现是在127.0.0.1:7000上监听。

3. 扩展

  另一台可以访问到B机器的机器C,如何通过B机器访问A呢?

       首先,C可以ssh到B,然后再一步ssh到A。

但是,有时候需要再C上直接ssh到A,该怎么办呢?
      方案是在B上使用iptables进行端口转发。
    假设A对B发起反向ssh,B在127.0.0.1:7000上监听, 而我们想在C上通过B的端口7000直接ssh到A,由于C只能连接B的公网ip 123.123.123.123,所以我们需要一个新的端口假设7777.
     我们在B上将发往123.123.123.123:7777的数据转发到127.0.0.1:7000

 iptables -t nat -A PREROUTING -d 123.123.123.123 -p tcp -m tcp --dport 7777 -j DNAT --to-destination 127.0.0.1:7000

特别注意: 转发到localhost一般需要手工开启本地路由,将/proc/sys/net/ipv4/conf/all/route_localnet写入1

        参考https://superuser.com/questions/661772/iptables-redirect-to-localhost

 

 -----------

如果在

ssh -R 7000:localhost:22 B_user@123.123.123.123

 命令中将localhost改为*或123.123.123.123,使用 lsof -i4 也可以看到在7000端口监听,但是监听的地址仍然是127.0.0.1。 而且此时无法进行反向ssh,提示

ssh_exchange_identification: Connection closed by remote host