Unit 5
Unit 5
Unit 5
• Strong passwords and MFA: Use strong, unique passwords for all your
online accounts and enable multi-factor authentication (MFA) whenever
possible. MFA adds an extra layer of security by requiring a second
verification step beyond just your password.
• Anti-virus and anti-malware software: Keep your devices up-to-date with
the latest security software to help protect against malware that might be
installed through social engineering tricks.
• Browser security settings: Enable features like pop-up blockers and
website verification to warn you about suspicious websites.
Organizational Measures
• Security awareness training: Organizations should provide regular
security awareness training to employees to educate them on social
engineering techniques and best practices for secure information
handling. This training should be ongoing, as new tactics emerge.
• Reporting procedures: Establish clear procedures for employees to
report suspected social engineering attempts. This allows for investigation
and helps the organization identify and address vulnerabilities.
• Limited access controls: Implement access controls that restrict
employee access to data and systems based on their job duties. This
principle of least privilege minimizes the potential damage if an employee
falls victim to a social engineering attack.
Injection
• SQL Injection
• Code Injection
• Command Injection
• Mitigating SQL Injection
• Generic Injection Defenses
Injection attack
• An injection attack in web application security is a malicious
technique where an attacker inserts (injects) unauthorized code into a
seemingly legitimate part of a web application
• This code can then trick the application into performing unintended
actions, potentially compromising sensitive data or even taking over
the entire system.
Types of injection attacks
• SQL Injection (SQLi): The attacker injects malicious SQL code to
manipulate database queries. This can be used to steal data, modify
information, or even gain unauthorized access to the database server.
• In all the three cases, the malicious code is sent to the web server in a HTTP
request
SQL Injection
• Here, the attacker manipulates database queries for malicious purposes.
• SQL injection attacks take place in the SQL interpreter
The attacker might use the UNION operator to combine the malicious
SQL query with the original query, retrieving unintended data
Snippet of code from simple
Node.js/Express.js server
/* Query the database, providing the `user_id` param from the HTTP
request body. */
const result = await sql.query('SELECT * FROM users WHERE USER = ‘ +
user_id);
/* Return the result of the SQL query to the requester in the HTTP response. */
return res.json(result)
In the case of a valid user_id, this query will return a user object to the
requester.
• const user_id = ‘1=1’
• The query looks like SELECT * FROM users WHERE USER = 1=1;
For this, the query will return ALL rows from the “users" table,
since 1=1 is always TRUE.
If users’ passwords are stored in the table, then the passwords of all
the users will be returned
• user_id = '123abc; DROP TABLE users;’
The query looks like SELECT * FROM users WHERE USER = 123abc;
DROP TABLE users;
This query deletes the users table permanently
• const user_id = '123abc; UPDATE users SET credits = 10000 WHERE user =
123abd;’
The query looks like SELECT * FROM users WHERE USER = '123abc;
UPDATE users SET credits = 10000 WHERE user = 123abd;
This query updates our own user account in the database
Code Injection
• The attacker injects code that gets executed on the web server itself.
Here, the attack takes place in the Command Line Interface (CLI)
Command Injection
• Command injection is an attack on the host operating system i.e. on the
operating system of web server.
• If we have direct access to the host Unix OS (over 95% of servers are Unix-
based), and our commands are interpreted as a super user, we can do
anything we want to that OS
• A compromised OS gives the hacker access to a number of very integral
files and permissions, such as:
• /etc/passwd
Keeps track of every user account on the OS
• /etc/shadow
Contains encrypted passwords for users
• ~/.ssh
Contains SSH keys for communicating with other systems
• /etc/apache2/httpd.conf
Configuration for Apache-based servers
• /etc/nginx/nginx.conf
Configuration for Nginx-based servers
• We can make use of command injection to cause more havoc than
expected including
Steal data from the server
Rewrite log files to hide our tracks
Add an additional database user with write access for later use.
Delete important files on the server.
Wipe the server and kill it.
Make use of integrations with other servers/APIs (e.g., using a server’s
Sendgrid keys to send spam mail)
Change a single login form in the web app to be a phishing form that
sends unencrypted passwords to our site.
Lock the admins out and blackmail them
Mitigating SQL injection
Prepared statements
• Prepared statements are often considered the “first line” of defense
against injection
• Prepared statements are easy to implement and well documented on the
web
A prepared statement separates the SQL code itself from the data being
used in the query.
The SQL statement acts as a template with placeholders (usually
question marks) representing the data values.
Traditional SQL Query
username = 'user123’
password = 'mypassword’
query = "INSERT INTO users (username, password) VALUES ('" +
username + "', '" + password + “’)”
# Execute the query
• The above approach is vulnerable to injection attack if username or password
contains malicious code
• In a traditional SQL query, the user-submitted data (variables) and the query
itself are sent to the interpreter/database together in the form of a string.
This means that if the user data is manipulated, it could change the intention
of the query.
Prepared statements
prepared_stmt = "INSERT INTO users (username, password)
VALUES (?, ?)"
# Execute with data binding
stmt.execute((username, password))
• In this example, username and password are provided separately and
securely bound to the placeholders only during execution
Prepared statements
• Prepared statements works in two steps:
First, the query (with placeholder values for variables) is sent to the
interpreter/database. So, the intention of the query is set and cannot
be changed
Next, the placeholders are replaced with the values provided by the
developer.
• It is because of the two step process, the user data cannot change the
intention of the query which was set in step 1.
Generic Injection Defenses
Principle of Least Authority
• The principle of least authority (often called principle of least
privilege) states that in any system, each member of the system
should only have access to the information and resources required to
accomplish their job
Cross Site Request Forgery (CSRF)
• Default Configurations
Many systems and applications come with default configurations that are
not secure. This could include default usernames and passwords, which
can be easily exploited if not changed.
• Unnecessary Features Enabled
Systems often come with many features enabled by default, some of
which may not be necessary for your operations. These unnecessary
features can increase your system's vulnerability.
• Excessive Permissions
A mobile application that requests access to more resources on the device
than it actually needs to function is another example. For instance, a note-
taking app that requests access to your contacts and location could be a
misconfiguration.
• Unnecessary Ports Open
If a firewall is misconfigured to leave unnecessary ports open, it could
expose the network to potential attacks. For instance, having port 22 (used
for SSH) open to the entire internet can invite brute-force attacks.
• All of this information has to be sent back and forth between the user and
the server. If that information is not encrypted and is sent as plain text
instead, it becomes an attack vector. Hackers can then intercept user
credentials or session IDs to impersonate that person. This is
especially true when operating on a public network (e.g. coffee shop
wifi) or a public computer that anyone else can access.
Examples of Broken Authentication and
Session Management
• Credential stuffing
The stealing of usernames and passwords to gain unauthorized
access to user accounts across multiple websites and services is known
as credential stuffing. This technique relies on the fact that many
people reuse the same login credentials across different online
platforms. Attackers typically obtain these credentials from
breaches of other websites and then use automated tools to test
them on various websites in hopes of finding matches. Credential
stuffing exploits the widespread issue of password reuse and can lead
to unauthorized access to user accounts, compromising sensitive
information, and leading to financial or reputational damage.
Examples of Broken Authentication and
Session Management
• Brute force attacks
Another approach a cybercriminal could take is attempting a
brute-force attack wherein they repeatedly try common weak
passwords to guess a user’s correct password. It is also possible for
attackers to forge session IDs if they are not randomly generated.
For example, if an attacker intercepts several legitimate session IDs that
are enumerated, it is possible to guess the next legitimate session ID
and access the site fraudulently. These are commonly referred to as
man-in-the-middle attacks
Examples of Broken Authentication and
Session Management
• Password spraying attacks
• This type of cyberattack uses a single password against many user
accounts before moving on to another password to avoid triggering
account lockouts. This technique contrasts with brute force attacks,
which try many passwords against a single user account. Password
spraying targets the common use of weak passwords across multiple
accounts and takes advantage of the fact that many users opt for
simplicity over security. By exploiting the likelihood that at least some
accounts will use common passwords, attackers can gain unauthorized
access without alerting the authentication mechanisms designed to
lock accounts after a few unsuccessful login attempts.
What are the risks?
• Unauthorized Access to Sensitive Information
When attackers exploit broken authentication vulnerabilities, they can
gain access to sensitive data such as personal information, financial
details, or intellectual property. This unauthorized access can lead to
data breaches and privacy violations.
• Escalation of Privileges
If the compromised account belongs to an administrator or privileged user,
attackers can escalate their privileges within the application. This can lead
to complete system compromise and greater control over critical functions.
What are the risks?
• Financial Losses and Legal Consequences
The aftermath of a broken authentication attack can result in financial
losses for businesses, especially if customer trust is compromised.
Moreover, organizations may face legal consequences for failing to
protect user data adequately.
How to prevent broken authentication
attacks
• To prevent man-in-the-middle type attacks on your site’s sessions, it is
important to encrypt this data in transit using an SSL certificate.
• Use a VPN
A VPN (virtual private network) is another effective way to protect
yourself from broken authentication vulnerabilities. VPNs enable users to
send and receive data across shared or public networks privately.
How to prevent broken authentication
attacks
• Implement a web application firewall
You can prevent attackers from exploiting vulnerabilities or forging
session IDs by using a web application firewall (WAF). A WAF is
designed to scan and filter all incoming traffic to a website—it only
lets good visitors in and keeps malicious ones out.
• Using encryption keys that are too short or too weak, such as using a 128-
bit key for AES encryption instead of a 256-bit key.
Input validation: Validate all user input related to URLs to prevent them
from manipulating URLs to access restricted pages. This is like checking IDs
before granting access to restricted areas.