GitHub热门项目-Frp内网穿透原理与高级用法
Frp是一款高性能的反向代理应用,专注于内网穿透,支持TCP、UDP、HTTP、HTTPS等多种协议。本文将深入讲解Frp的工作原理、配置方法、高级特性,以及在渗透测试和运维场景中的实战应用。
一、Frp概述 1.1 项目信息
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 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_amd64docker run -d --name frps \ -p 7000:7000 \ -p 7500:7500 \ -v /path/to/frps.toml:/etc/frp/frps.toml \ snowdreamtech/frps sudo cp frps /usr/local/bin/sudo cp frps.toml /etc/frp/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-reloadsudo systemctl enable frpssudo 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 bindAddr = "0.0.0.0" bindPort = 7000 auth.method = "token" auth.token = "your_secure_token_here" 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 vhostHTTPPort = 80 vhostHTTPSPort = 443 subDomainHost = "frp.example.com" allowPorts = [ { start = 10000 , end = 20000 } ] transport.tls.force = false transport.maxPoolCount = 10
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 serverAddr = "your_server_ip" serverPort = 7000 auth.method = "token" auth.token = "your_secure_token_here" log.to = "/var/log/frpc.log" log.level = "info" 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 [[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 [[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 [[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" [[proxies]] name = "web-auth" type = "http" localIP = "127.0.0.1" localPort = 8080 customDomains = ["secure.example.com" ]httpUser = "admin" httpPassword = "password123" [[proxies]] name = "web-api" type = "http" localIP = "127.0.0.1" localPort = 8080 customDomains = ["api.example.com" ]locations = ["/api" , "/v1" ][[proxies]] name = "web-https" type = "https" localIP = "127.0.0.1" localPort = 443 customDomains = ["secure.example.com" ][[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 [[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 [[proxies]] name = "secret_ssh" type = "stcp" secretKey = "your_secret_key" localIP = "127.0.0.1" localPort = 22 [[visitors]] name = "secret_ssh_visitor" type = "stcp" serverName = "secret_ssh" secretKey = "your_secret_key" bindAddr = "127.0.0.1" bindPort = 10022 [[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 [[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 [[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 transport.bandwidthLimit = "10MB" transport.bandwidthLimitMode = "server" [[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 [[proxies]] name = "http_proxy" type = "tcp" remotePort = 10080 [proxies.plugin] type = "http_proxy" httpUser = "admin" httpPassword = "password" [[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" [[proxies]] name = "unix_socket" type = "tcp" remotePort = 10082 [proxies.plugin] type = "unix_domain_socket" unixPath = "/var/run/docker.sock" [[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" [[proxies]] name = "socks5_proxy" type = "tcp" remotePort = 11080 [proxies.plugin] type = "socks5" [[proxies]] name = "http_proxy" type = "tcp" remotePort = 18080 [proxies.plugin] type = "http_proxy" [[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 [[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) → 核心网段 bindPort = 7000 auth.token = "level1_token" serverAddr = "vps_ip" serverPort = 7000 auth.token = "level1_token" [[proxies]] name = "level2_frps" type = "tcp" localIP = "127.0.0.1" localPort = 7001 remotePort = 17001 bindPort = 7001 auth.token = "level2_token" 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 bindPort = 443 transport.tls.force = true transport.tls.certFile = "/path/to/server.crt" transport.tls.keyFile = "/path/to/server.key" serverAddr = "legitimate-domain.com" serverPort = 443 transport.tls.enable = true transport.tls.serverName = "legitimate-domain.com" transport.protocol = "websocket" transport.protocol = "websocket" kcpBindPort = 7000 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 auth.method = "token" auth.token = "very_long_and_complex_token_here_32chars" 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 } ] webServer.addr = "127.0.0.1" webServer.port = 7500 webServer.user = "admin" webServer.password = "very_strong_password" transport.maxPoolCount = 5 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 [[proxies]] name = "secure_web" type = "http" localIP = "127.0.0.1" localPort = 80 customDomains = ["secure.example.com" ]httpUser = "admin" httpPassword = "secure_password" [[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 GET /api/ proxy/udp GET /api/ proxy/http GET /api/ proxy/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" 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 [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 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是功能强大且易用的内网穿透工具,在运维和安全测试中都有广泛应用。关键要点:
架构简单 :服务端+客户端模式,易于部署
协议丰富 :支持TCP/UDP/HTTP/HTTPS等多种协议
安全特性 :支持TLS加密、Token认证、STCP安全隧道
高级功能 :负载均衡、健康检查、插件系统
渗透应用 :内网代理、多级穿透、隐蔽通信
掌握Frp可以有效解决内网服务暴露和远程访问的需求。
注意 :本文内容仅供授权的安全测试和学习研究使用,未经授权使用内网穿透工具可能违反相关法律法规。