简体   繁体   English

PHP安全检查表(注入,会话等)

[英]PHP Security checklist (injection, sessions etc)

So what kind of things should a person using PHP and MySql be focused on to maximize security. 因此,使用PHP和MySql的人应该着重于哪种事情以最大化安全性。

Things I have done: 我所做的事情:
-mysql_real_escape_string all inputs -mysql_real_escape_string所有输入
-validate all inputs after escaping em -转义后验证所有输入
-Placed random alpha numerics before my table names -在我的表格名称之前放置随机字母数字
-50character salt + Ripemd passwords -50个字符盐+ Rawmd密码

Heres where I think I am slacking: 我认为我懈怠的地方是:
-I know know nothing about sessions and securing them. -我对会话和保护它们一无所知。 How unsafe/safe is it if all you are doing is: 如果您正在做的事情是不安全/有多危险:

session_start(); 
$_SESSION['login']= $login;

and checking it with: 并使用以下命令进行检查:

session_start();
if(isset($_SESSION['login'])){

-I heard something about other forms of injection like cross site injection and what not... -And probably many other things I dont know about. -我听说过其他形式的注射,例如跨站注射,但不是...-还有很多其他我不知道的事情。

Is there a "checklist"/Quicktut on making php secure? 是否有使PHP安全的“清单” / Quicktut? I dont even know what I should be worried about.I kinda regret now not building off cakephp since I am not a pro. 我什至不知道我应该担心什么。我有点遗憾,因为我不是专业人士,所以现在还没有建立cakephp。

You can try to avoid hijacking by testing user agent. 您可以尝试通过测试用户代理来避免劫持。

Something like that : 像这样:

if (isset($_SESSION['userAgent'])) {
    if ($_SESSION['userAgent'] != md5($_SERVER['HTTP_USER_AGENT'])) {
       // HACK !!!
       // Kill the process or ask for authenticating
    }
}
else {
    $_SESSION['userAgent'] = md5($_SERVER['HTTP_USER_AGENT']);
}

If your application allows any kind of content to be posted you should use some kind of encryption at login. 如果您的应用程序允许发布任何类型的内容,则应在登录时使用某种加密。 SSL is best of course, poor mans ssl is encrypting password with JS before posting the login-form. SSL当然是最好的,可怜的人ssl在发布登录表单之前使用JS加密密码。

To avoid session hijacking, tie down the session to the ip at login, (save ip at login and compare with every request). 为了避免会话劫持,请在登录时将会话绑定到IP(在登录时保存IP并与每个请求进行比较)。

regards, //t 问候,// t

For XSS and other injections types, which are mainly HTML and js injection the key security is escaping all outputs.Everything you paste in HTML should be escaped for HTML, that mean no js or HTML should be seen by the browser in the content generated by something directly coming from the databse. 对于XSS和其他注入类型(主要是HTML和js注入),关键的安全性是转义所有输出。在HTML中粘贴的所有内容都应转义为HTML,这意味着浏览器在生成的内容中不应看到js或HTML直接来自数据库的东西。

As an example user_name, that the user as set in a form, should not contain any HTML or js code. 例如,以表单形式设置的用户名user_name不应包含任何HTML或js代码。 If by any way someone an inject such thing, you'll have some problem (and the easiest way to inject is the search form input in most app, were your search is echoed in the response page). 如果以某种方式注入某人,您将遇到一些问题(最简单的注入方法是大多数应用程序中输入的搜索表单,如果您的搜索在响应页面中回显)。 So all theses output should have a htmlspecialchars() before output. 因此,所有这些输出在输出前都应具有htmlspecialchars()。

Here are some usefull links: 以下是一些有用的链接:

There are multiple ways of hacking. 有多种黑客手段。 The first is when an actual (or fake) user is trying to find gaps in your software to try to damage your server. 第一种是当实际(或虚假)用户试图找出软件中的漏洞来破坏服务器时。 You will need the escaping and input checking to prevent SQL injection to work around this. 您将需要转义和输入检查,以防止SQL注入解决此问题。

The (or 'an') other is a hacker that tries to steal a session to impersonate another user. 另一个(或“一个”)是试图窃取会话以冒充另一个用户的黑客。 This allows them to reach (and change) data they are not entitled to. 这使他们能够访问(和更改)他们无权获得的数据。

SQL injection is fixed by using mysql_real_escape_string . 通过使用mysql_real_escape_string修复SQL注入。 When use use that and use it right, there is no need to be afraid of SQL injection. 当使用并正确使用它时,无需担心SQL注入。 There is no need to prepend random characters to table names. 无需在表名前添加随机字符。 This will make your programming harder while not providing a real additional safety. 这将使您的编程更加困难,同时又无法提供真正的额外安全性。 You could also use mysqli and parameterized queries, which don't have this problem at all. 您还可以使用mysqli和参数化查询,它们根本没有这个问题。 mysqli takes care of the escaping for you. mysqli为您处理转义。 Theoretically, parameterized queries could even run faster, because the queries can be more efficiently cached. 从理论上讲,参数化查询甚至可以更快地运行,因为可以更有效地缓存查询。 In practise, however, this is not the case. 但实际上并非如此。 It is only since MySQL 5.2 that these queries are cached at all, but still not as efficient as could be. 只是从MySQL 5.2开始,这些查询才被完全缓存,但仍然不够高效。 That is however nothing to worry about right now. 但是,现在无需担心。 Any solution will proably perform well enough for you right now. 任何解决方案现在都可以为您提供足够好的性能。

One thing you shouldn't do -ever- is allowing PHP code in user generated content. 您永远都不应做的一件事就是允许用户生成的内容中包含PHP代码。 If you allow users to type PHP, you will allow them to break your application and possibly modify your database. 如果允许用户键入PHP,则将允许他们破坏应用程序并可能修改数据库。 Also, when a hacker manages to impersonate a user/content editor, he gets a complete toolbox for free when you allow the content to contain PHP. 另外,当黑客设法模仿用户/内容编辑器时,如果您允许内容包含PHP,他将免费获得完整的工具箱。

To prevent sessions from being hyjacked, I think it is best to use SSL. 为了防止会话被劫持,我认为最好使用SSL。 If you don't want to server all your pages via SSL, you could choose to save a session in cookies, but demand a relogin (using SSL) whenever important changes are done. 如果您不想通过SSL服务器所有页面,则可以选择将会话保存在Cookie中,但是每当进行重要更改时都要求重新登录(使用SSL)。

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

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