Ge0rg3/requests-ip-rotator
GitHub: Ge0rg3/requests-ip-rotator
利用AWS API Gateway大规模IP池为Python requests库提供每次请求自动切换出口IP能力的代理轮换库。
Stars: 1665 | Forks: 172
# requests-ip-rotator
一个 Python 库,利用 AWS API Gateway 的大规模 IP 池作为代理,为网络爬虫和暴力破解生成伪无限 IP。
该库将允许用户绕过网站和服务的基于 IP 的速率限制。
除非另有指定,X-Forwarded-For 请求头会自动随机化并应用。这是因为否则,AWS 会在此请求头中发送客户端的真实 IP 地址。
AWS 的 ApiGateway 从任何可用的 IP 发送其请求 - 并且由于 AWS 基础设施非常庞大,几乎可以保证每次请求的 IP 都不相同。通过将 ApiGateway 作为代理,我们可以利用这一点,每次都从不同的 IP 发送请求。请注意,这些请求很容易被识别和阻止,因为它们带有独特的 AWS 请求头(例如 "X-Amzn-Trace-Id")。
## 安装
此包已在 pypi 上发布,因此你可以通过以下任一方式进行安装:
* `pip3 install requests-ip-rotator`
* `python3 -m pip install requests-ip-rotator`
## 简单用法
```
import requests
from requests_ip_rotator import ApiGateway
# 创建 gateway 对象并在 AWS 中初始化
gateway = ApiGateway("https://site.com")
gateway.start()
# 将 gateway 分配给 session
session = requests.Session()
session.mount("https://site.com", gateway)
# 发送请求(IP 将随机化)
response = session.get("https://site.com/index.php", params={"theme": "light"})
print(response.status_code)
# 删除 gateway
gateway.shutdown()
```
### 替代用法(自动启动和关闭)
```
import requests
from requests_ip_rotator import ApiGateway
with ApiGateway("https://site.com") as g:
session = requests.Session()
session.mount("https://site.com", g)
response = session.get("https://site.com/index.php")
print(response.status_code)
```
请记住,如果在使用方法 #1 时没有通过 `shutdown()` 方法关闭网关,你可能会在未来被收取费用。
## 费用
API Gateway 在每个区域的前一百万次请求是免费的,这意味着对于大多数用例,这应该是完全免费的。
在撰写本文时,AWS 在超出免费额度后,每百万次请求收费约 $3。
如果你的请求涉及数据流,AWS 将按 $0.09 / GB 收取数据传输费。
## 文档
### AWS 身份验证
建议通过环境变量设置身份验证。使用 [awscli](https://docs.aws.amazon.com/cli/latest/userguide/install-cliv2.html),你可以运行 `aws configure` 来完成此操作,或者,你也可以按照此[帖子](https://stackoverflow.com/a/21443349/21208413)中提到的步骤自行设置 `AWS_ACCESS_KEY_ID` 和 `AWS_SECRET_ACCESS_KEY` 变量。
你可以按照[官方 AWS 教程](https://docs.aws.amazon.com/powershell/latest/userguide/pstools-appendix-sign-up.html)查找你的访问密钥 ID 和密钥。
### 创建 ApiGateway 对象
可以使用以下可选参数创建 ApiGateway 类:
| 名称 | 描述 | 必需 | 默认值
| ----------- | ----------- | ----------- | -----------
| site | 请求将发送到的站点(不含路径)。 | 是 |
| regions | 用于设置网关的 [AWS 区域](https://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/Concepts.RegionsAndAvailabilityZones.html#Concepts.RegionsAndAvailabilityZones.Regions) 数组。 | 否 | ip_rotator.DEFAULT_REGIONS
| access_key_id | AWS Access Key ID(将覆盖环境变量)。 | 否 | *依赖环境变量。*
| access_key_secret | AWS Access Key Secret(将覆盖环境变量)。 | 否 | *依赖环境变量。*
| verbose | 包含状态和错误消息。 | 否 | True
```
from ip_rotator import ApiGateway, EXTRA_REGIONS, ALL_REGIONS
# 仅限两个区域的出站 HTTP IP 和端口 Gateway
gateway_1 = ApiGateway("http://1.1.1.1:8080", regions=["eu-west-1", "eu-west-2"])
# 带有指定 access key pair 的额外区域包 HTTPS google Gateway
gateway_2 = ApiGateway("https://www.google.com", regions=EXTRA_REGIONS, access_key_id="ID", access_key_secret="SECRET")
```
### 启动 API gateway
然后必须使用 `start` 方法启动 ApiGateway 对象。
**默认情况下,如果站点已存在 ApiGateway,它将使用现有端点而不是创建新端点。**
这不需要任何参数,但接受以下内容:
| 名称 | 描述 | 必需 | 默认值
| ----------- | ----------- | ----------- | -----------
| endpoints | 预存端点数组(例如来自上次会话)。 | 否 |
| force | 创建一组新端点,即使有些端点已经存在。 | 否 | False
| require_manual_deletion | 指定 Apigateways 是否应在 `shutdown()` 调用后保留的布尔值 | 否 | False
```
# 为 site 启动新的 ApiGateway 实例,或如果已存在 endpoint 则定位现有 endpoint。
gateway_1.start()
# 即使部分已存在,也启动新的 ApiGateway 实例。
gateway_2.start(force=True)
```
### 发送请求
通过将 ApiGateway 对象附加到 requests 的 Session 对象来发送请求。
`mount` 中给定的站点必须与 `ApiGateway` 构造函数中传递的站点匹配。
```
import requests
# 向在 gateway_1 中创建的 site 提交请求。将从随机 IP 发送。
session_1 = requests.Session()
session_1.mount("http://1.1.1.1:8080", gateway_1)
session_1.post("http://1.1.1.1:8080/update.php", headers={"Hello": "World"})
# 在出站请求中将 127.0.0.1 作为 X-Forwarded-For header 发送(否则 X-Forwarded-For 将被随机化)。
session_1.post("http://1.1.1.1:8080/update.php", headers={"X-Forwarded-For", "127.0.0.1"})
# 从随机 IP 执行 Google 搜索查询
session_2 = requests.Session()
session_2.mount("https://www.google.com", gateway_2)
session_2.get("https://www.google.com/search?q=test")
```
### 关闭 ApiGateway 资源
在完成 ApiGateway 资源的使用后将其关闭非常重要,以防止出现可能导致账户产生额外费用的孤立公共端点。
这通过 ApiGateway 对象的 `shutdown` 方法完成。它将关闭在 ApiGateway 对象构造函数中指定的区域的所有资源。
```
# 这将关闭 "eu-west-1" 和 "eu-west-2" 中针对 "http://1.1.1.1:8080" 的所有 gateway proxies
gateway_1.shutdown()
# 这将关闭 ip_rotator.EXTRA_REGIONS 中所有区域针对 "https://www.google.com" 的所有 gateway proxies
gateway_2.shutdown()
```
或者,如果需要,你可以选择性地关闭特定端点。为此,只需将端点数组传递给 `shutdown()` 方法即可,例如:
```
# 这将强制启动新的 gateway(即,即使该区域已存在一些 endpoint,也创建新 endpoint),然后仅删除其中的前 3 个。
gateway_3 = ApiGateway("http://1.1.1.1:8082", regions=ALL_REGIONS)
endpoints = gateway_3.start(force=True)
gateway_3.shutdown(endpoints[:3])
```
**请记住,任何以 `require_manual_deletion` 参数设置为 `True` 启动的网关都不会通过 `shutdown` 方法被删除,必须通过 AWS CLI 或网站手动删除。**
## 媒体报道
* [DOGE 软件工程师的电脑感染了信息窃取恶意软件 (NPR)](https://www.npr.org/2025/04/15/nx-s1-5355896/doge-nlrb-elon-musk-spacex-security)
* [DOGE 工作人员的代码支持 NLRB 举报人 (Krebs on Security)](https://krebsonsecurity.com/2025/04/doge-workers-code-supports-nlrb-whistleblower/)
## 致谢
核心的网关创建和组织代码改编自 RhinoSecurityLabs 的 [IPRotate Burp Extension](https://github.com/RhinoSecurityLabs/IPRotate_Burp_Extension/)。
X-My-X-Forwarded-For 请求头转发概念最初由 [ustayready](https://twitter.com/ustayready) 在其 [fireprox](https://github.com/ustayready/fireprox) 代理中构思。
标签:API网关, AWS, CISA项目, DPI, IP池, IP轮换, PoC, Python, requests, Web爬虫, X-Forwarded-For, 代理, 匿名访问, 反封禁, 基础设施工具, 开源, 无后门, 无文件攻击, 暴力破解, 绕过速率限制, 绕过防御, 请求库, 请求欺骗, 逆向工具, 黑帽SEO