admin管理员组

文章数量:1653594

文章目录

  • 工控机配置
    • enp3s0网络接口配置
    • enp7s0网络接口配置
  • DHCP服务
    • 安装DHCP服务
    • 配置DHCP服务
    • 启动DHCP服务
    • 查看日志
    • 测试连接
    • DHCP相关命令:
    • IP转发和NAT规则
      • 启用IP转发
      • 添加NAT规则
    • 测试连接
  • Danted代理
    • 安装Danted代理
    • 配置Danted代理
    • 启动服务
    • 开机自动运行
    • 查看日志
    • 测试
  • redoskcs透明代理
    • 安装redsocks
    • 配置redsocks
    • 启动redsocks
    • 查看日志
  • iptables
    • 查看所有规则
    • 删除所有规则
    • 添加规则
      • danted不在本地
      • danted在本地
    • 测试
    • 开机自动启动

实验设备

​ 工控机(ubuntu系统),两个路由器。

前提准备

​ 工控机(ubuntu系统)的任一网络接口与外部网络相连(本实验中,将工控机的enp3s0与路由器A的Lan口相连(也可以直接插在墙上或猫上),确保工控机可以正常访问外部网站),将工控机的其他网路接口(本实验使用enp7s0接口)与路由器B的wan口相连。

工作流程

  1. 工控机本机的出站流量通过iptables重定向到redsocks,redsocks将tcp流量转发给danted代理服务器,实现全局透明代理;
  2. 连接路由器B的设备(通过有线或无线任一方式连接路由器B)的出站流量通过iptables重定向到redsocks,redsocks将tcp流量转发给danted代理服务器,实现全局透明代理。

工控机配置

enp3s0网络接口配置

​ enp3s0网络接口直接与外部网络接口(路由器A)相连,在工控机网络设置中设置enp3s0网络接口的IP、DNS和路由等信息为自动获取。(默认情况下都是自动获取)确保使用浏览器可以正常访问页面。

enp7s0网络接口配置

​ enp7s0网络接口与路由器B的wan口相连。在工控机网络设置中设置enp7s0网络接口的ip改为手动设置,并手动设置ip为192.168.127.6,子网掩码为255.255.255.0,(该ip可根据自身喜好随意设置),网关为192.168.127.6

​ (可省略)DNS为8.8.8.8 8.8.4.4(Google DNS服务器地址,根据自身喜好也可以改为其他的DNS服务器地址。),路由信息为自动获取

DHCP服务

工控机无法为连接enp7s0网络接口的设备自动分配IP,因此需要使用DHCP服务

安装DHCP服务

sudo apt install isc-dhcp-server

配置DHCP服务

修改配置文件 /etc/dhcp/dhcpd.conf(将前面的内容全部注释)

default-lease-time 600; # 默认租约10分钟
max-lease-time 7200; # 最大租约2小时(时间到了后会续租)
ddns-update-style none; # 不进行动态更新
authoritative; # 权威的

# 定义子网
# 该子网便是为连接enp7s0网络接口的设备(路由器B)划分的子网段
# 该子网与enp7s0网络接口配置中的ip属于同一网段
subnet 192.168.127.0 netmask 255.255.255.0 {
   interface enp7s0; # 指定接口
   range 192.168.127.2 192.168.127.5; # 分配范围
   option routers 192.168.127.6; # 路由为enp7s0网络接口的地址(该地址即为路由器B的网关地址,因为路由器B上的流量要经过enp7s0网络接口进行转发)
   option domain-name-servers 8.8.8.8, 8.8.4.4; # Google NDS 服务器地址
}

启动DHCP服务

sudo systemctl start isc-dhcp-server

查看日志

sudo journalctl -u isc-dhcp-server -f

​ 可以看到DHCP为路由器B分配IP地址。(本实验中为路由器B分配的地址为192.168.127.2

ps:

​ 路由器B必须与enp7s0处于同一网段,并且路由器B的网关地址为enp7s0的ip地址

测试连接

​ 连接路由器B的设备可以ping通enp7s0(192.168.127.6

DHCP相关命令:

查看DHCP服务状态

sudo systemctl status isc-dhcp-server

关闭DHCP服务

sudo systemctl stop isc-dhcp-server

重启DHCP服务

sudo systemctl restart isc-dhcp-server

开机自启

sudo systemctl enable isc-dhcp-server

IP转发和NAT规则

enp7s0上的出站流量进行地址伪装。

启用IP转发

修改配置文件 /etc/sysctl.conf, 添加以下内容设置为永久生效

net.ipv4.ip_forward = 1

执行下述命令使得修改生效

sudo sysctl -p

添加NAT规则

sudo iptables -t nat -A POSTROUTING -o enp3s0 -j MASQUERADE  -m comment --comment '所有的出站流量的原地址都为enp3s0'

测试连接

​ 连接路由器B的所有设备都可正常访问外部网络

Danted代理

安装Danted代理

在云节点上安装socks5代理服务器dante

sudo apt update
sudo apt install dante-server

配置Danted代理

备份并编辑配置文件

sudo cp /etc/danted.conf /etc/danted_copy.conf
sudo  /etc/danted.conf

内容如下:

logoutput: stderr

internal: 127.0.0.1 port = 8430  # 内部网络接口
external: enp3s0 # 外部网络接口

socksmethod: none
clientmethod: none

user.unprivileged: nobody
user.privileged: root

client pass {
    from: 0.0.0.0/0 to: 0.0.0.0/0
    log: error connect
}

socks pass {
    from: 0.0.0.0/0 to: 0.0.0.0/0
    command: connect
    log: error connect
}

启动服务

sudo systemctl start danted

开机自动运行

sudo systemctl enable --now danted

查看日志

sudo journalctl -u danted -f

测试

  • 使用命令行可以正确返回
curl -v -x socks5://127.0.0.1:8430 http://www.baidu
  • 设置火狐浏览器,手动添加代理服务器ip为127.0.0.1,端口为8430,浏览器可以正常访问

redoskcs透明代理

安装redsocks

sudo apt update
sudo apt-get install redsocks

配置redsocks

编辑 Redsocks 的配置文件,通常位于``/etc/redsocks.conf`

sudo vi /etc/redsocks.conf
base {
        // debug: connection progress & client list on SIGUSR1
    	//日志级别
        log_debug = on;

        // info: start and end of client session
    	//日志记录 
        log_info = on;

        /* possible `log' values are:
         *   stderr
         *   "file:/path/to/file"
         *   syslog:FACILITY  facility is any of "daemon", "local0"..."local7"
         */
        // log = "syslog:daemon";
    	//日志文件路径
    	log  =  "file:/var/log/redsocks.log"

        // detach from console
        daemon = on;

        /* Change uid, gid and root directory, these options require root
         * privilegies on startup.
         * Note, your chroot may requre /etc/localtime if you write log to syslog.
         * Log is opened before chroot & uid changing.
         */
        // user = redsocks;
        // group = redsocks;
        // chroot = "/var/chroot";

        /* possible `redirector' values are:
         *   iptables   - for Linux
         *   ipf        - for FreeBSD
         *   pf         - for OpenBSD
         *   generic    - some generic redirector that MAY work
         */
    	//使用iptables进行流量重定向
        redirector = iptables;
}
redsocks {
        /* `local_ip' defaults to 127.0.0.1 for security reasons,
         * use 0.0.0.0 if you want to listen on every interface.
         * `local_*' are used as port to redirect to.
         */
        local_ip = 0.0.0.0; /*允许其他接口连接*/
        local_port = 12345;

        // `ip' and `port' are IP and tcp-port of proxy-server
        // You can also use hostname instead of IP, only one (random)
        // address of multihomed host will be used.
    	//代理服务器(入口节点)ip和端口
        ip = 127.0.0.1
        port = 8430;


        // known types: socks4, socks5, http-connect, http-relay
    	//代理服务器类型
        type = socks5;
		
    	//代理服务器用户名和密码
        // login = "foobar";
        // password = "baz";
}
redudp {
        // `local_ip' should not be 0.0.0.0 as it's also used for outgoing
        // packets that are sent as replies - and it should be fixed
        // if we want NAT to work properly.
        local_ip = 127.0.0.1;
        local_port = 10053;

        // `ip' and `port' of socks5 proxy server.
        ip = 192.0.2.1;
        port = 1080;
        login = username;
        password = pazzw0rd;

        // kernel does not give us this information, so we have to duplicate it
        // in both iptables rules and configuration file.  By the way, you can
        // set `local_ip' to 127.45.67.89 if you need more than 65535 ports to
        // forward ;-)
        // This limitation may be relaxed in future versions using contrack-tools.
        dest_ip = 192.0.2.2;
        dest_port = 53;

        udp_timeout = 30;
        udp_timeout_stream = 180;
}
dnstc {
        // fake and really dumb DNS server that returns "truncated answer" to
        // every query via UDP, RFC-compliant resolver should repeat same query
        // via TCP in this case.
        local_ip = 127.0.0.1;
        local_port = 5300;
}

启动redsocks

sudo systemctl start redsocks

查看日志

sudo tail -f /var/log/redsocks.log

iptables

查看所有规则

sudo iptables -L -v -n #filter表
sudo iptables -t nat -L -v -n #nat表

删除所有规则

sudo iptables -t nat -F #filter表
sudo iptables -F #nat表

添加规则

  • 本机流量出站会经过nat表的OUTPUT链,在经过POSTROUTING链。因此在OUTPUT链中放行danted的出站流量(danted服务在本机),其余出站流量转至自建链REDSOCKS。在REDSOCKS链中放行所有局域网流量,其余流量重定向到redsocks。重定向后的流量会再次经过OUTPUT链。
  • 路由器B上的出站流量会经过nat链的PREROUTING链,在PREROUTING链中将出站流量转发至REDSOCKS链。路由器B上的流量要想出站还需要再POSTROUTNG链上进行地址伪装

danted不在本地

sudo iptables -t nat -N REDSOCKS    # 新建REDSOCKS链

sudo iptables -t nat -A OUTPUT -j REDSOCKS -m comment --comment '跳转到redsocks全局代理REDSOCKS链'
sudo iptables -t nat -A POSTROUTING -o enp3s0 -j MASQUERADE  -m comment --comment '所有的出站流量的原地址都为enp3s0'
sudo iptables -t nat -A REDSOCKS -j REDIRECT -p tcp --to-ports 12345 -m comment --comment '重定向到本机redsocks代理服务'

# 放行发往danted服务流量

sudo iptables -t nat -I REDSOCKS -j RETURN -m comment --comment 'socks5代理服务器放行' -d dantedServer_ip

sudo iptables -t nat -A PREROUTING -p tcp -j REDSOCKS -m comment --comment '路由器的流量转发redsocks代理'

sudo iptables -t nat -I REDSOCKS -j RETURN -m comment --comment '局域网放行' -d 0.0.0.0/8
sudo iptables -t nat -I REDSOCKS -j RETURN -m comment --comment '局域网放行' -d 10.0.0.0/8
sudo iptables -t nat -I REDSOCKS -j RETURN -m comment --comment '局域网放行' -d 100.64.0.0/10
sudo iptables -t nat -I REDSOCKS -j RETURN -m comment --comment '局域网放行' -d 127.0.0.0/8
sudo iptables -t nat -I REDSOCKS -j RETURN -m comment --comment '局域网放行' -d 169.254.0.0/16
sudo iptables -t nat -I REDSOCKS -j RETURN -m comment --comment '局域网放行' -d 172.16.0.0/12
sudo iptables -t nat -I REDSOCKS -j RETURN -m comment --comment '局域网放行' -d 192.168.0.0/16
sudo iptables -t nat -I REDSOCKS -j RETURN -m comment --comment '局域网放行' -d 198.18.0.0/15
sudo iptables -t nat -I REDSOCKS -j RETURN -m comment --comment '局域网放行' -d 224.0.0.0/4
sudo iptables -t nat -I REDSOCKS -j RETURN -m comment --comment '局域网放行' -d 240.0.0.0/4

danted在本地

sudo iptables -t nat -N REDSOCKS    # 新建REDSOCKS链

sudo iptables -t nat -A OUTPUT -m owner --uid-owner $(id -u nobody) -j RETURN -m comment --comment '放行danted出站流量' #nobody为danted配置时的用户名
sudo iptables -t nat -A OUTPUT -j REDSOCKS -m comment --comment '跳转到redsocks全局代理REDSOCKS链'
sudo iptables -t nat -A POSTROUTING -o enp3s0 -j MASQUERADE  -m comment --comment '所有的出站流量的原地址都为enp3s0'
sudo iptables -t nat -A REDSOCKS -j REDIRECT -p tcp --to-ports 12345 -m comment --comment '重定向到本机redsocks代理服务'

sudo iptables -t nat -A PREROUTING -i enp7s0 -j REDSOCKS -m comment --comment '路由器的流量转发redsocks代理'

sudo iptables -t nat -I REDSOCKS -j RETURN -m comment --comment '局域网放行' -d 0.0.0.0/8
sudo iptables -t nat -I REDSOCKS -j RETURN -m comment --comment '局域网放行' -d 10.0.0.0/8
sudo iptables -t nat -I REDSOCKS -j RETURN -m comment --comment '局域网放行' -d 100.64.0.0/10
sudo iptables -t nat -I REDSOCKS -j RETURN -m comment --comment '局域网放行' -d 127.0.0.0/8
sudo iptables -t nat -I REDSOCKS -j RETURN -m comment --comment '局域网放行' -d 169.254.0.0/16
sudo iptables -t nat -I REDSOCKS -j RETURN -m comment --comment '局域网放行' -d 172.16.0.0/12
sudo iptables -t nat -I REDSOCKS -j RETURN -m comment --comment '局域网放行' -d 192.168.0.0/16
sudo iptables -t nat -I REDSOCKS -j RETURN -m comment --comment '局域网放行' -d 198.18.0.0/15
sudo iptables -t nat -I REDSOCKS -j RETURN -m comment --comment '局域网放行' -d 224.0.0.0/4
sudo iptables -t nat -I REDSOCKS -j RETURN -m comment --comment '局域网放行' -d 240.0.0.0/4

测试

​ 本机浏览器(或手机连接路由器B)均可正常访问外网。

开机自动启动

sudo apt-get update
sudo apt-get install iptables iptables-persistent
sudo iptables-save > /etc/iptables/rules.v4

本文标签: 全局双网卡dantedredsocksLinux