How to Evaluate and Block SQL Injection Attempts Hidden in JSON Bodies

本文为原文前 6,000 字符的节选翻译,完整内容请查看原文。

How to Evaluate and Block SQL Injection Attempts Hidden in JSON Bodies

An API can reject suspicious query strings while still accepting dangerous input inside a JSON request body. A search endpoint might receive {“filter”:“pending”}, then concatenate that value into a database query. If an attacker replaces the value with SQL syntax, the vulnerability is in how the application builds the query. JSON is only the delivery format.

API 可以拒绝可疑的查询字符串,但仍可能接受 JSON 请求体内的危险输入。例如,一个搜索端点可能会接收到 {"filter":"pending"},然后将该值拼接到数据库查询中。如果攻击者将该值替换为 SQL 语法,漏洞的根源就在于应用程序构建查询的方式,而 JSON 仅仅是传输格式。

AI-assisted automation makes it easier for attackers to generate variations across endpoints, field names, nesting levels, and encodings. Defenders may see repeated POST requests to search or reporting APIs, database syntax errors, unusual response sizes, or bursts of failures followed by successful responses. These signals warrant investigation, but none alone proves that SQL injection succeeded.

人工智能辅助的自动化工具使攻击者更容易在不同的端点、字段名、嵌套层级和编码方式之间生成变体。防御者可能会观察到针对搜索或报告 API 的重复 POST 请求、数据库语法错误、异常的响应大小,或者在出现一连串失败后紧接着成功的响应。这些信号值得调查,但没有任何单一信号能证明 SQL 注入已经成功。

For security owners researching how to block sql injection attempts hidden in json bodies, the practical task is to verify the entire request path. SafeLine documents SQL injection protection and reverse proxy deployment, but the supplied documentation does not specify JSON parsing behavior, nested-field coverage, decoding rules, or request-body inspection limits. The setup below therefore combines documented deployment steps with explicit acceptance checks instead of assuming universal JSON coverage.

对于研究如何拦截隐藏在 JSON 体中 SQL 注入尝试的安全负责人来说,实际任务是验证整个请求路径。SafeLine 提供了关于 SQL 注入防护和反向代理部署的文档,但所提供的文档并未明确说明 JSON 解析行为、嵌套字段覆盖范围、解码规则或请求体检查限制。因此,以下设置结合了已记录的部署步骤与明确的验收检查,而不是假设其具备通用的 JSON 覆盖能力。

1. Identify the endpoint and establish safe test conditions

Choose an API endpoint you own and reproduce its normal request in a staging environment. Record the HTTP method, path, content type, authentication requirements, expected response, and JSON structure. Include legitimate values containing apostrophes, such as customer names, so testing measures business compatibility as well as attack blocking.

1. 识别端点并建立安全的测试条件

选择一个您拥有的 API 端点,并在预发布环境中重现其正常请求。记录 HTTP 方法、路径、内容类型、身份验证要求、预期响应和 JSON 结构。包含带有撇号的合法值(例如客户名称),以便测试既能衡量业务兼容性,也能衡量攻击拦截效果。

Review the application code that consumes those fields. Values must reach the database through parameterized queries. If a field selects a column or sort direction, map it to an approved identifier rather than inserting arbitrary input into SQL. A WAF adds a protective layer; repairing unsafe query construction addresses the underlying vulnerability. For suspected active exploitation, preserve relevant application and database evidence before changing configuration. Avoid copying credentials or complete sensitive request bodies into investigation notes.

审查处理这些字段的应用程序代码。数值必须通过参数化查询到达数据库。如果某个字段用于选择列或排序方向,请将其映射到已批准的标识符,而不是将任意输入插入 SQL。WAF 增加了一层保护;修复不安全的查询构建才能解决根本漏洞。对于疑似正在进行的利用行为,在更改配置之前,请保留相关的应用程序和数据库证据。避免将凭据或完整的敏感请求体复制到调查笔记中。

2. Install SafeLine on a suitable host

SafeLine is a self-hosted WAF that filters HTTP/S traffic in front of an application and lists SQL injection among the attacks it protects against. See the official overview. For a straightforward installation, use a Linux x86_64 host meeting the documented requirements: Docker 20.10.14 or later, Docker Compose 2.0.0 or later, at least one CPU core, 1 GB memory, 5 GB disk space, and the listed CPU instruction requirement. Check the current prerequisites before provisioning.

2. 在合适的宿主机上安装 SafeLine

SafeLine 是一款自托管的 WAF,可在应用程序前端过滤 HTTP/S 流量,并将 SQL 注入列为其防护的攻击类型之一。请参阅官方概述。为了简化安装,请使用符合记录要求的 Linux x86_64 宿主机:Docker 20.10.14 或更高版本、Docker Compose 2.0.0 或更高版本、至少一个 CPU 核心、1 GB 内存、5 GB 磁盘空间以及列出的 CPU 指令集要求。在配置前请检查当前的先决条件。

The installation documentation separately describes ARM requirements and licensing restrictions. The official installation guide provides this automated installation command, which requires root privileges: bash -c "$(curl -fsSLk https://waf.chaitin.com/release/latest/manager.sh)" -- --en Review the installer before running it under your organization’s privileged software installation process. After installation, open https://<safeline-ip>:9443/. Obtain the administrator credentials using the documented command: docker exec safeline-mgt resetadmin. Restrict management-console access to authorized administrators.

安装文档分别描述了 ARM 要求和许可限制。官方安装指南提供了此自动化安装命令,该命令需要 root 权限:bash -c "$(curl -fsSLk https://waf.chaitin.com/release/latest/manager.sh)" -- --en。在通过贵组织的特权软件安装流程运行安装程序之前,请先进行审查。安装完成后,打开 https://<safeline-ip>:9443/。使用记录的命令获取管理员凭据:docker exec safeline-mgt resetadmin。请限制对管理控制台的访问权限,仅限授权管理员使用。

3. Configure the application and verify the traffic path

In the SafeLine console, open Applications, select Add Application, and enter the application’s domain, listening port, and upstream address. For HTTPS, select the SSL option. Then resolve the application’s domain to the SafeLine server’s IP address. These fields and routing steps are documented in Protecting an application. Send a normal API request through the public hostname and confirm that the application still works. Check your network configuration for alternate routes that expose the upstream directly. A request that bypasses the reverse proxy cannot benefit from its inspection. Keep a tested rollback procedure available before changing production routing.

3. 配置应用程序并验证流量路径

在 SafeLine 控制台中,打开“应用管理”,选择“添加应用”,并输入应用程序的域名、监听端口和上游地址。对于 HTTPS,请选择 SSL 选项。然后将应用程序的域名解析到 SafeLine 服务器的 IP 地址。这些字段和路由步骤在“保护应用程序”文档中有记录。通过公共主机名发送正常的 API 请求,并确认应用程序仍能正常工作。检查您的网络配置,查看是否存在直接暴露上游的替代路由。绕过反向代理的请求无法从其检查中获益。在更改生产环境路由之前,请确保已准备好经过测试的回滚程序。

4. Test JSON bodies as a separate acceptance requirement

The official application guide demonstrates SQL injection testing in a URL query parameter. That example does not establish equivalent inspection of JSON bodies. In staging, start with a valid request, then change one string field at a time to a controlled SQL injection probe, such as a quote followed by a boolean expression. Use an isolated test database and avoid destructive statements, data extraction, and delay-based probes.

4. 将 JSON 体测试作为独立的验收要求

官方应用指南演示了 URL 查询参数中的 SQL 注入测试。该示例并未建立对 JSON 体的等效检查。在预发布环境中,从一个有效的请求开始,然后每次更改一个字符串字段,将其替换为受控的 SQL 注入探测(例如,一个引号后跟一个布尔表达式)。请使用隔离的测试数据库,并避免使用破坏性语句、数据提取和基于延迟的探测。

Repeat the checks for the structures your API actually accepts: top-level strings, nested objects, and array elements. Test alternate encodings only where your application accepts and decodes them. Preserve the expected content type and authentication so a rejected request does not merely reflect invalid JSON or missing credentials. For every case, record three observations: the client response, whether SafeLine records an attack, and whether the upstream received the request. The official guide documents an attack list and a detail view in the console. Correlate those observations using timestamps and available request identifiers. A blocking page alone does not prove that every JSON field is inspected. Conversely, an allowed probe does not prove exploitable SQL injection; the application may safely parameterize it.

针对您的 API 实际接受的结构重复这些检查:顶层字符串、嵌套对象和数组元素。仅在您的应用程序接受并解码的情况下测试替代编码。保持预期的内容类型和身份验证,以确保被拒绝的请求不仅仅是因为 JSON 无效或缺少凭据。对于每种情况,记录三个观察结果:客户端响应、SafeLine 是否记录了攻击,以及上游是否收到了请求。官方指南记录了控制台中的攻击列表和详细视图。使用时间戳和可用的请求标识符关联这些观察结果。仅凭拦截页面并不能证明每个 JSON 字段都已受到检查。反之,允许通过的探测也不代表存在可利用的 SQL 注入;应用程序可能已经对其进行了安全的参数化处理。

5. Make the production decision from evidence

Proceed when normal requests work, representative malicious requests are blocked before reaching the upstream, and application review confirms safe database access. If a probe reaches the application, investigate the request path and inspection coverage before claiming protection for that endpoint. If legitimate input is blocked, inspect the specific request and determine the cause before changing enforcement. Avoid broad exceptions for an entire API simply because one field contains punctuation. Retain a small regression set.

5. 根据证据做出生产环境决策

当正常请求能够工作、具有代表性的恶意请求在到达上游之前被拦截,且应用程序审查确认数据库访问安全时,即可继续。如果探测请求到达了应用程序,请在声称该端点受到保护之前,调查请求路径和检查覆盖范围。如果合法输入被拦截,请在更改执行策略之前检查具体请求并确定原因。避免仅仅因为一个字段包含标点符号就对整个 API 设置广泛的例外。保留一套小型回归测试集。