[绝对原创] ssh 使用新法:公网(合法 ip)用户访问内网(私有 ip)服务器(http,ftp,sshd,cvs...),内网的朋友

由 quanliking 在 05-05-2003 00:10 发表:

[绝对原创] ssh 使用新法:公网(合法 ip)用户访问内网(私有 ip)服务器(http,ftp,sshd,cvs...),内网的朋友不妨一看。

内网的朋友苦于没有合法 ip,不能对外提供 internet 服务。解决方案很多,可以通过在网关做端口映射,或其他的辅助软件等。

本文介绍两种比较简单实用的方法,利用 ssh 这个强大的工具。

(以下方法不分平台,都适用)

案例一、

内网主机 A ,开了 http,ftp ,http ,vnc,sshd,socks5,cvs 等服务。无合法 ip 地址。

外网主机 B ,开了 sshd 服务。有合法 ip : 218.xxx.xxx.xxx

我们的目的是让 B 能访问 A 上的各种服务。

步骤:

1、A 知道 B ip 后,先用 ssh client 连上 B,命令如下:

ssh -R 1234:localhost:21 -l root 218.xxx.xxx.xxx

解释:

关于 ssh 的参数,请看 ssh --help

-L listen-port:host ort Forward local port to remote address

-R listen-port:host ort Forward remote port to local address

-L local (本地) -R :remote (远程)

-R 1234:localhost:21 其实做了个“端口转发(forward)"。

意思是主机 A 把本地的 21端口(对应ftp服务)映射为 B 的1234 端口(任意未被占用),同时 A 监听 B 的1234 端口。

在 B 上用 netstat -al | grep 1234 ,你能看到这个监听连接。

任何发送到 B 1234 端口的请求将被传送到 A的 21 端口。

2、B 用 ftp 工具(任意,如gftp) 连本地的 1234 端口,输入 A 的 ftp 用户和密码。

ftp localhost 1234

千万不要觉的奇怪,为什么连的是本地的地址。

举个不恰当例子,相当于 A 在 B 的房间里装了个窃听器(监听端口),那么 B 在房间里说的话就通过窃听器传送到了 A。

3、推广:

如果 B 没占用 21 端口的话,那么可以写成:

A使用: ssh -R 21:localhost:21 -l root 218.xxx.xxx.xxx

B使用: ftp localhost

如果你想使用 A 上的 http 或其他服务,只需改变服务端口:

http服务 :

A使用:ssh -R 1234:localhost:80 -l root 218.xxx.xxx.xxx

B使用:w3m http://localhost:1234

sshd服务:

A使用:ssh -R 1234:localhost:22 -l root 218.xxx.xxx.xxx

B使用:ssh localhost -p 1234

vnc 服务:

A使用:ssh -R 1234:localhost:5901(其他) -l root 218.xxx.xxx.xxx

B使用:vncviewer localhost:1

socks5服务:

A使用:ssh -R 1234:localhost:1080 -l root 218.xxx.xxx.xxx

B 略

cvs 服务:

A使用:ssh -R 1234:localhost:2401 -l root 218.xxx.xxx.xxx

B使用:cvs -d server:root@localhost:1234/home/cvsroot login

这里是否一定要用 root ,涉及到权限问题,具体还得靠大家来总结经验。

案例二、

部分朋友会问了,这样的话只是两台机器的互相通讯,如何让广域网的人都能访问呢?

聪明的你,这时候可能已经有了答案。

内网主机 A ,开了 http,ftp ,http ,vnc,sshd,socks5,cvs等服务。无合法 ip 地址。

外网主机 B ,开了 sshd 服务。有合法 ip : 218.xxx.xxx.xxx

我们的目的是让 internet 上的任何主机能访问 A 上的各种服务。

步骤:

1、首先,B 的sshd 服务端做点小小的设置:

vi /etc/ssh/sshd.config

加入 GatewayPorts yes

然后重启 sshd 服务: /etc /init.d/ssh restart 或 /etc/init.d/sshd restart

(解释:

不加,默认会把监听端口绑定在 localhost 或 lo(127.0.0.1),这样除了 B自身别人是没法访问监听端口的。

加入 GatewayPorts yes,把监听端口绑定到 0.0.0.0 ,这样外部的所有机器都能访问到这个监听端口。

主要是考虑安全性问题,默认情况,只允许本地访问。

这里才是真正的难点,实验了一个晚上,累人呀!给点鼓励吧 :)

2、A 知道 B ip 后,先用 ssh client 连上 B,命令如下:

ssh -R 21:localhost:21 -l root 218.xxx.xxx.xxx

(事先确定 B 的21 端口未被占用)

3、分布在 internet 的其它客户机使用 ftp 工具(任意),连 B 21端口。

ftp 218.xxx.xxx.xxx 21

你会发现自己连上了内网 A 的ftp 服务。

此法和案例一完全一样。

internet --------->> B 21 端口----------->>A 21端口

可以叫做端口转发,或隧道技术,也可以称之为跳板(B),或反弹 。呵呵,我瞎说的。。。

可能遇到的问题:

Country:/etc# ssh localhost -p 1234

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

@ WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED! @

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

IT IS POSSIBLE THAT SOMEONE IS DOING SOMETHING NASTY!

Someone could be eavesdropping on you right now (man-in-the-middle attack)!

It is also possible that the RSA host key has just been changed.

The fingerprint for the RSA key sent by the remote host is

be:5f:d2:45:66:4d:0c:9e:2b:6b:45:65:a7:b2:85:28.

Please contact your system administrator.

Add correct host key in /root/.ssh/known_hosts to get rid of this message.

Offending key in /root/.ssh/known_hosts:11

RSA host key for localhost has changed and you have requested strict checking.

Host key verification failed.

Country:/etc# ssh localhost -p 1234

root@localhost's password:

Last login: Mon May 5 02:39:53 2003 from localhost

localhost root #

如上问题,请删除 ~/.ssh/known_hosts,然后再试。

点评:

当然 ssh 还有很多的功能没有用,如先用 ssh 连接 上去后,可以用 scp命令来存取文件,等等。

scp -P xxx user@host ath/file

其它突破网关传送文件的方式也千变万化。

优点是:

可以突破网关,一般情况下,向网管要求在网关上给你做端口映射是不现实的,但用此法你可以让要好的朋友给你做。

使用方案一:比较点对点传送文件比较方便,或使用ssh进行远程控制内网。

ssh本身是加密,保证安全可靠。

缺点也不少:

使用 ssh 加密,势必影响性能,可以用 -C 选项调节压缩率。

如果象方案二使用额外的服务器,数据都要服务器中转(我是这样认为,没跟踪过),势必影响速度。

公网的服务器不好找。

建议:恳请编程高手们根据类似得原理,做个端口转发小工具,效果会更好。

本文,雕虫小计,漏洞无数。旨在提供一种如何解决问题的思路,请大侠不要笑话。也请大家多多支持,共同提高。

特别感谢 :sleeve ,x11 兄,是他们帮助一起实现的。以及 irc.linuxfire.com 中的大伙:)


由 silver810212 在 05-05-2003 00:13 发表:


哈哈,极为有用。感谢quanliking老大。

quanliking,你真是“一个好人”


生命不息,学习不止!

你帮助我,我也帮助你。

MSN:[email protected]


由 kemean 在 05-05-2003 00:33 发表:

我怎么连不上?

我想在内网中试试,可是我按上述方法执行后,情况如下:

ssh:connect to host 192.168.101.35 port 22:connection refused

怎么会被拒绝呢?


由 quanliking 在 05-05-2003 00:52 发表:


具体你用的命令是什么,你的网络情况请告诉我。

方法是经过检验的。:)


There are two distinct opinions:

One would say, "The love of money is the root of all evil."

The other, "The lack of money is the root of all evil."

Which one i should take, that is the question.


由 kemean 在 05-05-2003 00:56 发表:


命令是

ssh -R 2000:kemean:80 -l root 192.168.101.35

Published At
Categories with 服务器类
Tagged with
comments powered by Disqus