简体   繁体   English

在哪里使用mysql_real_escape_string来阻止SQL注入?

[英]Where to use mysql_real_escape_string to prevent SQL Injection?

I'm in trouble with a group of hackers. 我和一群黑客有麻烦。 they hacked my client's site few times, and my client gets more angry :( my client lost his database (which has hundreds records), and had to enter all :( 他们几次攻击我的客户的网站,我的客户端更生气:(我的客户丢失了他的数据库(有数百条记录),并且不得不输入所有:(

now I'm following some more introductions; 现在我正在进行更多的介绍;

  • fixed file permissions 修复了文件权限
  • changed ftp and host login info 更改了ftp和主机登录信息
  • cleared all remote mysql accesses 清除所有远程mysql访问

now working on SQL Injection issue. 现在正致力于SQL注入问题。 I added mysql_real_escape_string to admin panel login paramaters. 我将mysql_real_escape_string添加到管理面板登录参数。 So where else should I use this mysql_real_escape_string ? 那么我应该在哪里使用这个mysql_real_escape_string呢? I have few email forms at site, I dont think i need to add there... 我在网站上的电子邮件表格很少,我不认为我需要添加...

I have an index.php as a mainpage. 我有一个index.php作为主页。 Should I do anything for this page to prevent any sql injection attack via url like index.php?somesql= ? 我是否应该为此页面做任何事情以防止任何sql注入攻击通过url如index.php?somesql=

Please advise me! 请建议我! I appreciate so much!!! 我很感激!!! :( :(


for example: 例如:

I have such code; 我有这样的代码;

public function showDetails($id) {

    // SQL Jobs Details
    $this->sql_job = "SELECT * FROM jobs WHERE id=".mysql_real_escape_string($id);
    $this->rst_job = mysql_query($this->sql_job);           
    $this->row_all = mysql_fetch_assoc($this->rst_job);     

    // SQL State
    $this->sql_state = "SELECT title FROM state WHERE id=" . $this->row_all[$this->tbl_jobs['f4']];
    $this->rst_state = mysql_query($this->sql_state);   
    $this->row_state = mysql_fetch_assoc($this->rst_state);
........

is it enough to use mysql_real_escape_string for $id . 是否足以使用mysql_real_escape_string作为$ id。 not for $this->row_all[$this->tbl_jobs['f4']] 不是$ this-> row_all [$ this-> tbl_jobs ['f4']]

Basically, each time you use some unsafe data (user input, value from a database, a file or an external website, ie any data that you are not 100% sure that it is safe) in a SQL query, you should escape it using mysql_real_escape_string . 基本上,每次在SQL查询中使用一些不安全的数据(用户输入,数据库,文件或外部网站的值,即您不能100%确定它是安全的任何数据)时,您应该使用它来转义它mysql_real_escape_string Note that according to OWASP , this function is not secure for escaping dynamic table names (but this is far less common than "basic" user input insertion). 请注意, 根据OWASP ,此函数对于转义动态表名称并不安全(但这远不如“基本”用户输入插入那么常见)。

I suggest you to have a look at the whole OWASP article on SQL injection , and also to browse the rest of the website. 我建议你看一下关于SQL注入的整篇OWASP文章 ,还要浏览网站的其他部分。 It's a great source of information about security in web applications. 它是有关Web应用程序安全性的重要信息来源。

IMO, the preferred way of preventing SQL injection is to use prepared statements . IMO,防止SQL注入的首选方法是使用预准备语句

Please remember that if you do choose to use mysql_real_escape_string() it only works when used inside a string that is delimited by quotes. 请记住,如果您确实选择使用mysql_real_escape_string()它只能在由引号分隔的字符串中使用时才有效。 Never use it on any unquoted values. 切勿在任何未引用的值上使用它。 This includes numeric values; 这包括数值; instead, validate that the user-input is actually numeric. 相反,验证用户输入实际上是数字。

The two biggest things to do with user input are 用户输入的两个最重要的事情是

  1. Input Filtering 输入过滤
  2. Output Escaping 输出转义

Input Filtering is the process of transforming the data /[before]/ it's stored in the database. 输入过滤是转换数据/ [之前] /它存储在数据库中的过程。 Executing mysql_real_escape_string() falls under this step (although there are better ways to sanitize user data for db insertion), but this step can also include trimming white-space, profanity filtering, markup conversion, and more. 执行mysql_real_escape_string()属于此步骤(尽管有更好的方法可以清理数据库插入的用户数据),但此步骤还可以包括修剪空白区域,亵渎过滤,标记转换等。

Output Escaping is taking care when send user-content to the browser that you don't allow malicious behavior. 将用户内容发送到不允许恶意行为的浏览器时,输出转义非常重要。 This means executing htmlentities() or some other selective screening process. 这意味着执行htmlentities()或其他一些选择性筛选过程。

There are other things you can do, like resource throttling (DOS prevention), form tokens (CSRF protection), etc. Go to OWASP and start reading. 您还可以执行其他操作,例如资源限制(DOS预防),表单令牌(CSRF保护)等。转到OWASP并开始阅读。

One of the golden rules of web development is NEVER (EVER!) trust user input. Web开发的黄金法则之一是永远(永远!)信任用户输入。 Therefore, anywhere you have data going into the database, you should call mysql_real_escape_string(). 因此,在数据进入数据库的任何地方,都应该调用mysql_real_escape_string()。

Also, to prevent angry clients in the future, you should regularly backup your database. 此外,为了防止将来生气的客户,您应该定期备份您的数据库。 If I were your client, I would be furious right now. 如果我是你的客户,我现在会生气。

Good luck in securing your site. 祝你的网站安全。

The best way to prevent SQL injection is with use of prepared statements and bind variables. 防止SQL注入的最佳方法是使用预准备语句和绑定变量。 What version of MySQL are you using? 您使用的是哪个版本的MySQL? Prepared statements are available in 4.1 and higher. 准备好的声明有4.1及更高版本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM