English 中文(简体)
防止 Java在PHP网络中的注射
原标题:Preventing JavaScript Injections in a PHP Web Application

有哪些必要措施防止或阻止 Java语注射在PHP网络应用中发生,以便不提供敏感信息(PHP、超文本/X超文本和 Java本中的最佳做法)?

最佳回答

第一个良好步骤是使用。 Gert G联系。 详细涵盖可在不同情况下用于清理投入的各种职能,包括<编码>mysql_real_einski_string,htmlentities(,htmlspecialchars(,strip_tags(addslashes()

最好尽可能避免将用户投入直接输入你的数据库。 Employ whitelist content accreditation:在你只有有限范围的选择的情况下,选择硬编码的插入数值,而不是从任何客户方接受的形式。 基本上,这意味着只有你接受的某些价值观,而不是努力消除/描述邪恶/恶意/恶意投入。

For example: If you have a form with a drop down for items, do not take use the input from this dropdown for insertion. Remember that a malicious client can edit the information sent with the form s submission, even if you think they only have limited options. Instead, have the drop down refer to an index in an array in your server-side code. Then use that array to choose what to insert. This way, even if an attacker tries to send you malicious code, it never actually hits your database.

显然,这不利于像论坛或博客这样的免费应用。 对于这些人来说,你必须回到“第一步”技术上来。 尽管如此,仍有各种选择,可以通过白名单输入鉴定加以改进。

也可使用http://dev.mysql.com/tech-resources/articles/4.1/prepared-statements.html” rel=“noreferer”查询。 (aka编写了具有约束性变量的声明),供你尽可能的地进行q。 这将告诉你的数据库服务器,所有投入只是一种价值,从而缓解了注射袭击可能造成的许多问题。 在许多情况下,甚至可以包括免费申请。

问题回答

页: 1

唯一不使用html专门书的借口是,你需要产出到html,表明自己包含html。 在这种情况下,你必须确保这种扼杀完全是安全的。 如果你没有这种信心,那么你就必须通过白色名单 html过滤器通过这种过滤器,这种过滤只允许精心限定的一组标签、属性以及属性价值。 你们应当特别关注归属价值观。 你们绝不应允许一切东西都作为属性价值,特别是 s、 he、风格。

你们应该知道你在网上访问的所有地点,如果你不使用htmspeciachars,就会知道你确实需要这些地方,并意识到,尽管你们有信心,这些地方是潜在的弱点。

如果你认为这过于谨慎:“我需要html专门分析一下我所知道的这种变数,它只是一劳永逸地罗开了所有珍贵的万国邮联周期?”

牢记这一点: 你们不知道,你只是认为你知道,万国邮联的周期是世界上最廉价的,几乎所有的周期都将被搁置,等待数据库或档案系统,甚至接受记忆。

从未使用黑名单 html过滤器。 优素克认为这一错误,有人突然发现,只有第1条<代码><script>被删除,如果你在评论中加入第2条,你就可以向访客浏览器注入任何 Java印。

同样,为了避免喷射,用我sql_real_einski_string()来看待你 gl到Qry的所有价值,或更好地利用PDO编制的声明。

我不同意所提供的其他答复,因此我将提出我的建议。

Recommended reading XSS_(Cross_Site_Scripting)_Prevention_Cheat_Sheet

Html Injection: Whenever displaying any user submitted content, it should be appropriately cleaned up with htmlspecialchars or htmlentities when specifying ENT_QUOTES if used inside of single quotes. I would recommend never encapsulating in single quotes and always encapsulating your attributes in double quotes (do not omit them). This applies to things such as:

<input value="<?php echo htmlspecialchars($var); ?>" />
<textarea><?php echo htmlspecialchars($var); ?></textarea>
<p><?php echo htmlspecialchars($var); ?></p>
<img width="<?php echo htmlspecialchars($var); ?>" />

Javascript Injection: It is best practice (but not always practical) to never echo user content into events and javascript. However, if you do there are some things that can be done to reduce the risk. Only pass integer id s. If you require something such as a type specifier, then use a whitelist and/or conditional check ahead of time before outputting. Possibly force user content to alphanumeric only when appropriate; preg_replace("/[^A-Za-z0-9]/", , $string); but be very careful what you allow here. Only include content when it is encapsulated in quotes and note that htmlspecialchars/htmlentities does not protect you here. It will be interpreted at runtime even if it has been translated into html entities. This applies to things such as:

<a href="www.stackoverlow.com/?id=<?php echo (int)$id; ?>">Click</a>
href, src, style, onClick, etc.

不要把任何用户内容重复到其他领域,例如文字标签的体格等,除非它被强行置于某种内装上或其他一些极为有限的特性(如果你知道你正在做些什么)。

SQL Injection: Use Prepared statements, bind user content to them, and never directly insert user content into the query. I would recommend creating a class for prepared statements with helper functions for your different basic statement types (and while on the subject, functionalize all of your database statements). If you choose not to use prepared statements then use mysql_real_escape_string() or similar (not addslashes()). Validate content when possible before storing into the database such as forcing/checking for integer data type, conditional checks on types, etc. Use proper database column types and lengths. Remember the main goal here is to prevent sql injection but you can optionally do html/javascript injection protection here as well.

Other Resources I have done some research online in hopes to find a simple solution already publicly available. I found OWASP ESAPI but it appears quite dated. The links to the php version are broken in several places. I believe I found it here; ESAPI PHP but again it is quite dated and not as simple as I was hoping for. You may find it useful however.

毕竟,从来就没有假定你会重新受到保护,例如,在浮雕的特性中使用html。 你们必须在正确的地点使用正确的工具,避免在错误的地点做事。

这个问题已经得到用户接受和评定的一些答案。

相反,我也做了回答,希望这将奏效。

我对此进行了测试。

1) Use strip_tags()  //Prevent html injection

2) mysqli_real_escape_string //suspicious element

3) preg_replace("/[ ")(;|`,<>]/", "", $value); //This will replace match

你可以尝试你们喜欢的东西。





相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签