SOCKS5实现(一)

article/2025/9/27 8:39:47

引文

假如我有一部安卓手机,我想把手机上的全部流量打到指定的代理上(即透明代理),代理外接外部的VPN,以达到分散IP出口的目的。
这是这个系列的第一篇文章,本文将简单介绍一下 SOCKSiptable透明代理 的概念,以及一点小小的实践。

SOCKS

SOCKS是一种网络传输协议,当防火墙后的客户端要访问外部的服务器时,就跟SOCKS代理服务器连接。这个代理服务器控制客户端访问外网的资格,允许的话,就将客户端的请求发往外部的服务器。

SOCKS5协议

参考
rfc:https://tools.ietf.org/html/rfc1928
wikipadia:https://zh.wikipedia.org/wiki/SOCKS

协议这里不展开了。

iptable

参考
wikipadia:https://zh.wikipedia.org/wiki/Iptables
Archlinux: https://wiki.archlinux.org/index.php/Iptables_(简体中文)

iptables 是一个配置 Linux 内核 防火墙 的命令行工具,是 netfilter 项目的一部分。术语 iptables 也经常代指该内核级防火墙。iptables 用于 ipv4,ip6tables 用于 ipv6。

iptables 可以检测、修改、转发、重定向和丢弃 IPv4 数据包。过滤 IPv4 数据包的代码已经内置于内核中,并且按照不同的目的被组织成 表 的集合。表 由一组预先定义的 链 组成,链 包含遍历顺序规则。每一条规则包含一个谓词的潜在匹配和相应的动作(称为 目标),如果谓词为真,该动作会被执行。也就是说条件匹配。iptables 是用户工具,允许用户使用 链 和 规则。

透明代理

透明代理可以是通过配置iptable(设置防火墙规则),将电脑上的网络请求全部自动 代理出去。

下面,我用 redsocks 做一下实践。

参考
http://darkk.net.ru/redsocks/
https://github.com/darkk/redsocks

redsocks只是一个TCP-to-proxy的转向器,支持把流量打到SSH、SOCKS5等

有几个步骤:
0,开启redsocks

sudo apt-get install iptables git-core libevent-1.4-2 libevent-dev
git clone http://github.com/darkk/redsocks.git
cd redsocks 
make
vim redsocks.conf

redsocks.conf

base{log_debug = on;log_info = on;log = "file:/tmp/reddi.log";daemon = on;redirector = iptables;
}
redsocks {local_ip = 127.0.0.1;local_port = 20182;ip = 127.0.0.1;port = 20183;type = socks5;
}
./redsocks -c redsocks.conf

1,把设置iptable的规则,把流量打到redsocks上

# 创建规则
root# iptables -t nat -N REDSOCKS
# 忽略内网流量
root# iptables -t nat -A REDSOCKS -d 0.0.0.0/8 -j RETURN
root# iptables -t nat -A REDSOCKS -d 10.0.0.0/8 -j RETURN
root# iptables -t nat -A REDSOCKS -d 100.64.0.0/10 -j RETURN
root# iptables -t nat -A REDSOCKS -d 127.0.0.0/8 -j RETURN
root# iptables -t nat -A REDSOCKS -d 169.254.0.0/16 -j RETURN
root# iptables -t nat -A REDSOCKS -d 172.16.0.0/12 -j RETURN
root# iptables -t nat -A REDSOCKS -d 192.168.0.0/16 -j RETURN
root# iptables -t nat -A REDSOCKS -d 198.18.0.0/15 -j RETURN
root# iptables -t nat -A REDSOCKS -d 224.0.0.0/4 -j RETURN
root# iptables -t nat -A REDSOCKS -d 240.0.0.0/4 -j RETURN
# 外网流量
# TCP
root# iptables -t nat -A REDSOCKS -p tcp -o eth0 -j DNAT --to 127.0.0.1:20182
root# iptables -t nat -A OUTPUT -p tcp -j REDSOCKS

ps:iptable关机时所有配置会丢失。可以将当前配置保存到文件

# 保存
root# iptables-save > proxy.iptablesv
# 恢复
root# iptables-restore proxy.iptables

2.1,在20183端口建立ssh通道

Local

# ServerB :root@192.168.0.92
ssh -fqND 20183 root@192.168.0.92

检查
ServerB

watch -n 1 -d "netstat -an | grep 118.89.53.151"

Local

wget 118.89.53.151

result(ServerB)

Every 1.0s: netstat -an | grep 118.89.53.151                                                                   Wed Feb 28 15:41:05 2018tcp        0      0 192.168.0.92:44426      118.89.53.151:443       TIME_WAIT
tcp        0      0 192.168.0.92:46026      118.89.53.151:80        TIME_WAIT

可以看到,在本地执行的wget 在ServerB上能看到建立的端口,流量就这样自动通过ssh出去了。
这里我们已经用iptable、redsocks、ssh做了一层透明代理。

2.2,设置redsocks的出口为socks5

a,部署一个简单的 socks server

python2.7 ServerB:192.168.0.92

git clone https://github.com/fengyouchao/pysocks.git
cd pysocks
python socks5.py start

ServerB

# 关闭
python socks5.py stop

b,修改步骤0中的redsocks.conf

改成:
Local

base{log_debug = on;log_info = on;log = "file:/tmp/reddi.log";daemon = on;redirector = iptables;
}
redsocks {local_ip = 127.0.0.1;local_port = 20182;ip = 192.168.0.92;port = 1080;type = socks5;
}
ps aux | grep redsocks
# 24588
kill -9 24588
./redsocks -c redsocks.conf

c,检查

ServerB

watch -n 1 -d "netstat -an | grep 118.89.53.151"

Local

wget 118.89.53.151

result(ServerB)

Every 1.0s: netstat -an | grep 118.89.53.151                                                                   Wed Feb 28 15:41:05 2018tcp        0      0 192.168.0.92:44426      118.89.53.151:443       TIME_WAIT
tcp        0      0 192.168.0.92:46026      118.89.53.151:80        TIME_WAIT

这样过了一轮之后,概念也熟悉了,本机的流量就可以想怎么发就怎么发了。

只用tcp的话,看到这里就可以拉源码下来看了~


2018.3.10 更新

支持UDP代理

redsocks.conf

base{log_debug = on;log_info = on;log = "file:/tmp/reddi.log";daemon = on;redirector = iptables;
}
redsocks {local_ip = 127.0.0.1;local_port = 31338;ip = 192.168.0.92;port = 1080;type = socks5;
}redudp {local_ip = 127.0.0.1;local_port = 10053;ip = 192.168.0.92;port = 1080;dest_ip = 8.8.8.8;dest_port = 53;udp_timeout = 30;udp_timeout_stream = 180;
}dnstc {local_ip = 127.0.0.1;local_port = 5300;
}dnsu2t {local_ip = 127.0.0.1;local_port = 5313;remote_ip = 8.8.8.8;remote_port = 53;
}

iptables

# Create new chain
root# iptables -t nat -N REDSOCKS# Ignore LANs and some other reserved addresses.
# See Wikipedia and RFC5735 for full list of reserved networks.
root# iptables -t nat -A REDSOCKS -d 0.0.0.0/8 -j RETURN
root# iptables -t nat -A REDSOCKS -d 10.0.0.0/8 -j RETURN
root# iptables -t nat -A REDSOCKS -d 127.0.0.0/8 -j RETURN
root# iptables -t nat -A REDSOCKS -d 169.254.0.0/16 -j RETURN
root# iptables -t nat -A REDSOCKS -d 172.16.0.0/12 -j RETURN
root# iptables -t nat -A REDSOCKS -d 192.168.0.0/16 -j RETURN
root# iptables -t nat -A REDSOCKS -d 224.0.0.0/4 -j RETURN
root# iptables -t nat -A REDSOCKS -d 240.0.0.0/4 -j RETURN# TCP
# Anything else should be redirected to port 31338
root# iptables -t nat -A REDSOCKS -p tcp -j REDIRECT --to-ports 31338
root# iptables -t nat -A OUTPUT -p tcp -j REDSOCKS# UDP
root# iptables -t nat -A REDSOCKS -p udp -j REDIRECT --to-ports 10053
root# iptables -t nat -A OUTPUT -p udp -j REDSOCKS

检查配置是否完成

root@ys:~# iptables -t nat -L -n -v
Chain PREROUTING (policy ACCEPT 58 packets, 7428 bytes)pkts bytes target     prot opt in     out     source               destination4   268 DOCKER     all  --  *      *       0.0.0.0/0            0.0.0.0/0            ADDRTYPE match dst-type LOCALChain INPUT (policy ACCEPT 27 packets, 5718 bytes)pkts bytes target     prot opt in     out     source               destinationChain OUTPUT (policy ACCEPT 4 packets, 266 bytes)pkts bytes target     prot opt in     out     source               destination0     0 DOCKER     all  --  *      *       0.0.0.0/0           !127.0.0.0/8          ADDRTYPE match dst-type LOCAL4   240 REDSOCKS   tcp  --  *      *       0.0.0.0/0            0.0.0.0/04   222 REDSOCKS   udp  --  *      *       0.0.0.0/0            0.0.0.0/0Chain POSTROUTING (policy ACCEPT 6 packets, 342 bytes)pkts bytes target     prot opt in     out     source               destination0     0 MASQUERADE  all  --  *      !docker0  172.17.0.0/16        0.0.0.0/0Chain DOCKER (2 references)pkts bytes target     prot opt in     out     source               destinationChain REDSOCKS (2 references)pkts bytes target     prot opt in     out     source               destination0     0 RETURN     all  --  *      *       0.0.0.0/0            0.0.0.0/80     0 RETURN     all  --  *      *       0.0.0.0/0            10.0.0.0/80     0 RETURN     all  --  *      *       0.0.0.0/0            127.0.0.0/80     0 RETURN     all  --  *      *       0.0.0.0/0            169.254.0.0/160     0 RETURN     all  --  *      *       0.0.0.0/0            172.16.0.0/123   180 RETURN     all  --  *      *       0.0.0.0/0            192.168.0.0/162   146 RETURN     all  --  *      *       0.0.0.0/0            224.0.0.0/40     0 RETURN     all  --  *      *       0.0.0.0/0            240.0.0.0/41    60 REDIRECT   tcp  --  *      *       0.0.0.0/0            0.0.0.0/0            redir ports 313382    76 REDIRECT   udp  --  *      *       0.0.0.0/0            0.0.0.0/0            redir ports 10053

2018.3.17 更新

在浏览器上设置代理进行调试

以firefox为例

打开这个地址

about:preferences#general

红框中填写代理服务器的地址和端口,入下图

这里写图片描述


项目开源在 github -> wuyisheng/socks

扫码关注,实时互动

关注我

在这里插入图片描述


http://chatgpt.dhexx.cn/article/l8yWhWCk.shtml

相关文章

socks协议详解

0x01 socks协议简介 Socks(Socket Secure)协议是一种网络协议,处于会话层,用于管理网络连接并提供安全性和隐私保护。通过使用Socks代理服务器,客户端可以隐藏其真实IP地址和其他身份信息,从而匿名地访问互…

socks代理实战

文章目录 Socks代理实战Socks代理简介Socks代理工具Socks代理实战-MSF渗透场景Target1Target2Target3 Socks代理实战-FRPTarget1Target2Target3 Socks代理实战-EWEW使用简介EW正反向Socks代理EW创建一层Socks代理二级级联本地SocksEW创建二层Socks代理三级级联的本地SocksEW创建…

Socket(四)

文章目录 1. 服务器Socket简介2. 使用ServerSocket3. 用Socket写入服务器4. 关闭服务器Socket 1. 服务器Socket简介 博客Socket(一)~Socket(二)从客户端的角度讨论了Socket,客户端就是向监听连接的服务器打…

CobaltStrike建立Socks4代理

目录 Socks4代理建立 ProxyChains的使用 Socks4代理建立 当我们的被控主机是位于公网和内网边界的服务器时,我们想利用该主机继续对内网的主机进行渗透,比如经过端口扫描我们发现被控机器的同一网段有一主机开放了80端口,我们想在本地访问…

Java常用工具类

学习内容 包装类型(基本数据类型的包装类型) String字符串常用方法 StringBuffer与Stringbuilder 日期和时间处理工具类 System,Math,Random,UUID等工具类 枚举类型 File类 递归算法 目录 1.包装类型 1.1.Integer的基本用法 1.2.包装类型的使用 1.3.自动装箱与自动…

Java——Collections工具类

Collections工具类 Collections 是一个操作 Set、List 和 Map 等集合的工具类 Collections 中提供了一系列静态的方法对集合元素进行排序、查询和修改等操作,还提供了对集合对象设置不可变、对集合对象实现同步控制等方法。 排序操作:(均为…

Spring自带的工具类

最近发现同事写了不少重复的工具类,发现其中很多功能,Spring 自带的都有。于是整理了本文,希望能够帮助到大家! 目录 断言 对象、数组、集合 ObjectUtils StringUtils CollectionUtils 文件、资源、IO 流 FileCopyUtils R…

【常用工具类】Java控制台打印工具类LogUtil

文章目录 1 带分割线版2 不带分割线版3 终极版 1 带分割线版 import java.text.SimpleDateFormat; import java.util.Date;/*** W SKH 的输出工具类*/ public class LogUtil {private SimpleDateFormat simpleDateFormat new SimpleDateFormat("yyyy-MM-dd,HH:mm:ss.SSS&…

Java 对象拷贝工具类

目录 1. Spring 中的对象拷贝 2. 本工具类中的对象拷贝 2.1 拷贝对象本身(单个) 2.2 拷贝对象本身(批量) 2.3 拷贝对象属性至其他类(单个) 2.4 拷贝对象属性至其他类(批量) 4.…

Java表达式运算工具类

对于表达式的运算转换工具类,使用场景,例如前端文本框让用户输入了加减乘除运算,工具类智能转换。 资源下载:Java表达式工具类,用于加减乘除等智能转换-Java文档类资源-CSDN下载 例如: public static void…

阿里OSS工具类

参考博客:阿里OSS入门 bucket配置信息:alios.properties OSS配置文件:AliOSSConfig.java Data public class AliOSSConfig {private String endpoint;private String accessKeyId;private String accessKeySecret;private String bucketNam…

Guava Lists工具类

文章目录 01 概述02 Lists工具类03 文末 01 概述 Guava 是 Google 开源的一个 Java 工具库&#xff0c;里面有很多工具类&#xff0c;本文要讲的是里面的Lists工具类。 注意&#xff0c;使用Guava工具类库&#xff0c;必须先添加依赖&#xff1a; <dependency><gro…

JAVA之常用的工具类

目录 1 集合工具类 1.1 java.util.Collections 1.1.1 基本操作 1.1.2 转换线程安全集合 1.2 org.springframework.util.CollectionUtils 1.3 org.apache.commons.collections.CollectionUtils 1.4 org.apache.commons.lang.ArrayUtils 1.5 org.apache.commons.lang3.Ar…

常用的Java开发工具类

前言 在java的庞大体系中&#xff0c;其实有很多不错的小工具&#xff0c;也就是我们平常说的&#xff1a;轮子。 如果在我们的日常工作当中&#xff0c;能够将这些轮子用户&#xff0c;再配合一下idea的快捷键&#xff0c;可以极大得提升我们的开发效率。 今天我决定把一些…

工具类的详解

工具类网站 1、工具类 1.1 定义 工具类是为了提供一些通用的、某一非业务领域内的公共方法&#xff0c;不需要配套的成员变量&#xff0c;仅仅是作为工具方法被使用。所以将它做成静态方法最合适&#xff0c;不需要实例化&#xff0c;能够获取到方法的定义并调用就行。 1.2…

学习日记-安卓Package Manager和Package Installer

安装和卸载APK&#xff08;安卓应用程序包文件&#xff09;&#xff0c;运作原理。 什么是Package Manager&#xff08;包管理器&#xff09;和Package Installer&#xff08;程序安装包&#xff09;&#xff1f; APK文件保存在Android的哪个地方&#xff1f; APK文件安装过…

PackageInstaller (tv 修改安装app界面按钮及自动获取焦点)附源码分析

\packages\apps\PackageInstaller 一、一条真实的修改记录 TVOS基于的是一套板卡厂商原有的源码(mstar android8.0版本&#xff09;原生的这个app安装界面&#xff0c;存在俩个比较严重的用户体验问题&#xff0c; 1’、下面那俩按钮太小了&#xff0c;而且…

android packages/apps 加入工程,深入安卓Package Manager和Package Installer

我们每天都在安装和卸载APK(安卓应用程序包文件)&#xff0c;或许一天会有好几次&#xff0c;但是你有想过下面问题吗&#xff1f;什么是Package Manager(包管理器)和Package Installer(程序安装包)&#xff1f; APK文件保存在Android的哪个地方&#xff1f; APK文件安装过程的…

RK3568平台开发系列讲解(安卓篇)PackageInstaller(应用安装)流程介绍

文章目录 <font color=#0990d9>一、PackageInstaller入口<font color=#0990d9>二、InstallStart<font color=#0990d9>三、InstallStaging<font color=#0990d9>四、PackageInstallerActivity<font color=#0990d9>五、InstallInstalling<font c…

Android9.0 PM机制系列(一)PackageInstaller初始化解析

前言 包管理机制是Android中的重要机制&#xff0c;是应用开发和系统开发需要掌握的知识点之一。 包指的是Apk、jar和so文件等等&#xff0c;它们被加载到Android内存中&#xff0c;由一个包转变成可执行的代码&#xff0c;这就需要一个机制来进行包的加载、解析、管理等操作&…