What is SQL injection (SQLi)?

SQL injection is a code injection technique that can destroy your database.

It’s most common web hacking techniques.

Attacker can inject malicious code in SQL statements, via web page input.

The following code is vulnerable to SQL injection because the user input is concatenated directly into the query:

String query = “SELECT * FROM products WHERE category = ‘”+ input + “‘”;Statement statement = connection.createStatement();ResultSet resultSet = statement.executeQuery(query);

How to prevent SQL injection

Most instances of SQL injection can be prevented by using parameterized queries (also known as prepared statements) instead of string concatenation within the query.

  1. Using PDO(for any supported database driver):

$stmt = $pdo->prepare(‘SELECT * FROM employees WHERE name = :name’); $stmt->execute(array(‘name’ => $name)); foreach ($stmt as $row) {    // Do something with $row}

 

  1. Using MySQLi(for MySQL):

$stmt = $dbConnection->prepare(‘SELECT * FROM employees WHERE name = ?’);$stmt->bind_param(‘s’, $name); // ‘s’ specifies the variable type => ‘string’ $stmt->execute(); $result = $stmt->get_result();while ($row = $result->fetch_assoc()) {    // Do something with $row}

 

 What is cross-site scripting (XSS)?

Cross-site Scripting (XSS) is a client-side code injection attack. The attacker aims to execute malicious scripts in a web browser of the victim by including malicious code in a legitimate web page or web application

What is the difference between XSS and SQL injection?

XSS is a client-side vulnerability that targets other application users, while SQL injection is a server-side vulnerability that targets the application’s database.

 

How do I prevent XSS in PHP?

Filter your inputs with a whitelist of allowed characters and use type hints or type casting. Escape your outputs with htmlentities and ENT_QUOTES for HTML contexts, or JavaScript Unicode escapes for JavaScript contexts.

How do I prevent XSS in Java?

Filter your inputs with a whitelist of allowed characters and use a library such as Google Guava to HTML-encode your output for HTML contexts or use JavaScript Unicode escapes for JavaScript contexts.

Security Headers – X-XSS-Protection

 

To improve the security of your site against some types of XSS (cross-site scripting) attacks, it is recommended that you add the following header to your site:

X-XSS-Protection: 1; mode=block

It is supported by IE (Internet Explorer) and Chrome. You can enable it by modifying your Apache settings or your .htaccess file, and adding the following line to it:

Header set X-XSS-Protection “1; mode=block”

Security Headers – X-Content-Type: nosniff

In order to improve the security of your site (and your users) against some types of drive-by-downloads,
it is recommended that you add the following header to your site:

X-Content-Type-Options: nosniff

It is supported by IE (Internet Explorer) and Chrome and prevents them from MIME-sniffing a response from the declared content-type.

Enabling this header

You can enable it by modifying your Apache settings or your .htaccess file, and adding the following line to it:

Header set X-Content-Type-Options nosniff

 

Files & Folders Permission:

You can use the following commands to set permissions on files & directories separately:

To change all the directories to 755 (drwxr-xr-x):

find /Path/to/Directory -type d -exec chmod 755 {} \;

To change all the files to 644 (-rw-r–r–):

find /Path/to/Directory -type f -exec chmod 644 {} \;

You may also like

1 Comment

Leave a Reply