Frp是一款高性能的反向代理应用,专注于内网穿透,支持TCP、UDP、HTTP、HTTPS等多种协议。本文将深入讲解Frp的工作原理、配置方法、高级特性,以及在渗透测试和运维场景中的实战应用。

一、Frp概述

1.1 项目信息

属性 说明
项目地址 https://github.com/fatedier/frp
开发语言 Go
GitHub Stars 85k+
开源协议 Apache-2.0
最新版本 v0.52.x
支持平台 Windows / Linux / macOS / ARM

1.2 核心架构

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
┌─────────────────────────────────────────────────────────────┐
│ Frp 架构 │
├─────────────────────────────────────────────────────────────┤
│ │
│ 内网环境 公网 外部访问 │
│ ┌─────────┐ ┌─────────┐ ┌─────────┐ │
│ │ 内网服务 │ │ frps │ │ 用户 │ │
│ │ (Web/SSH)│◄────────────│ (服务端) │◄────────│ (访问者) │ │
│ └────┬────┘ └────┬────┘ └─────────┘ │
│ │ │ │
│ │ ┌─────────┐ │ │
│ └────│ frpc │────────┘ │
│ │ (客户端) │ │
│ └─────────┘ │
│ │
│ 数据流向: │
│ 用户 → frps(公网) → frpc(内网) → 内网服务 │
│ │
└─────────────────────────────────────────────────────────────┘

1.3 支持的代理类型

类型 说明 使用场景
tcp TCP端口转发 SSH、数据库、任意TCP服务
udp UDP端口转发 DNS、游戏、VoIP
http HTTP反向代理 Web服务,支持域名
https HTTPS反向代理 加密Web服务
stcp 安全TCP隧道 点对点加密通信
sudp 安全UDP隧道 点对点UDP加密
xtcp P2P穿透 NAT穿透,减少服务器带宽
tcpmux TCP多路复用 多服务共享端口

二、安装与基础配置

2.1 安装方式

安装Frp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# 下载最新版本
# https://github.com/fatedier/frp/releases

# Linux AMD64
wget https://github.com/fatedier/frp/releases/download/v0.52.3/frp_0.52.3_linux_amd64.tar.gz
tar -xzf frp_0.52.3_linux_amd64.tar.gz
cd frp_0.52.3_linux_amd64

# 文件说明
# frps - 服务端程序
# frpc - 客户端程序
# frps.toml - 服务端配置
# frpc.toml - 客户端配置

# Windows
# 下载 frp_x.x.x_windows_amd64.zip 解压即可

# Docker部署
docker run -d --name frps \
-p 7000:7000 \
-p 7500:7500 \
-v /path/to/frps.toml:/etc/frp/frps.toml \
snowdreamtech/frps

# 系统服务 (Linux)
sudo cp frps /usr/local/bin/
sudo cp frps.toml /etc/frp/

# 创建systemd服务
sudo cat > /etc/systemd/system/frps.service << 'EOF'
[Unit]
Description=Frp Server Service
After=network.target

[Service]
Type=simple
User=nobody
Restart=on-failure
RestartSec=5s
ExecStart=/usr/local/bin/frps -c /etc/frp/frps.toml

[Install]
WantedBy=multi-user.target
EOF

sudo systemctl daemon-reload
sudo systemctl enable frps
sudo systemctl start frps

2.2 服务端配置

frps.toml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# frps.toml - 服务端配置

# 基础配置
bindAddr = "0.0.0.0"
bindPort = 7000

# 认证配置
auth.method = "token"
auth.token = "your_secure_token_here"

# Dashboard配置
webServer.addr = "0.0.0.0"
webServer.port = 7500
webServer.user = "admin"
webServer.password = "admin123"

# 日志配置
log.to = "/var/log/frps.log"
log.level = "info"
log.maxDays = 7

# HTTP/HTTPS代理端口
vhostHTTPPort = 80
vhostHTTPSPort = 443

# 子域名配置
subDomainHost = "frp.example.com"

# 端口范围限制
allowPorts = [
{ start = 10000, end = 20000 }
]

# TLS配置
transport.tls.force = false
# transport.tls.certFile = "/path/to/server.crt"
# transport.tls.keyFile = "/path/to/server.key"

# 连接池
transport.maxPoolCount = 10

# 带宽限制 (每个代理)
# transport.bandwidthLimit = "1MB"
# transport.bandwidthLimitMode = "client"

2.3 客户端配置

frpc.toml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# frpc.toml - 客户端配置

# 服务端连接
serverAddr = "your_server_ip"
serverPort = 7000

# 认证
auth.method = "token"
auth.token = "your_secure_token_here"

# 日志
log.to = "/var/log/frpc.log"
log.level = "info"

# TLS
transport.tls.enable = false

# 连接配置
transport.poolCount = 5
transport.heartbeatInterval = 30
transport.heartbeatTimeout = 90

# 代理配置
[[proxies]]
name = "ssh"
type = "tcp"
localIP = "127.0.0.1"
localPort = 22
remotePort = 10022

[[proxies]]
name = "web"
type = "http"
localIP = "127.0.0.1"
localPort = 80
customDomains = ["web.frp.example.com"]

[[proxies]]
name = "web-https"
type = "https"
localIP = "127.0.0.1"
localPort = 443
customDomains = ["secure.frp.example.com"]

三、常用代理配置

3.1 TCP端口转发

TCP代理
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# SSH转发
[[proxies]]
name = "ssh"
type = "tcp"
localIP = "127.0.0.1"
localPort = 22
remotePort = 10022

# 数据库转发
[[proxies]]
name = "mysql"
type = "tcp"
localIP = "192.168.1.100"
localPort = 3306
remotePort = 13306

# RDP远程桌面
[[proxies]]
name = "rdp"
type = "tcp"
localIP = "127.0.0.1"
localPort = 3389
remotePort = 13389

# 多端口转发
[[proxies]]
name = "range-tcp"
type = "tcp"
localIP = "127.0.0.1"
localPort = 8080
remotePort = 18080

[[proxies]]
name = "range-tcp-2"
type = "tcp"
localIP = "127.0.0.1"
localPort = 8081
remotePort = 18081

3.2 HTTP/HTTPS代理

HTTP代理
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# HTTP代理 (使用域名)
[[proxies]]
name = "web"
type = "http"
localIP = "127.0.0.1"
localPort = 8080
customDomains = ["www.example.com"]

# 子域名方式
[[proxies]]
name = "web-subdomain"
type = "http"
localIP = "127.0.0.1"
localPort = 8080
subdomain = "myapp"
# 访问: myapp.frp.example.com

# HTTP基本认证
[[proxies]]
name = "web-auth"
type = "http"
localIP = "127.0.0.1"
localPort = 8080
customDomains = ["secure.example.com"]
httpUser = "admin"
httpPassword = "password123"

# URL路由
[[proxies]]
name = "web-api"
type = "http"
localIP = "127.0.0.1"
localPort = 8080
customDomains = ["api.example.com"]
locations = ["/api", "/v1"]

# HTTPS代理
[[proxies]]
name = "web-https"
type = "https"
localIP = "127.0.0.1"
localPort = 443
customDomains = ["secure.example.com"]

# 修改Host头
[[proxies]]
name = "web-host"
type = "http"
localIP = "127.0.0.1"
localPort = 80
customDomains = ["proxy.example.com"]
hostHeaderRewrite = "internal.local"

3.3 UDP代理

UDP代理
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# DNS转发
[[proxies]]
name = "dns"
type = "udp"
localIP = "127.0.0.1"
localPort = 53
remotePort = 10053

# 游戏服务器
[[proxies]]
name = "game"
type = "udp"
localIP = "127.0.0.1"
localPort = 27015
remotePort = 27015

3.4 安全隧道 (STCP/XTCP)

安全隧道
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# STCP - 安全TCP隧道 (需要访问者也运行frpc)

# 服务提供方 (frpc)
[[proxies]]
name = "secret_ssh"
type = "stcp"
secretKey = "your_secret_key"
localIP = "127.0.0.1"
localPort = 22

# 访问方 (另一个frpc)
[[visitors]]
name = "secret_ssh_visitor"
type = "stcp"
serverName = "secret_ssh"
secretKey = "your_secret_key"
bindAddr = "127.0.0.1"
bindPort = 10022
# 访问: ssh -p 10022 user@127.0.0.1

# XTCP - P2P穿透 (减少服务器带宽)

# 服务提供方
[[proxies]]
name = "p2p_ssh"
type = "xtcp"
secretKey = "your_secret_key"
localIP = "127.0.0.1"
localPort = 22

# 访问方
[[visitors]]
name = "p2p_ssh_visitor"
type = "xtcp"
serverName = "p2p_ssh"
secretKey = "your_secret_key"
bindAddr = "127.0.0.1"
bindPort = 10022
keepTunnelOpen = true

四、高级特性

4.1 负载均衡

负载均衡配置
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# 多后端负载均衡
[[proxies]]
name = "web-lb-1"
type = "http"
localIP = "192.168.1.101"
localPort = 80
customDomains = ["www.example.com"]
loadBalancer.group = "web"
loadBalancer.groupKey = "web_key"

[[proxies]]
name = "web-lb-2"
type = "http"
localIP = "192.168.1.102"
localPort = 80
customDomains = ["www.example.com"]
loadBalancer.group = "web"
loadBalancer.groupKey = "web_key"

[[proxies]]
name = "web-lb-3"
type = "http"
localIP = "192.168.1.103"
localPort = 80
customDomains = ["www.example.com"]
loadBalancer.group = "web"
loadBalancer.groupKey = "web_key"

4.2 健康检查

健康检查配置
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# TCP健康检查
[[proxies]]
name = "web-health"
type = "tcp"
localIP = "127.0.0.1"
localPort = 80
remotePort = 10080
healthCheck.type = "tcp"
healthCheck.timeoutSeconds = 3
healthCheck.maxFailed = 3
healthCheck.intervalSeconds = 10

# HTTP健康检查
[[proxies]]
name = "api-health"
type = "http"
localIP = "127.0.0.1"
localPort = 8080
customDomains = ["api.example.com"]
healthCheck.type = "http"
healthCheck.path = "/health"
healthCheck.timeoutSeconds = 5
healthCheck.maxFailed = 3
healthCheck.intervalSeconds = 30

4.3 带宽限制

带宽限制
1
2
3
4
5
6
7
8
9
10
11
12
13
# 服务端全局限制 (frps.toml)
transport.bandwidthLimit = "10MB"
transport.bandwidthLimitMode = "server"

# 客户端限制 (frpc.toml)
[[proxies]]
name = "limited-proxy"
type = "tcp"
localIP = "127.0.0.1"
localPort = 80
remotePort = 10080
transport.bandwidthLimit = "1MB"
transport.bandwidthLimitMode = "client"

4.4 插件系统

插件配置
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# HTTP代理插件
[[proxies]]
name = "http_proxy"
type = "tcp"
remotePort = 10080
[proxies.plugin]
type = "http_proxy"
httpUser = "admin"
httpPassword = "password"

# SOCKS5代理插件
[[proxies]]
name = "socks5"
type = "tcp"
remotePort = 11080
[proxies.plugin]
type = "socks5"
username = "admin"
password = "password"

# 静态文件服务
[[proxies]]
name = "static_file"
type = "tcp"
remotePort = 10081
[proxies.plugin]
type = "static_file"
localPath = "/var/www/html"
stripPrefix = "static"
httpUser = "admin"
httpPassword = "password"

# Unix域套接字
[[proxies]]
name = "unix_socket"
type = "tcp"
remotePort = 10082
[proxies.plugin]
type = "unix_domain_socket"
unixPath = "/var/run/docker.sock"

# HTTPS转HTTP
[[proxies]]
name = "https2http"
type = "https"
customDomains = ["secure.example.com"]
[proxies.plugin]
type = "https2http"
localAddr = "127.0.0.1:80"
crtPath = "/path/to/server.crt"
keyPath = "/path/to/server.key"

五、渗透测试应用

5.1 内网代理搭建

渗透场景配置
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# 场景: 获取内网主机权限后,搭建代理通道

# 客户端配置 (内网主机)
serverAddr = "attacker_vps_ip"
serverPort = 7000
auth.token = "pentest_token"

# SOCKS5代理 (用于Proxychains)
[[proxies]]
name = "socks5_proxy"
type = "tcp"
remotePort = 11080
[proxies.plugin]
type = "socks5"

# HTTP代理 (用于浏览器)
[[proxies]]
name = "http_proxy"
type = "tcp"
remotePort = 18080
[proxies.plugin]
type = "http_proxy"

# 内网Web服务转发
[[proxies]]
name = "internal_web"
type = "http"
localIP = "192.168.1.100"
localPort = 80
customDomains = ["internal.attacker.com"]

# 内网数据库转发
[[proxies]]
name = "internal_mysql"
type = "tcp"
localIP = "192.168.1.200"
localPort = 3306
remotePort = 13306

# 域控RDP
[[proxies]]
name = "dc_rdp"
type = "tcp"
localIP = "192.168.1.1"
localPort = 3389
remotePort = 13389

5.2 多级代理

多级代理
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
多级代理拓扑:

攻击者 → VPS(frps) → 边界主机(frpc1) → 内网主机(frpc2) → 核心网段

# VPS服务端 (frps.toml)
bindPort = 7000
auth.token = "level1_token"

# 边界主机 (frpc1.toml)
serverAddr = "vps_ip"
serverPort = 7000
auth.token = "level1_token"

[[proxies]]
name = "level2_frps"
type = "tcp"
localIP = "127.0.0.1"
localPort = 7001
remotePort = 17001

# 边界主机同时运行frps
# frps2.toml
bindPort = 7001
auth.token = "level2_token"

# 内网主机 (frpc2.toml)
serverAddr = "边界主机内网IP"
serverPort = 7001
auth.token = "level2_token"

[[proxies]]
name = "core_socks"
type = "tcp"
remotePort = 11080
[proxies.plugin]
type = "socks5"

5.3 隐蔽通信

隐蔽配置
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# 使用HTTPS伪装
# frps.toml
bindPort = 443
transport.tls.force = true
transport.tls.certFile = "/path/to/server.crt"
transport.tls.keyFile = "/path/to/server.key"

# frpc.toml
serverAddr = "legitimate-domain.com"
serverPort = 443
transport.tls.enable = true
transport.tls.serverName = "legitimate-domain.com"

# 使用WebSocket
# frps.toml
transport.protocol = "websocket"

# frpc.toml
transport.protocol = "websocket"

# 使用KCP (UDP)
# frps.toml
kcpBindPort = 7000

# frpc.toml
transport.protocol = "kcp"

六、安全加固

6.1 认证与加密

安全配置
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# frps.toml - 服务端安全配置

# 强制Token认证
auth.method = "token"
auth.token = "very_long_and_complex_token_here_32chars"

# 强制TLS
transport.tls.force = true
transport.tls.certFile = "/etc/frp/server.crt"
transport.tls.keyFile = "/etc/frp/server.key"
transport.tls.trustedCaFile = "/etc/frp/ca.crt"

# 端口限制
allowPorts = [
{ start = 10000, end = 20000 }
]

# 禁用Dashboard或设置强密码
webServer.addr = "127.0.0.1" # 仅本地访问
webServer.port = 7500
webServer.user = "admin"
webServer.password = "very_strong_password"

# 连接数限制
transport.maxPoolCount = 5

# frpc.toml - 客户端安全配置
transport.tls.enable = true
transport.tls.certFile = "/etc/frp/client.crt"
transport.tls.keyFile = "/etc/frp/client.key"
transport.tls.trustedCaFile = "/etc/frp/ca.crt"
transport.tls.serverName = "frp.example.com"

6.2 访问控制

访问控制
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# IP白名单 (需要配合防火墙)
# 使用iptables限制frps端口访问

# 代理级别认证
[[proxies]]
name = "secure_web"
type = "http"
localIP = "127.0.0.1"
localPort = 80
customDomains = ["secure.example.com"]
httpUser = "admin"
httpPassword = "secure_password"

# STCP安全隧道
[[proxies]]
name = "private_ssh"
type = "stcp"
secretKey = "very_long_secret_key"
localIP = "127.0.0.1"
localPort = 22

七、监控与运维

7.1 Dashboard使用

Dashboard
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
Dashboard功能:
- 查看所有代理状态
- 实时流量统计
- 连接数监控
- 代理配置查看

访问地址: http://frps_ip:7500
用户名/密码: 配置文件中设置

API接口:
GET /api/serverinfo # 服务器信息
GET /api/proxy/tcp # TCP代理列表
GET /api/proxy/udp # UDP代理列表
GET /api/proxy/http # HTTP代理列表
GET /api/proxy/https # HTTPS代理列表
GET /api/traffic # 流量统计

7.2 日志分析

日志配置
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# 详细日志配置
log.to = "/var/log/frps.log"
log.level = "debug" # trace, debug, info, warn, error
log.maxDays = 30
log.disablePrintColor = true

# 日志分析
# 查看连接日志
grep "new proxy" /var/log/frps.log

# 查看错误
grep "error" /var/log/frps.log

# 查看流量
grep "traffic" /var/log/frps.log

# 实时监控
tail -f /var/log/frps.log | grep --line-buffered "proxy"

7.3 自动重连

自动重连
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# 使用systemd自动重启
# /etc/systemd/system/frpc.service
[Unit]
Description=Frp Client Service
After=network.target

[Service]
Type=simple
User=nobody
Restart=always
RestartSec=5s
ExecStart=/usr/local/bin/frpc -c /etc/frp/frpc.toml
ExecReload=/bin/kill -HUP $MAINPID

[Install]
WantedBy=multi-user.target

# 或使用脚本
#!/bin/bash
while true; do
/usr/local/bin/frpc -c /etc/frp/frpc.toml
echo "frpc disconnected, reconnecting in 5s..."
sleep 5
done

八、常见问题

8.1 故障排查

故障排查
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# 连接问题
1. 检查服务端是否运行
ps aux | grep frps
netstat -tlnp | grep 7000

2. 检查防火墙
iptables -L -n
firewall-cmd --list-all

3. 检查Token是否匹配
grep token frps.toml frpc.toml

4. 检查网络连通性
telnet frps_ip 7000

# 代理不通
1. 检查本地服务是否运行
curl http://127.0.0.1:80

2. 检查代理配置
frpc status

3. 查看日志
tail -f /var/log/frpc.log

# 性能问题
1. 检查带宽限制配置
2. 检查连接池大小
3. 考虑使用KCP协议

8.2 性能优化

场景 优化建议
高延迟 使用KCP协议
大流量 增加连接池、使用XTCP
多用户 负载均衡、增加服务器
稳定性 健康检查、自动重连

九、总结

Frp是功能强大且易用的内网穿透工具,在运维和安全测试中都有广泛应用。关键要点:

  1. 架构简单:服务端+客户端模式,易于部署
  2. 协议丰富:支持TCP/UDP/HTTP/HTTPS等多种协议
  3. 安全特性:支持TLS加密、Token认证、STCP安全隧道
  4. 高级功能:负载均衡、健康检查、插件系统
  5. 渗透应用:内网代理、多级穿透、隐蔽通信

掌握Frp可以有效解决内网服务暴露和远程访问的需求。

注意:本文内容仅供授权的安全测试和学习研究使用,未经授权使用内网穿透工具可能违反相关法律法规。