Unit - III - WEB SECURE API
Unit - III - WEB SECURE API
Cookies Session
Cookies are small text files used to store user Sessions are used to store user
information on the user’s computer. information on the user’s server side.
Cookies expire after a specified lifetime or A session ends when the user closes the
duration. browser or logs out.
Cookies can only store a limited amount of Sessions have a 128 MB size to store
data of 4KB in a browser. data for one time.
1. Request: The user intends to enter the service with login credentials on the application or
the website interface. The credentials involve a username, password, smartcard, or biometrics
2. Verification: The login information from the client-server is sent to the authentication
server for verification of valid users trying to enter the restricted resource. If the credentials
pass the verification the server generates a secret digital key to the user via HTTP in the form
of a code. The token is sent in a JWT open standard format which includes-
Header: It specifies the type of token and the signing algorithm.
Payload: It contains information about the user and other data
Signature: It verifies the authenticity of the user and the messages transmitted.
3. Token validation: The user receives the token code and enters it into the resource server to
grant access to the network. The access token has a validity of 30-60 seconds and if the user
fails to apply it can request the Refresh token from the authentication server. There’s a limit
on the number of attempts a user can make to get access. This prevents brute force attacks
that are based on trial-and-error methods.
4. Storage: Once the resource server validated the token and grants access to the user, it
stores the token in a database for the session time you define. The session time is different for
every website or app. For example, Bank applications have the shortest session time of about
a few minutes only.
So, here are the steps that clearly explain how token-based authentication works and what are
the main drivers driving the whole security process.
DEFINITION A Denial of Service (DoS) attack aims to prevent legitimate users from
accessing your API. This can include physical attacks, such as unplugging network cables,
but more often involves generating large amounts of traffic to overwhelm your servers. A
distributed DoS (DDoS) attack uses many machines across the internet to generate traffic,
making it harder to block than a single bad client.Many DoS attacks are caused using
unauthenticated requests. One simple way to limit these kinds of attacks is to never let
unauthenticated requests consume resources on your servers. Authentication is covered in
section 3.3 and should be applied immediately after rate-limiting before any other processing.
However, authentication itself can be expensive so this doesn’t eliminate DoS threats on its
own.
Network-level DoS attacks can be easy to spot because the traffic is unrelated to
legitimate requests to your API. Application-layer DoS attacks attempt to overwhelm an API
by sending valid requests, but at much higher rates than a normal client. A basic defense
against application-layer DoS attacks is to apply rate-limiting to all requests, ensuring that
you never attempt to process more requests than your server can handle. It is better to reject
some requests in this case, than to crash trying to process everything. Genuine clients can
retry their requests later when the system has returned to normal.
DEFINITION Application-layer DoS attacks (also known as layer-7 or L7 DoS)
send syntactically valid requests to your API but try to overwhelm it by sending a very large
volume of requests. Rate-limiting should be the very first security decision made when a
request reaches your API. Because the goal of rate-limiting is ensuring that your API has
enough resources to be able to process accepted requests, you need to ensure that requests
that exceed your API’s capacities are rejected quickly and very early in processing. Other
security controls, such as authentication, can use significant resources, so rate limiting must
be applied before those processes, as shown in figu
3.5. Encryption
Encryption is used to protect data from being stolen, changed, or compromised and
works by scrambling data into a secret code that can only be unlocked with a unique
digital key.
Encryption is the process by which a readable message is converted to an unreadable
form to prevent unauthorized parties from reading it.
Decryption is the process of converting an encrypted message back to its original
(readable) format. The original message is called the plaintext message.
Encryption works by encoding “plaintext” into “ciphertext,” typically through the use of
cryptographic mathematical models known as algorithms. To decode the data back to
plaintext requires the use of a decryption key, a string of numbers or a password also created
by an algorithm. Secure encryption methods have such a large number of cryptographic keys
that an unauthorized person can neither guess which one is correct, nor use a computer to
easily calculate the correct string of characters by trying every potential combination
One early example of a simple encryption is the “Caesar cipher,” named for Roman emperor
Julius Caesar because he used it in his private correspondence. The method is a type of
substitution cipher, where one letter is replaced by another letter some fixed number of
positions down the alphabet. To decrypt the coded text, the recipient would need to know the
key to the cipher, such as shifting down the alphabet four places and over to the left (a “left
shift four”). Thus, every “E” becomes a “Y” and so on.
Modern cryptography is much more sophisticated, using strings of hundreds (even thousands,
in some cases) of computer-generated characters as decryption keys.
Types of encryption
The two most common types of encryption algorithms are symmetric and asymmetric.
Symmetric encryption, also known as a shared key or private key algorithm, uses the same
key for encryption and decryption. Symmetric key ciphers are considered less expensive to
produce and do not take as much computing power to encrypt and decrypt, meaning there is
less of delay in decoding the data.
The drawback is that if an unauthorized person gets their hands on the key, they will be able
to decrypt any messages and data sent between the parties. As such, the transfer of the shared
key needs to be encrypted with a different cryptographic key, leading to a cycle of
dependency.
Asymmetric encryption, also known as public-key cryptography, uses two separate keys to
encrypt and decrypt data. One is a public key shared among all parties for encryption.
Anyone with the public key can then send an encrypted message, but only the holders of the
second, private key can decrypt the message.
Common encryption algorithms
The most common methods of symmetric encryption include:
Data Encryption Standard (DES): An encryption standard developed in the early 1970s,
DES was adopted by the US government in 1977. The DES key size was only 56 bits,
making it obsolete in today’s technology ecosystem. That being said, it was influential in the
development of modern cryptography, as cryptographers worked to improve upon its theories
and build more advanced encryption systems.
Triple DES (3DES): The next evolution of DES took the cipher block of DES and applied it
three times to each data block it encrypted by encrypting it, decrypting it, and then encrypting
it again. The method increased the key size, making it much harder to decrypt with a brute
force attack. However, 3DES is still considered insecure and has been deprecated by the US
National Institute of Standards (NIST) for all software applications .
Advanced Encryption Standard (AES): The most used encryption method today, AES was
adopted by the US government in 2001. It was designed on a principle called a “substitution–
permutation network” that is a block cipher of 128 bits and can have keys at 128, 192, or 256
bits in length.
Twofish: Used in both hardware and software, Twofish is considered the fastest symmetric
encryption method. While Twofish is free to use, it’s not patented nor open source.
Nevertheless, it’s used in popular encryption applications like PGP (Pretty Good Privacy). It
can have key sizes up to 256 bits.
The most common methods of asymmetric encryption include:
RSA: Stands for Rivest-Shamir-Adelman, the trio of researchers from MIT who first
described the method in 1977. RSA is one of the original forms of asymmetric encryption.
The public key is created by the factoring of two prime numbers, plus an auxiliary value.
Anyone can use the RSA public key to encrypt data, but only a person who knows the prime
numbers can decrypt the data. RSA keys can be very large (2,048 or 4,096 bits are typical
sizes) and are thus considered expensive and slow. RSA keys are often used to encrypt
the shared keys of symmetric encryption.
Elliptic Curve Cryptography (ECC): An advanced form of asymmetric encryption based
on elliptic curves over finite fields. The method provides the robust security of massive
encryption keys, but with a smaller and more efficient footprint. For instance, a “256-bit
elliptic curve public key should provide comparable security to a 3,072-bit RSA public key.”
Often used for digital signatures and to encrypt shared keys in symmetric encryption.
Importance of data encryption
People encounter encryption every day, whether they know it or not. Encryption is used for
securing devices such as smartphones and personal computers, for protecting financial
transactions such as making a bank deposit and buying an item from an online retailer, and
for making sure messages such as email and texts are private.
If you’ve ever noticed that a website’s address starts with “https://” (the “s” means “secure”)
it means that the website is using transport encryption. Virtual private networks (VPNs) use
encryption to keep data coming and going from a device private from prying eyes.
Data encryption is important because it helps protect people’s privacy, and secures data from
attackers and other cybersecurity threats. Encryption is often mandatory from a regulatory
perspective for organizations such as in healthcare, education, finance and banking, and
retail.
Encryption performs four important functions:
Confidentiality: keeps the contents of the data secret
Integrity: verifies the origin of the message or data
Authentication: validates that the content of the message or data has not been altered
since it was sent
Nonrepudiation: prevents the sender of the data or message from denying they were
the origin
Advantages of encryption
Protects data across devices
Data is constantly on the move, be it messages between friends or financial transactions.
Encryption paired with other security functions like authentication can help keep data safe
when it moves between devices or servers.
Ensures data integrity
In addition to keeping unauthorized people from seeing the plaintext of data, encryption
safeguards the data so that malicious actors cannot use it to commit fraud or extortion, or
change important documents.
Protects digital transformations
With more organizations and individuals using cloud storage, encryption plays a key role in
protecting that data while it is in-transit to the cloud, once it is at rest on the server, and while
it’s being processed by workloads. Google offers different levels of encryption, as well as key
management services.
Disadvantages of encryption
Ransomware
While encryption is generally used to protect data, malicious actors can sometimes use it to
hold data hostage. If an organization is breached and its data accessed, the actors can encrypt
it and hold it ransom until the organization pays to have it released.
Key management
Encryption is much less effective if the cryptographic keys that encrypt and decrypt the data
are not secure. Malicious actors often concentrate their attacks on obtaining an organization’s
encryption keys. In addition to malicious actors, losing encryption keys (such as during a
natural disaster that compromises servers) can lock organizations out of important data. This
is why a secure key management system is often used by organizations to manage and secure
their keys.
Quantum computing
Quantum computing poses an existential threat to modern encryption techniques. When it is
ready, quantum computing will be able to process massive amounts of data in a fraction of
the time of normal computers. As such, quantum computing has the potential to break
existing encryption. In the future, all organizations will have to adapt encryption techniques
by using quantum encryption techniques. Currently, quantum computing is relatively limited
and not yet ready to break modern encryption standards. However, NIST has announced their
support of four new “quantum-resistant” algorithms that are designed to withstand quantum
computer attacks.
When you use a technology service or product, audit logs are generated in response to every
user action and system response. These logs capture critical information that can be used to:
Authenticate users.
Identify and validate requests.
Route requests to the appropriate service nodes.
Perform relevant technology operations and processing.
While both audit logs and system logs record events and actions, they serve distinct purposes:
Audit Logs capture who did what, where, and when. They are primarily used for compliance,
security, and computer forensic investigations. Audit logs track user actions and system
changes to ensure accountability and traceability. They provide a chronological record of
activities, crucial for audits and compliance checks.
System Logs primarily record system events and operational activities, such as errors,
performance data, and service statuses. System logs are mainly used for debugging,
monitoring system health, and optimizing performance. They offer insights into the
operational state and efficiency of the system.
SOC1 imposes requirements for incident detection, configuration, management, and event log
collection.
(See how Splunk supports organizational compliance.)
Use case: Accountability & authentication
As with standard audit procedures, audit logging is frequently used for accountability and
verification of factual information. Common applications include:
Organizational policy enforcement
Accounting and finance
HR policies
In this context, audit logging is an important part of analyzing how users act and the accuracy
of information recorded by the systems. For example, audit logging can quickly enable
systems and uncover insights into the use of financial resources across all departments.
Imagine a world where all this was straightforward:
Authorizing and spending finances.
Understanding which users are responsible for the most spending.
Comparing against budget allocations.
Use case: Cyber forensics
Cyber forensics is another key application domain of audit logging practices that requires the
reconstruction of events and insights into a technology process. Often, this might stand up as
legal evidence in a court of law.
Typically, businesses aren’t conducting cyber forensics for all their activities. Instead, we
usually require cyber forensics in two situations:
An external requirement for investigation in the form of a court subpoena
An internal request by business executives and technical teams, perhaps around a major cyber
incident or significant, unplanned downtime in a website or system
Audit logs outline the action sequences that connect a user to an action. Investigators can
analyze audit logs to gain deeper insights into various scenarios and outcomes represented by
the audit logs. This requires a thorough analysis of raw logging data before it is converted
into insightful knowledge.
Audit logging best practices
Considering the vast volume of network, hardware, and application logs generated at scale, IT
teams can be easily overwhelmed by the audit trail data. To gain the right insights with your
audit log metrics data, you can adopt the following best practices:
Store all structures at a scale
Establish a data platform that can integrate and store data of all structural formats at scale. Data
platform technologies such as a data lake commonly capture real-time log data streams with a
schema-on-read consumption model.
Third-party analytics and monitoring tools integrate to make sense of this information in real-
time while processing only the most relevant portions of audit logs data based on the tooling
specifications for data structure.
Use statistical models, not predefined thresholds
Use statistical models to generalize system behavior instead of using predefined and fixed
thresholds to capture data. Since the network behavior evolves continuously, models based on
machine learning can continuously learn and adapt.
These models are helpful for accurate analysis of audit logs, where thresholds for anomalous
behavior can be a moving target.
Secure data with eye to CIA triad
Store audit logging data in secure environments with high standards of confidentiality,
integrity, and availability — known as the CIA triad. Modified audit logs and misconfigured
networking systems can generate misleading information, and likely lead your log analysis to
incorrect conclusions.
Infinite data storage is not sustainable
It is important to understand that data stores that integrate large volumes of real-time log data
streams can grow exponentially. When designing the data platform for audit log analysis,
evaluate the cost, security, and performance of your data platform against your security and
compliance requirements.
Additionally, implementing quotas and limits on logging uses is crucial to managing storage
efficiently. Setting quotas ensures that logging does not consume excessive resources and
helps maintain system performance. Define limits based on the importance and relevance of
the logs, ensuring that only critical data is retained long-term.
1. Use HTTPS
Ensure all incoming requests are served over HTTPS to encrypt the data in transit.
HTTPS prevents man-in-the-middle attacks and ensures the integrity of data sent
between the client and server.
2. Input Validation
Sanitize inputs: Ensure that any user input, query parameters, or data submitted via
forms is sanitized to prevent attacks like SQL injection or XSS.
Use whitelists: Limit accepted inputs to known good values using whitelisting rather
than blacklisting.
3. Authentication and Authorization
Token-based authentication: Implement token-based systems like JWT (JSON Web
Token) to validate incoming requests.
Session management: Secure sessions with strong session tokens, ensuring that
session data is not predictable or accessible by unauthorized users.
Role-based access control: Ensure that users are authenticated and have the correct
permissions to access certain resources or perform specific actions.
4. Rate Limiting and Throttling
Apply rate limiting to prevent brute-force attacks or denial of service (DoS) attacks.
By limiting the number of requests a client can make in a given time, you reduce the
risk of abuse.
5. Cross-Origin Resource Sharing (CORS)
Configure CORS properly to ensure only trusted domains are allowed to interact with
your API. This reduces the risk of cross-origin attacks by controlling which domains
are permitted to send requests.
6. CSRF Protection
Implement anti-CSRF (Cross-Site Request Forgery) tokens to ensure that actions
performed by authenticated users are genuinely authorized. This helps prevent
unauthorized commands being sent from trusted users.
7. Use Security Headers
Implement security headers such as:
o Content-Security-Policy: Prevents XSS by restricting what resources can be
loaded.
o X-Content-Type-Options: Prevents the browser from interpreting files as a
different MIME type.
o X-Frame-Options: Protects against clickjacking by controlling whether your
content can be displayed in an iframe.
o Strict-Transport-Security (HSTS): Forces browsers to use HTTPS and
prevents them from using insecure connections.
8. Logging and Monitoring
Log incoming requests and monitor for unusual patterns (e.g., high rates of requests
from a single IP or malicious payloads).
Use monitoring tools that can alert you of potential intrusions or malicious activities
in real-time.
9. Use Web Application Firewall (WAF)
A WAF helps protect against common attacks like SQL injection, XSS, and more. It
inspects incoming traffic and can block or flag requests that contain malicious
payloads.
10. Input Size Limits
Set size limits on incoming data (e.g., request body, headers, etc.) to prevent attackers
from sending overly large payloads, which could lead to buffer overflow attacks or
denial of service.
11. Security Patches
Regularly update server software, frameworks, and libraries to ensure that
vulnerabilities in third-party software are addressed.