HTTP Security Headers

[T1]

HTTP HeaderDescriptionExample VulnerabilityExample Use-Case
Strict-Transport-Security (HSTS)Instructs browsers to only communicate via HTTPS, preventing Man-in-the-Middle (MitM) attacks.Downgrade attacks (HTTP to HTTPS)Forcing secure connections on a web application.
Content-Security-Policy (CSP)Defines trusted sources for content, mitigating XSS and data injection attacks.Cross-Site Scripting (XSS)Preventing inline scripts and unauthorized resource loading.
Cross-Origin Resource Sharing (CORS)Controls access to resources from different origins, reducing the risk of CSRF and data leaks.Cross-Site Request Forgery (CSRF)Allowing specific domains to access APIs.
X-Frame-OptionsPrevents clickjacking by controlling whether a page can be displayed in a frame or iframe.Clickjacking attacksProtecting sensitive actions from being deceived through framing.
X-XSS-ProtectionEnables the browser’s built-in XSS protection to block identified attacks.Cross-Site Scripting (XSS)Allows browsers to detect and block reflected XSS.
X-Content-Type-OptionsPrevents browsers from MIME sniffing content types, reducing the risk of certain attacks.MIME type confusionEnsuring proper handling of file types served by an application.
Referrer-PolicyControls the amount of information shared when navigating from one page to another, enhancing privacy.Referrer leakageLimiting sensitive URL information while navigating.
Permissions-PolicyEnables or disables the use of certain features (like geolocation, camera) on your site.Malicious feature accessControlling access to features in iframes.
Public-Key-Pins (HPKP)Allows a site to specify which public key (or keys) are valid for it, aimed to prevent MITM attacksMan-in-the-Middle attacksEnforcing public key usage for client verification.
Expect-CTAllows web hosts to determine if they can enforce Certificate Transparency for their HTTPS certificates.Misissued or fraudulent certificatesEnsuring compliance with certificate transparency protocols.
Content-DispositionIndicates if content should be treated as an attachment or inline, helping protect against unwanted file types.Unintended file downloadsCustomize how files are handled and presented to users.

Certainly! Below is a structured table representing the HTTP security headers, including ASCII art examples of how you might format these in a tool like Postman.

HTTP Security Headers Examples in Postman

HTTP HeaderExample Request
Strict-Transport-Security (HSTS)POST /example HTTP/1.1
Host: example.com
Strict-Transport-Security: max-age=31536000; includeSubDomains
Content-Security-Policy (CSP)POST /example HTTP/1.1
Host: example.com
Content-Security-Policy: default-src ‘self’; script-src ‘none’;
Cross-Origin Resource Sharing (CORS)POST /example HTTP/1.1
Host: example.com
Access-Control-Allow-Origin: https://trusted.com
X-Frame-OptionsPOST /example HTTP/1.1
Host: example.com
X-Frame-Options: DENY
X-XSS-ProtectionPOST /example HTTP/1.1
Host: example.com
X-XSS-Protection: 1; mode=block
X-Content-Type-OptionsPOST /example HTTP/1.1
Host: example.com
X-Content-Type-Options: nosniff
Referrer-PolicyPOST /example HTTP/1.1
Host: example.com
Referrer-Policy: no-referrer
Permissions-PolicyPOST /example HTTP/1.1
Host: example.com
Permissions-Policy: geolocation=(self)
Public-Key-Pins (HPKP)POST /example HTTP/1.1
Host: example.com
Public-Key-Pins: pin-sha256=“base64-encoded-public-key”; max-age=5184000; includeSubDomains
Expect-CTPOST /example HTTP/1.1
Host: example.com
Expect-CT: max-age=86400; enforce
Content-DispositionPOST /example HTTP/1.1
Host: example.com
Content-Disposition: attachment; filename=“example.png”

Certainly! Below are the tables for HTTP security headers configuration for Apache and Nginx, now including a description for each header.

HTTP Security Headers Configuration for Apache

HeaderConfigurationDescription
X-Frame-OptionsHeader always set “X-Frame-Options: DENY”Prevents the page from being displayed in a frame or iframe, mitigating clickjacking attacks.
Header always set “X-Frame-Options: SAMEORIGIN”Allows the page to be displayed in a frame on the same origin only, protecting against framing.
X-XSS-ProtectionHeader always set “X-XSS-Protection: 1; mode=block”Enables the browser’s cross-site scripting (XSS) filter to block detected attacks.
X-Content-Type-OptionsHeader always set “X-Content-Type-Options: nosniff”Prevents the browser from MIME-sniffing the content type, helping to prevent attacks based on content type mismatches.
Content-Security-PolicyHeader set “Content-Security-Policy: default-src ‘self’”Specifies allowed sources for content to prevent XSS and data injection attacks.
Referrer-PolicyHeader always set “Referrer-Policy: no-referrer”Controls the amount of referrer information sent when navigating from your site, enhancing privacy.

HTTP Security Headers Configuration for Nginx

HeaderConfigurationDescription
X-Frame-Optionsadd_header X-Frame-Options “DENY”;Prevents the page from being displayed in a frame or iframe, mitigating clickjacking attacks.
add_header X-Frame-Options “SAMEORIGIN”;Allows the page to be displayed in a frame on the same origin only, protecting against framing.
X-XSS-Protectionadd_header X-XSS-Protection “1; mode=block”;Enables the browser’s cross-site scripting (XSS) filter to block detected attacks.
X-Content-Type-Optionsadd_header X-Content-Type-Options “nosniff”;Prevents the browser from MIME-sniffing the content type, helping to prevent attacks based on content type mismatches.
Content-Security-Policyadd_header Content-Security-Policy “default-src ‘self’;”;Specifies allowed sources for content to prevent XSS and data injection attacks.
Referrer-Policyadd_header Referrer-Policy “no-referrer”;Controls the amount of referrer information sent when navigating from your site, enhancing privacy.
Permissions-Policyadd_header Permissions-Policy “geolocation=(self)”;Specifies features that can be accessed by the web page, including geolocation, enhancing privacy.
Expect-CTadd_header Expect-CT “max-age=86400; enforce”;Instructs browsers to enforce Certificate Transparency, helping to detect misissued certificates.
Public-Key-Pins (HPKP)add_header Public-Key-Pins “pin-sha256="base64-encoded-public-key"; max-age=5184000; includeSubDomains”;Provides a mechanism to prevent certain types of MITM attacks by specifying expected public key hashes.
Cookie FlagDescriptionExample VulnerabilityExample Use-Case
SecureEnsures the cookie is only sent over HTTPS connections to prevent transmission over unsecured channels.Man-in-the-Middle (MitM) attacksProtecting session cookies during transit in secure applications.
HttpOnlyPrevents JavaScript access to cookies, reducing the risk of XSS attacks that could steal cookies.Cross-Site Scripting (XSS)Safeguarding authentication tokens from client-side scripts.
SameSiteControls whether a cookie is sent with cross-site requests, mitigating CSRF attacks.Cross-Site Request Forgery (CSRF)Restricting cookies to first-party contexts to enhance security.
DomainSpecifies which domains can access the cookie.Subdomain hijackingLimiting cookie access to specific subdomains to contain exposure.
PathDefines the URL path that must exist in the requested URL for the browser to send the cookie.Cookie leakage through unintended pathsScoping cookies to specific pages or directories of web applications.
Expires / Max-AgeDefines the duration for which the cookie will be valid, after which it’s automatically deleted.Session fixation attacksExpiring session cookies after a reasonable time period.
Samesite=StrictCookies are only sent in a first-party context; not sent along with cross-origin requests.Cross-Site Request Forgery (CSRF)Preventing cookies from being sent in cross-origin requests regardless of user interactions.
Samesite=LaxCookies are sent in a first-party context but are sent along with top-level navigation.Cross-Site Request Forgery (CSRF)Allowing cookies in navigational requests while blocking others.

Common Endpoints[S1]

EndpointDescription
/adminCommon admin panel access.
/loginLogin page for user authentication.
/logoutLogout functionality.
/apiBase URL for API calls.
/api/v1/Versioned API endpoint.
/api/v1/auth/loginAPI endpoint for user login.
/api/v1/auth/logoutAPI endpoint for user logout.
/settingsSettings page for user configurations.
/configConfiguration settings, often sensitive.
/statusServer status page (could reveal uptime).
/docsDocumentation endpoint for APIs.
/user/profileUser profile access.
/usersUser management or user listings.
/api/v1/users/{id}Fetch user details by ID.
/api/v1/productsList of products, could be sensitive.
/api/v1/ordersOrders management, may return sensitive data.
/account/view?id=123Viewing account details, can test ID changes.
/searchSearch functionality, can manipulate queries.
/registerUser registration page.
/password/resetPassword reset flows.
/uploadsFile upload endpoint, often critical for security.
/filesAccess to user-uploaded files or documents.
/reportsReporting tools access, might reveal internal metrics.
/notificationsUser notifications endpoint.
/helpHelp or support page.
/termsTerms of service and agreements.
/privacyPrivacy policy information.
/api/healthcheckHealthcheck endpoint for service availability.
/debugDebug page might expose application info.
/?debug=trueTesting for debug mode that reveals more data.
/?goto=...with different values to check for potential unlinked pages.

JWT

Cheet Sheet

Here’s an expanded JWT (JSON Web Token) cheat sheet table that includes examples for each aspect. This will help clarify how to implement and utilize JWTs in web applications.

AspectDetailsExamples
What is JWT?JWT is a compact, URL-safe means of representing claims to be transferred between two parties.- An authorization token for a user after login.
Structure of JWTJWT consists of three parts: <Header>.<Payload>.<Signature>.Example JWT: eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c
HeaderContains metadata about the token, such as the signing algorithm (e.g., HS256) and token type (JWT).{ "alg": "HS256", "typ": "JWT" }
PayloadContains the claims. Claims are statements about an entity (typically, the user) and additional data.{ "sub": "1234567890", "name": "John Doe", "iat": 1516239022 }
SignatureUsed to verify the sender of the JWT and to ensure that the message wasn’t changed. It is created using the header, payload, and a secret key.HMACSHA256(base64UrlEncode(header) + "." + base64UrlEncode(payload), secret)
Common Header Algorithms- HS256: HMAC using SHA-256.
- RS256: RSA Signature using SHA-256.
- ES256: ECDSA using P-256 and SHA-256.
- HS256: {"alg": "HS256", "typ": "JWT"}
- RS256: Use with a public/private key pair for signing and verification.
Common Claims- iss: Issuer.
- sub: Subject (the user the token represents).
- aud: Audience (intended recipients of the token).
- exp: Expiration time.
- nbf: Not before.
- iat: Issued at time.
- jti: JWT ID.
Example Claims:
{ "iss": "example.com", "sub": "user123", "aud": "example_app", "exp": 1633072800 }
Best Practices- Always use HTTPS to protect JWT in transit.
- Set short expiration times using the exp claim.
- Use secure storage for JWTs (e.g., HttpOnly cookies).
- Regularly rotate signing keys.
- Validate all claims when processing JWTs.
- Use audience and issuer validation to prevent token misuse.
- Implement token revocation strategies (e.g., blacklisting).
- Use exp to set a token’s expiration: { "exp": Math.floor(Date.now() / 1000) + (60 * 60) } (expires in 1 hour).
Common Pitfalls- Using weak signing algorithms (e.g., none is the default) can lead to security issues.
- Storing sensitive data in the payload (it’s easily decodable).
- Failing to validate the token properly, leading to unauthorized access.
- Ignoring proper error handling on token validation errors.
- Failure to validate: if (!token) { throw new Error('Token not provided'); } should be implemented.
Revocation Strategies- Store blacklisted tokens.
- Use short-lived tokens with refresh tokens.
- Maintain a mutable token store for invalidation.
- When a user logs out, add the token to a blacklist or mark it as revoked for a specific user session.
Token Storage- Local Storage: Can be accessed by JavaScript but is vulnerable to XSS attacks.
- Session Storage: Similar risks as local storage.
- HttpOnly Cookies: Not accessible via JavaScript, reducing XSS risks.
- Store token in HttpOnly cookie: Set-Cookie: jwt=your_jwt_token; HttpOnly; Secure; SameSite=Strict

Additional Considerations

  • Token Size: Limit payload size to improve performance, especially for mobile applications.
  • Libraries: Use established libraries for JWT handling (e.g., jsonwebtoken in Node.js, jwt in Python).
  • Audit Logs: Maintain logs of token usage for security and monitoring.

Token Structure

Sure! Below is an ASCII representation of the structure of a JSON Web Token (JWT) along with related components such as headers and claims. A JWT typically consists of three parts: Header, Payload, and Signature. In this representation, I’ll include the components of the JWT and some key flags that may be relevant.

+-------------------------------------------------------+
|                       JSON Web Token                  |
+-------------------------------------------------------+
|                       Header                           |
| -----------------------------------------------------  |
| {"alg": "HS256", "typ": "JWT"}                        |
| - alg: Algorithm used for signing (e.g., HS256)      |
| - typ: Type of the token (JWT)                         |
+-------------------------------------------------------+
|                       Payload                          |
| -----------------------------------------------------  |
| {"sub": "1234567890",                                 |
|  "name": "John Doe",                                   |
|  "iat": 1516239022,                                   |
|  "exp": 1516242622}                                   |
| - sub: Subject (e.g., user ID)                        |
| - name: User's name                                    |
| - iat: Issued at (timestamp for when token was issued)|
| - exp: Expiration time (timestamp)                    |
| - other custom claims can be added here as needed     |
+-------------------------------------------------------+
|                       Signature                        |
| -----------------------------------------------------  |
| HMACSHA256(                                          |
|   base64UrlEncode(header) + "." +                    |
|   base64UrlEncode(payload),                           |
|   your-256-bit-secret)                                |
| - Used to verify that the sender of the JWT is who it  |
|   claims to be and to ensure that the message wasn't   |
|   changed along the way.                               |
+-------------------------------------------------------+

Example JWT

Here’s an example JWT string that encapsulates the above components:

eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.\
eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.\
SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c

Explanation of the Example JWT

  1. Header:

    • alg: Indicates the algorithm used (HMAC SHA-256).
    • typ: Indicates the type of the token (JWT).
  2. Payload:

    • sub: Identifier for the subject of the token (user ID).
    • name: A claim that represents the user’s name.
    • iat: Indicates when the token was issued (in UNIX timestamp format).
  3. Signature:

    • Combines the base64Url encoded header and payload along with a secret key to produce a signature.
  • Authorization: Bearer tokens are typically sent in the Authorization header when making requests to protect resources.
  • exp: Expiration time of the token, limiting how long the token is valid.
  • nbf: Indicates that the token is not valid before a certain time.
  • iat: Issued at-time for the token.
  • jti: Unique identifier for the token.

Using JWTs allows for secure transmission of information between parties and can be verified and trusted since they are digitally signed.

Certainly! Below is the organized table with headers clearly delineating the Problem and Solution columns, along with a brief introduction for clarity.

JWT Security Issues and Solutions

ProblemExampleSolution
Signature Algorithm None AttackAn attacker modifies the JWT header to use alg: "none" and removes the signature, allowing the token to be accepted without verification.Always validate the alg field; do not accept unsigned tokens. Implement an allowlist of expected algorithms.
Token Expiration MismanagementA token is still accepted for an extended period, even after a user has logged out, potentially allowing unauthorized access.Implement a short expiration time (exp) and refresh tokens using proper token management strategies.
Replay AttacksAn attacker captures a valid token and reuses it to gain unauthorized access.Use nonce values or implement token binding to associate tokens with specific sessions or devices.
Token Storage VulnerabilitiesTokens stored in insecure places (e.g., localStorage) can be accessed by malicious scripts.Store tokens securely using HttpOnly and Secure cookies to prevent XSS and CSRF attacks.
Lack of Revocation MechanismIf a token is compromised, there is no way to revoke it before expiration.Maintain a blacklist or a revocation list of tokens, or alternatively, use short-lived tokens with refresh tokens.
Excessive Token SizeIncluding too many claims can lead to large token sizes, affecting performance and network efficiency.Limit claims to only what’s necessary for the application and avoid excess claims and metadata.
Weak Secret KeyUsing a weak or easily guessable secret key makes it easier for attackers to forge tokens.Use a strong, complex secret key. Employ key rotation policies and consider using asymmetric keys for added security.
Misconfigured Audience ClaimsAccepting a token issued for a different audience (aud) could lead to unauthorized access.Validate the aud claim properly and enforce that tokens are only accepted for the expected audience.
Insecure TransportSending JWTs over non-secure channels (i.e., HTTP instead of HTTPS) can lead to interception.Always use HTTPS for all communication involving JWTs to protect against man-in-the-middle attacks.
Lack of Claims ValidationFailing to validate required claims (e.g., iss, exp) results in accepting invalid tokens.Always validate claims upon token usage and ensure necessary claims are present and correct before processing.

Here’s a sorted comparison table of RSA (Rivest-Shamir-Adleman) and HMAC (Hash-based Message Authentication Code), along with other related cryptographic methods. The table highlights their characteristics, use cases, strengths, weaknesses, and a comparative analysis regarding their vulnerability to attacks.

Cryptographic MethodTypeKey LengthStrengthsWeaknessesUse CasesBetter for Attacking
RSAAsymmetric encryptionTypically 2048-4096 bitsHigh security with large key sizes, widely used for secure key exchangeSlower than symmetric; vulnerability to certain attacks if key size is smallSecure email, digital signatures, key exchangeModerate (depends on implementation)
HMACSymmetric authenticationBased on hash function (e.g., SHA-256)Fast, provides strong integrity and authenticity with a secret keyShared secret key must be protected; not suitable for non-repudiationAPI authentication, data integrity verificationLow (strong against attacks with proper key protection)
AESSymmetric encryption128, 192, or 256 bitsFast, secure, widely adopted, efficient with big dataRequires secure key management; vulnerable if key is weak or not stored properlyData encryption, VPNs, secure storageLow (if implemented correctly)
ECDSAAsymmetric digital signatureTypically 256, 384 bitsEfficient with shorter keys compared to RSA; strong securityComplex implementation; issues with certain curves have led to vulnerabilitiesDigital signatures, software distributionModerate (depends on key management)

In terms of which is better for attacking, both are designed to prevent unauthorized access and attacks. However, poorly implemented systems using RSA without proper key management practices could be more susceptible to attacks compared to a well-implemented HMAC system under proper conditions.