Node.js is Web Scale

点击"打开/下载题目"进去看看情况:

为了方便查看翻译成中文简体来看:


emmm,看不懂什么意思,查看源代码,js表示是一段JavaScript代码,丢给AI分析一下:

复制代码
// server.js
const express = require("express");
const bodyParser = require("body-parser");
const path = require("path");
const { execSync } = require("child_process");

const app = express();
app.use(bodyParser.json());
app.use(express.static(path.join(__dirname, "public")));

let cmds = {
  getsource: "cat server.js",
  test: "echo 'hello, world!'",
};

let store = {};

// GET /api/store - Retrieve the current KV store
app.get("/api/store", (req, res) => {
  res.json(store);
});

// POST /set - Set a key-value pair in the store
app.post("/set", (req, res) => {
  const { key, value } = req.body;

  const keys = key.split(".");
  let current = store;

  for (let i = 0; i < keys.length - 1; i++) {
    const key = keys[i];
    if (!current[key]) {
      current[key] = {};
    }
    current = current[key];
  }

  // Set the value at the last key
  current[keys[keys.length - 1]] = value;

  res.json({ message: "OK" });
});

// GET /get - Get a key-value pair in the store
app.get("/get", (req, res) => {
  const key = req.query.key;
  const keys = key.split(".");

  let current = store;
  for (let i = 0; i < keys.length; i++) {
    const key = keys[i];
    if (current[key] === undefined) {
      res.json({ message: "Not exists." });
      return;
    }
    current = current[key];
  }

  res.json({ message: current });
});

// GET /execute - Run commands which are constant and obviously safe.
app.get("/execute", (req, res) => {
  const key = req.query.cmd;
  const cmd = cmds[key];
  res.setHeader("content-type", "text/plain");
  res.send(execSync(cmd).toString());
});

app.get("*", (req, res) => {
  res.sendFile(path.join(__dirname, "public", "index.html"));
});

// Start the server
const PORT = 3000;
app.listen(PORT, () => {
  console.log(`KV Service is running on port ${PORT}`);
});

这段代码实现了一个简单的键值对存储服务(KV Store)和一个命令执行功能:

  1. 引入依赖

express: 用于创建Web服务器。

body-parser: 用于解析HTTP请求体中的JSON数据。

path: 用于处理文件路径。

child_process.execSync: 用于同步执行系统命令。


  1. 初始化应用

创建Express应用实例。

使用bodyParser.json()中间件来解析JSON格式的请求体。

设置静态文件目录为public,这样可以访问放在public目录下的静态文件。


  1. 定义命令

cmds对象定义了一些安全的命令及其对应的系统命令字符串。例如:

getsource: 获取server.js文件的内容。

test: 输出hello, world!。


  1. 键值对存储功能

GET /api/store: 返回当前键值对存储的内容。


POST /set: 设置一个键值对。支持嵌套键,例如a.b.c。

解析请求体中的key和value。

使用递归的方式在store对象中创建嵌套结构并设置值。


GET /get: 获取一个键值对。

解析查询参数中的key。

按照.分割键,逐层查找store对象中的值。

如果键不存在,返回Not exists.;否则返回对应的值。


  1. 命令执行功能

GET /execute: 执行预定义的安全命令。

解析查询参数中的cmd。

从cmds对象中获取对应的命令字符串。

使用execSync同步执行命令,并将结果以纯文本形式返回。


  1. 路由处理

GET /*: 处理所有其他路由请求,返回public目录下的index.html文件。


  1. 启动服务器

监听端口3000,并在控制台输出启动信息。


回顾一下关键代码部分:

说明会直接执行预定义的命令"cat server.js"


这下思路有了,只要想办法修改预定义的命令为cat /flag就行了

此处,我先学会儿原型链污染的知识,再回来解释


将key设置为__proto__.nanyuan,value设置为cat /flag

成功解析cat /flag


访问/execute?cmd=nanyuan,页面发生变化,并显示出flag:

成功得到flag

相关推荐
codingWhat1 天前
能效平台设计方案(打通gitlab和飞书)
后端·node.js·koa
努力的小雨2 天前
我用 QClaw 做了个 Web3 陪学助手,专治 Java 程序员的“概念劝退”
经验分享·ai智能
见过夏天3 天前
Node.js 常用命令全攻略
node.js
前端双越老师3 天前
我从 0 开发的 AI Agent 智语项目发布了
前端·node.js·agent
kyriewen4 天前
2026 年了,还在用 Node.js?Bun 迁移实战:20 分钟搞定,附踩坑记录
前端·javascript·node.js
donecoding4 天前
3 条命令搞定闭环 Monorepo:Lerna 版本管理 + 拓扑构建 + 自定义分发
前端·前端框架·node.js
Flynt5 天前
npm v12 来了:allowScripts 默认关闭,我的项目差点跑不起来
安全·npm·node.js
叫我Paul就好6 天前
尝试 Node 搭建后端-开发框架
node.js
风止何安啊8 天前
网课倍速痛点解决:一套前端代码实现自由控速播放器
前端·javascript·node.js
糖拌西瓜皮8 天前
Node.js核心模块实战:文件、路径、HTTP与流处理
javascript·node.js