指南

注射基础知识

注入漏洞是众所周知的漏洞类别之一,尤其是其中最典型的代表:SQL 注入。在科技界,SQL 注入几乎无处不在,因此我们有必要深入探讨一下。

通过 SQL 注入,攻击者可以操纵 SQL 查询的行为,使其执行恶意指令。

此外,还有许多其他类型的注入攻击,虽然表现形式各异,但其基本原理都是相同的。

简单回顾一下, 一些最常见的注入类型包括:

  • SQL 注入
  • 跨站脚本攻击 (HTML/Javascript 注入)
  • 路径遍历 (路径/URL 注入)
  • 命令注入
  • 代码注入

注入攻击基础入门

观察上述注入类型列表,它们都有一个共同点:都涉及一个由解释器运行的字符串,解释器会执行该字符串所代表的任何指令。我们用花括号标出了“用户输入”。

Type Example input How it’s interpreted
SQL SELECT name FROM users WHERE username = '{admin}' Selects the "Name" column from all rows from the users table where the username is 'admin'
HTML {John Smith} Shows the name "John Smith" in bold letters
Path /var/www/app/documents/{privacy-policy.pdf} Points at the file `privacy-policy.pdf` in the `/var/www/app/documents/` folder
Command ping {8.8.8.8} Sends a series of ICMP pings to the IP `8.8.8.8`
Code const name = '{John Smith}'; Sets the constant variable `name` to the value `John Smith

那么,如果用户输入的插入方式不安全,会发生什么?攻击者能做什么?在此场景中,花括号内的所有内容都被视为“用户输入”。

Type Example input How it’s interpreted
SQL - Injected SELECT name FROM users WHERE username = '{1' UNION SELECT passwordhash from users WHERE username = 'admin}' Selects the "Name" from all rows from the users table where the username is 'admin', and the password hash for the users where the username is 'admin'
HTML - Injected {} Show the name "John Smith" in bold letters
Path - Injected /var/www/app/documents/{../../../../../etc/shadow} Points at the file `shadow` in the `/etc/` folder
Command - Injected ping {8.8.8.8 && ls . } Sends a series of ICMP pings to the IP `8.8.8.8`, and prints the contents of the current directory with `ls`
Code - Injected const name = '{John Smith'; exec('ls .'); # }'; Sets the constant variable `name` to the value `John Smith`, and then executes the system command `ls .

在这些示例中,请注意用户输入是如何被用来影响最终结果的。

这就是注入攻击的本质。它通过影响传递给解释器的内容,诱导其执行非程序员预期的操作。

以上只是基础概念。我们将一些不同的注入类型拆分到了单独的页面中,因为它们值得更深入的关注。

您可以在此处找到它们:

命令注入‍

路径遍历‍

SQL 注入‍

跨站脚本攻击

标语

实用安全编码

探索安全编码指南,了解并缓解 OWASP Top 10 等软件漏洞,并通过引导式训练任务在真实的应用程序模拟中进行实践。

更多指南
预约演示