# 宝塔安全建议

**宝塔站长群** [**https://TG.HostCLi.COM**](https://tg.hostcli.com)

### 禁止通过IP访问网站 <a href="#ban-ip" id="ban-ip"></a>

1、宝塔面板可以设置服务器禁ping，ping不到，就不会有后续的暴力破解登录ssh等安全隐患

&#x20;     教程：宝塔面板左边栏 -> 安全 -> 禁ping

2.1、通过宝塔面板新建一个网站，将本机所有IP当作域名填写进去，一行一个IP；

2.2、添加完成后，在 站点->伪静态 中收入以下几个方案：

> ### Nginx方案：（CentOS / Debian / Ubuntu） <a href="#ban-ip-nginx" id="ban-ip-nginx"></a>
>
> #### 方案一：访问IP时返回444状态码，直接关闭响应（更像不存在） <a href="#ban-ip-nginx-1" id="ban-ip-nginx-1"></a>
>
> ```
> location / {
> 	 return       444;
> }
> ```
>
> ⚠️ 444 是 Nginx 特有的状态码，表示 直接断开连接，不返回任何内容。
>
> &#x20;    大多数扫描器看到的是“连接被重置/无响应”，会认为这个 IP 没有网站。
>
> #### 方案二：返回空响应（最干净） <a href="#ban-ip-nginx-2" id="ban-ip-nginx-2"></a>
>
> ```
> location / {
> 	 return       204;
> }
> ```
>
> ⚠️ 204 表示 No Content，客户端会认为请求成功，但啥也没有。
>
> &#x20;    对普通人/扫描器来说，看起来像是“空站点”。
>
> #### 方案三：返回一个错误码（让人误判） <a href="#ban-ip-nginx-3" id="ban-ip-nginx-3"></a>
>
> ```
> location / {
> 	 return       495;
> }
> ```
>
> ⚠️ 204 表示 No Content，客户端会认为请求成功，但啥也没有。
>
> &#x20;    对普通人/扫描器来说，看起来像是“空站点”。

> ### Apache (httpd) <a href="#ban-ip-apache" id="ban-ip-apache"></a>
>
> #### 方案一：mod\_security 丢弃请求 <a href="#ban-ip-apache-1" id="ban-ip-apache-1"></a>
>
> ```
> SecRule REQUEST_URI ".*" "id:444,phase:1,deny,status:444,log"
> ```
>
> * 这样 Apache 收到请求后直接 丢弃，不返回 HTTP。
>
> * 但是 status:444 只是写在日志里，客户端感受到的是 连接被断开。
>
> * 等效于 Nginx 的 444。
>
> #### 方案二：防火墙层面阻断（推荐） <a href="#ban-ip-apache-2" id="ban-ip-apache-2"></a>
>
> 直接在 iptables 或 firewalld 里对裸 IP 访问丢包：
>
> ```
> iptables -A INPUT -p tcp --dport 80 -m string --string "Host: " --algo bm --to 70 -j DROP
> ```
>
> 只允许带 Host 域名的请求，IP 直连丢弃
>
> Apache 本身没有“挂起/断开”能力，只能借助安全模块或防火墙。

> ### Windows IIS <a href="#ban-ip-windows" id="ban-ip-windows"></a>
>
> #### 方案一：IIS 动态筛选请求 + 直接终止连接 <a href="#ban-ip-windows-1" id="ban-ip-windows-1"></a>
>
> * 安装 IIS Request Filtering 或 URL Rewrite 模块；
> * 配置规则：如果 Host 不是指定域名 → Abort Request（直接终止 TCP）。
>
> 例如在 web.config 里加：
>
> ```
> <system.webServer>
>   <rewrite>
>     <rules>
>       <rule name="Block IP access" stopProcessing="true">
>         <match url=".*" />
>         <conditions>
>           <add input="{HTTP_HOST}" pattern="^example\.com$" negate="true" />
>         </conditions>
>         <action type="CustomResponse" statusCode="444" statusReason="Connection Closed" statusDescription="Nginx-style 444"/>
>       </rule>
>     </rules>
>   </rewrite>
> </system.webServer>
> ```
>
> IIS 可以配置成直接关闭连接（浏览器看到的就是连接失败）。
>
> #### 方案二：Windows 防火墙过滤 <a href="#ban-ip-windows-2" id="ban-ip-windows-2"></a>
>
> 直接在 iptables 或 firewalld 里对裸 IP 访问丢包：
>
> 直接用防火墙丢掉所有 IP 直连请求，只允许 Host 为域名的流量通过。

### 修改宝塔默认配置： <a href="#change-default" id="change-default"></a>

修改默认SSH端口：[修改方法请自行google搜索](https://www.google.com/search?q=centos+%E6%94%B9+ssh+%E7%AB%AF%E5%8F%A3)；

登陆宝塔面板->安全  *启用SSH*  <mark style="background-color:red;">关闭</mark>；<mark style="color:green;">以免ssh root密码被暴力破解，禁止ssh登陆，只能宝塔登陆；</mark>

登陆宝塔面板->面板设置->面板端口 <mark style="background-color:purple;">修改默认端口</mark>；<mark style="color:green;">以免被批量扫描到宝塔端口、定向爆破；</mark>

登陆宝塔面板->面板设置->安全入口 <mark style="background-color:orange;">修改宝塔路径</mark>；

登陆宝塔面板->面板设置-><mark style="background-color:orange;">修改面板用户 和 面板密码;</mark>

### 善用计划任务+自动备份： <a href="#backup" id="backup"></a>

如：应用商店中搜索【谷歌云网盘】或其他网盘并安装、配置

然后通过计划任务，分别创建两条定时备份：备份网站程序/目录、备份数据库 到谷歌网盘；

![示例：计划任务+备份网站到谷歌网盘](https://1335146840-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F5FHc2JaRj2pmxKLktJe4%2Fuploads%2F5XyIdOFaWNsL2ohb2gvg%2Fimage.png?alt=media\&token=8432425f-8270-4246-9b0d-251f1e77da04)

### 删除登录日志： <a href="#delete-log" id="delete-log"></a>

![](https://1335146840-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F5FHc2JaRj2pmxKLktJe4%2Fuploads%2F1voIQNB0FaKOXdEvIPRL%2F22.png?alt=media\&token=9b782cd3-3113-47ad-9573-938b3f94edf3)

部分用户可能有这个需要；也可以配合[跳板机登录服务器](https://www.nextcli.com/cart?fid=4)+[跳板机的计划任务](https://docs.nextcli.com/jumpserver/security#delete-log)，更加安全；

&#x20;[CentOS7 系统清空删除系统日志的方法>>](https://cloud.tencent.com/developer/article/1969592) 将代码放到跳板机、宝塔计划任务内即可。

以上为[Centos清理ssh登录日志](https://www.google.com.hk/search?q=centos+%E6%B8%85%E7%90%86+ssh+%E7%99%BB%E5%BD%95+%E6%97%A5%E5%BF%97)，其他系统请自行搜索命令；

```
cat /dev/null > /var/log/boot.log
cat /dev/null > /var/log/btmp
cat /dev/null > /var/log/cron
cat /dev/null > /var/log/dmesg
cat /dev/null > /var/log/firewalld
cat /dev/null > /var/log/grubby
cat /dev/null > /var/log/lastlog
cat /dev/null > /var/log/mail.info
cat /dev/null > /var/log/maillog
cat /dev/null > /var/log/messages
cat /dev/null > /var/log/secure
cat /dev/null > /var/log/spooler
cat /dev/null > /var/log/syslog
cat /dev/null > /var/log/tallylog
cat /dev/null > /var/log/wpa_supplicant.log
cat /dev/null > /var/log/wtmp
cat /dev/null > /var/log/yum.log
```

### 其他建议： <a href="#others" id="others"></a>

禁止数据库等端口的外网访问；

尽量不要使用非官方提供、第三方破解的建站程序，大多数服务器被黑主要是因为程序有后门，跟宝塔没有直接、间接的关系；

**宝塔站长群** [**https://TG.HostCLi.COM**](https://tg.hostcli.com)

End.


---

# Agent Instructions: 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://support.hostcli.com/security.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.
