php - 基本的sql注入(inject)

标签 php mysql

我正在努力学习 SQL 注入(inject),以便将来可以保护自己。

这是 PHP 代码:

  $req = mysql_query("INSERT INTO ip_change VALUES('', '".$_SESSION['id']."', '".$_POST['raison']."')") or die(mysql_error());

用户可以完全控制 $_POST['raison'] 内容。

当我使用 'hello 作为 $_POST['raison'] 值时,我得到

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'hello')' at line 1

当我使用'); DELETE * FROM tabledetest;") 或 die(mysql_error());-- 作为我得到的 $_POST['raison'] 值

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'DELETE * FROM tabledetest;") or die(mysql_error());--')' at line 1

所以我不明白为什么我的请求没有被注入(inject),并且我无法删除我的 tabledetest 表。

如有任何帮助,我们将不胜感激。

最佳答案

那是因为你没有进行正确的注入(inject)!

这是您所做的。自动格式会提示您:

<?php
$_SESSION['id'] = "123"; //Just assume

$req = mysql_query("INSERT INTO ip_change VALUES('', '123', ''hello')") or die(mysql_error());

它没有正确结束声明。

对于下一个:

$req = mysql_query("INSERT INTO ip_change VALUES('', '123', ''); DELETE * FROM tabledetest;") or die(mysql_error());--')") or die(mysql_error());
  1. 来自 the manual :

mysql_query() sends a unique query (multiple queries are not supported) to the currently active database on the server that's associated with the specified link_identifier.

mysqli 支持 multiple statements .

  • -- 无法注释 PHP 代码! PHP 注释为 //#
  • 一些可能对您有帮助的链接:[与您的问题类似]

    1. https://en.wikibooks.org/wiki/PHP_Programming/SQL_Injection_Attacks
    2. http://roshanbh.com.np/2007/12/sql-injection-attack-examples-and-preventions-in-php.html
    3. SQL injection test - mysql_query

    关于php - 基本的sql注入(inject),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36969254/

    相关文章:

    javascript - php 正则表达式不工作,返回主题字符串而不是正则表达式结果

    PHP 从数据库导出 CSV 表

    php - 将数据从一个 MySQL 表传输到另一个

    Mysql 查询执行时间太长。为什么?

    javascript - 如何在jsp中保留mysql的last_insert_id()

    php - 如何在不更新或创建新行的情况下向同一用户添加更多数据?

    Java Web 应用程序无法连接到 AWS RDS 数据库实例

    php - Laravel carbon createFromFormat 添加 1 年

    清理所有数据的 PHP 函数

    javascript - 在具有相同类的多个 div 上启动 ProgressBar