Search This Blog

Tuesday, June 22, 2021

API Security Testing - Cheat Sheet

REST evolved as Fielding wrote the HTTP/1.1 and URI specs and has been proven to be well-suited for developing distributed hypermedia applications. While REST is more widely applicable, it is most commonly used within the context of communicating with services via HTTP.

The key abstraction of information in REST is a resource. A REST API resource is identified by a URI, usually a HTTP URL. REST components use connectors to perform actions on a resource by using a representation to capture the current or intended state of the resource and transferring that representation.

The primary connector types are client and server, secondary connectors include cache, resolver and tunnel.

REST APIs are stateless. Stateful APIs do not adhere to the REST architectural style. State in the REST acronym refers to the state of the resource which the API accesses, not the state of a session within which the API is called. While there may be good reasons for building a stateful API, it is important to realize that managing sessions is complex and difficult to do securely.

Stateful services are out of scope of this Cheat Sheet: Passing state from client to backend, while making the service technically stateless, is an anti-pattern that should also be avoided as it is prone to replay and impersonation attacks.

In order to implement flows with REST APIs, resources are typically created, read, updated and deleted. For example, an ecommerce site may offer methods to create an empty shopping cart, to add items to the cart and to check out the cart. Each of these REST calls is stateless and the endpoint should check whether the caller is authorized to perform the requested operation.

Another key feature of REST applications is the use of standard HTTP verbs and error codes in the pursuit or removing unnecessary variation among different services.

Another key feature of REST applications is the use of HATEOAS or Hypermedia As The Engine of Application State. This provides REST applications a self-documenting nature making it easier for developers to interact with a REST service without prior knowledge.

HTTPS

Secure REST services must only provide HTTPS endpoints. This protects authentication credentials in transit, for example passwords, API keys or JSON Web Tokens. It also allows clients to authenticate the service and guarantees integrity of the transmitted data.

See the Transport Layer Protection Cheat Sheet for additional information.

Consider the use of mutually authenticated client-side certificates to provide additional protection for highly privileged web services.

Access Control

Non-public REST services must perform access control at each API endpoint. Web services in monolithic applications implement this by means of user authentication, authorisation logic and session management. This has several drawbacks for modern architectures which compose multiple microservices following the RESTful style.

  • in order to minimize latency and reduce coupling between services, the access control decision should be taken locally by REST endpoints
  • user authentication should be centralised in a Identity Provider (IdP), which issues access tokens

JWT

There seems to be a convergence towards using JSON Web Tokens (JWT) as the format for security tokens. JWTs are JSON data structures containing a set of claims that can be used for access control decisions. A cryptographic signature or message authentication code (MAC) can be used to protect the integrity of the JWT.

  • Ensure JWTs are integrity protected by either a signature or a MAC. Do not allow the unsecured JWTs: {"alg":"none"}.
  • In general, signatures should be preferred over MACs for integrity protection of JWTs.

If MACs are used for integrity protection, every service that is able to validate JWTs can also create new JWTs using the same key. This means that all services using the same key have to mutually trust each other. Another consequence of this is that a compromise of any service also compromises all other services sharing the same key. See here for additional information.

The relying party or token consumer validates a JWT by verifying its integrity and claims contained.

  • A relying party must verify the integrity of the JWT based on its own configuration or hard-coded logic. It must not rely on the information of the JWT header to select the verification algorithm. See here and here

Some claims have been standardised and should be present in JWT used for access controls. At least the following of the standard claims should be verified:

  • iss or issuer - is this a trusted issuer? Is it the expected owner of the signing key?
  • aud or audience - is the relying party in the target audience for this JWT?
  • exp or expiration time - is the current time before the end of the validity period of this token?
  • nbf or not before time - is the current time after the start of the validity period of this token?

As JWTs contain details of the authenticated entity (user etc.) a disconnect can occur between the JWT and the current state of the users session, for example, if the session is terminated earlier than the expiration time due to an explicit logout or an idle timeout. When an explicit session termination event occurs, a digest or hash of any associated JWTs should be submitted to a block list on the API which will invalidate that JWT for any requests until the expiration of the token. See the JSON_Web_Token_for_Java_Cheat_Sheet for further details.

API Keys

Public REST services without access control run the risk of being farmed leading to excessive bills for bandwidth or compute cycles. API keys can be used to mitigate this risk. They are also often used by organisation to monetize APIs; instead of blocking high-frequency calls, clients are given access in accordance to a purchased access plan.

API keys can reduce the impact of denial-of-service attacks. However, when they are issued to third-party clients, they are relatively easy to compromise.

  • Require API keys for every request to the protected endpoint.
  • Return 429 Too Many Requests HTTP response code if requests are coming in too quickly.
  • Revoke the API key if the client violates the usage agreement.
  • Do not rely exclusively on API keys to protect sensitive, critical or high-value resources.

Restrict HTTP methods

  • Apply an allow list of permitted HTTP Methods e.g. GETPOSTPUT.
  • Reject all requests not matching the allow list with HTTP response code 405 Method not allowed.
  • Make sure the caller is authorised to use the incoming HTTP method on the resource collection, action, and record

In Java EE in particular, this can be difficult to implement properly. See Bypassing Web Authentication and Authorization with HTTP Verb Tampering for an explanation of this common misconfiguration.

Input validation

  • Do not trust input parameters/objects.
  • Validate input: length / range / format and type.
  • Achieve an implicit input validation by using strong types like numbers, booleans, dates, times or fixed data ranges in API parameters.
  • Constrain string inputs with regexps.
  • Reject unexpected/illegal content.
  • Make use of validation/sanitation libraries or frameworks in your specific language.
  • Define an appropriate request size limit and reject requests exceeding the limit with HTTP response status 413 Request Entity Too Large.
  • Consider logging input validation failures. Assume that someone who is performing hundreds of failed input validations per second is up to no good.
  • Have a look at input validation cheat sheet for comprehensive explanation.
  • Use a secure parser for parsing the incoming messages. If you are using XML, make sure to use a parser that is not vulnerable to XXE and similar attacks.

Validate content types

A REST request or response body should match the intended content type in the header. Otherwise this could cause misinterpretation at the consumer/producer side and lead to code injection/execution.

  • Document all supported content types in your API.

Validate request content types

  • Reject requests containing unexpected or missing content type headers with HTTP response status 406 Unacceptable or 415 Unsupported Media Type.
  • For XML content types ensure appropriate XML parser hardening, see the XXE cheat sheet.
  • Avoid accidentally exposing unintended content types by explicitly defining content types e.g. Jersey (Java) @consumes("application/json"); @produces("application/json"). This avoids XXE-attack vectors for example.

Send safe response content types

It is common for REST services to allow multiple response types (e.g. application/xml or application/json, and the client specifies the preferred order of response types by the Accept header in the request.

  • Do NOT simply copy the Accept header to the Content-type header of the response.
  • Reject the request (ideally with a 406 Not Acceptable response) if the Accept header does not specifically contain one of the allowable types.

Services including script code (e.g. JavaScript) in their responses must be especially careful to defend against header injection attack.

  • Ensure sending intended content type headers in your response matching your body content e.g. application/json and not application/javascript.

Management endpoints

  • Avoid exposing management endpoints via Internet.
  • If management endpoints must be accessible via the Internet, make sure that users must use a strong authentication mechanism, e.g. multi-factor.
  • Expose management endpoints via different HTTP ports or hosts preferably on a different NIC and restricted subnet.
  • Restrict access to these endpoints by firewall rules or use of access control lists.

Error handling

  • Respond with generic error messages - avoid revealing details of the failure unnecessarily.
  • Do not pass technical details (e.g. call stacks or other internal hints) to the client.

Audit logs

  • Write audit logs before and after security related events.
  • Consider logging token validation errors in order to detect attacks.
  • Take care of log injection attacks by sanitising log data beforehand.

Security Headers

There are a number of security related headers that can be returned in the HTTP responses to instruct browsers to act in specific ways. However, some of these headers are intended to be used with HTML responses, and as such may provide little or no security benefits on an API that does not return HTML.

The following headers should be included in all API responses:

HeaderRationale
Cache-Control: no-storePrevent sensitive information from being cached.
Content-Security-Policy: frame-ancestors 'none'To protect against drag-and-drop style clickjacking attacks.
Content-TypeTo specify the content type of the response. This should be application/json for JSON responses.
Strict-Transport-SecurityTo require connections over HTTPS and to protect against spoofed certificates.
X-Content-Type-Options: nosniffTo prevent browsers from performing MIME sniffing, and inappropriately interpreting responses as HTML.
X-Frame-Options: DENYTo protect against drag-and-drop style clickjacking attacks.

The headers below are only intended to provide additional security when responses are rendered as HTML. As such, if the API will never return HTML in responses, then these headers may not be necessary. However, if there is any uncertainty about the function of the headers, or the types of information that the API returns (or may return in future), then it is recommended to include them as part of a defence-in-depth approach.

HeaderRationale
Content-Security-Policy: default-src 'none'The majority of CSP functionality only affects pages rendered as HTML.
Feature-Policy: 'none'Feature policies only affect pages rendered as HTML.
Referrer-Policy: no-referrerNon-HTML responses should not trigger additional requests.

CORS

Cross-Origin Resource Sharing (CORS) is a W3C standard to flexibly specify what cross-domain requests are permitted. By delivering appropriate CORS Headers your REST API signals to the browser which domains, AKA origins, are allowed to make JavaScript calls to the REST service.

  • Disable CORS headers if cross-domain calls are not supported/expected.
  • Be as specific as possible and as general as necessary when setting the origins of cross-domain calls.

Sensitive information in HTTP requests

RESTful web services should be careful to prevent leaking credentials. Passwords, security tokens, and API keys should not appear in the URL, as this can be captured in web server logs, which makes them intrinsically valuable.

  • In POST/PUT requests sensitive data should be transferred in the request body or request headers.
  • In GET requests sensitive data should be transferred in an HTTP Header.

OK:

https://example.com/resourceCollection/[ID]/action

https://twitter.com/vanderaj/lists

NOT OK:

https://example.com/controller/123/action?apiKey=a53f435643de32 because API Key is into the URL.

HTTP Return Code

HTTP defines status code. When designing REST API, don't just use 200 for success or 404 for error. Always use the semantically appropriate status code for the response.

Here is a non-exhaustive selection of security related REST API status codes. Use it to ensure you return the correct code.

CodeMessageDescription
200OKResponse to a successful REST API action. The HTTP method can be GET, POST, PUT, PATCH or DELETE.
201CreatedThe request has been fulfilled and resource created. A URI for the created resource is returned in the Location header.
202AcceptedThe request has been accepted for processing, but processing is not yet complete.
301Moved PermanentlyPermanent redirection.
304Not ModifiedCaching related response that returned when the client has the same copy of the resource as the server.
307Temporary RedirectTemporary redirection of resource.
400Bad RequestThe request is malformed, such as message body format error.
401UnauthorizedWrong or no authentication ID/password provided.
403ForbiddenIt's used when the authentication succeeded but authenticated user doesn't have permission to the request resource.
404Not FoundWhen a non-existent resource is requested.
405Method Not AcceptableThe error for an unexpected HTTP method. For example, the REST API is expecting HTTP GET, but HTTP PUT is used.
406UnacceptableThe client presented a content type in the Accept header which is not supported by the server API.
413Payload too largeUse it to signal that the request size exceeded the given limit e.g. regarding file uploads.
415Unsupported Media TypeThe requested content type is not supported by the REST service.
429Too Many RequestsThe error is used when there may be DOS attack detected or the request is rejected due to rate limiting.
500Internal Server ErrorAn unexpected condition prevented the server from fulfilling the request. Be aware that the response should not reveal internal information that helps an attacker, e.g. detailed error messages or stack traces.
501Not ImplementedThe REST service does not implement the requested operation yet.
503Service UnavailableThe REST service is temporarily unable to process the request. Used to inform the client it should retry at a later time.

Security Testing of the APIs

What place do APIs occupy in our lives today? According to statistics, on average, each big company uses about 420 services, and 83% of all traffic on the Internet today belongs to API-based services. With the arrival of 5G and especially the Internet of Things, we expect that traffic between API services and apps will only grow.

Today we live in a world of microservices, containers, and clouds. The days of monolithic architecture are over, meaning we must change the approach to security that was established many years ago.

Below are the detailed point which should be looked into in daily basis

1. Broken Object Level Authorization

The first vulnerability on our list is Broken Object Level Authorization.
Let’s say a user generates a document with ID=322. They should only be allowed access to that document. If you specify ID=109 or some other ID, the service should return the 403 (Forbidden) error.

In reality, everything is a little different. Often, not all services are checked for access controls. You can often change a resource ID and access a document with content meant for another user.

To test this issue, what parameters can you experiment with? You could pass any ID in the URL or as part of Query parameters or Body (in XML or JSON). Try changing them to see what the service returns to you.

Unfortunately, there are no automatic tools where you can press one magic button and get a detailed report. So far, Broken Object Level Authorization can only be tested manually. You can use the same tools with which you usually test APIs like PostmanFiddlerReadyAPI. So, send different requests and analyze the responses.

During the first round, you must define these cases manually. Later you can automate them as part of your normal functional testing.

2. Broken User Authentication

The second type of flaw is called Broken User Authentication. I hope you know the difference between authentication and authorization? All vulnerabilities that are related to authentication are usually associated with password management mechanisms and login mechanisms.

To avoid Broken User Authentication, passwords should be long (at least, say eight characters), including uppercase and lowercase letters, and so on. You also need to ensure it is impossible to bypass the login procedure and access objects or pages without being authenticated. In this case, the service should return 401 (Unauthorized). You must also ensure that a brute force attack cannot be run, as well as that the Forgot Password functionality does not return the password in clear text to you in an email, and so on.

When users log into an application, they are assigned a unique session token. Almost all vulnerabilities associated with sessions are related to this session token.

Here, you can test whether this session token gets reassigned after each successful login procedure or after the access level gets escalated in the application. In case your application removes or somehow changes the session token, check to see whether it returns a 401 error. We must not allow the possibility of predicting the session token for each next session. It should be as random as possible.

In addition to working with the login procedure and session tokens, it is also important to remember that APIs communicate with the client (UI) and other APIs. In these scenarios, it is common to use the client’s session token for further communication. However, developers do not always follow this practice. They often use special service accounts for API-to-API communication. Thus, the API may have access to more data than intended.

Unfortunately, this kind of vulnerability cannot be detected even if using black-box testing. Such vulnerabilities are only found during code review or architecture review.

Broken user security issues can also be associated with different approaches to authentication. There is basic authentication and claims-based authentication, and the application can implement Single Sign-on.

Each of these mechanisms has its own set of vulnerabilities and best practices. OWASP offers detailed checklists for each of them. Developers must ensure authentication mechanisms are correctly set and secured. Several automated tools may help you test the most common authentication patterns. For example, for basic authentication, security tools like Acunetix or Burp Suite can verify the token is encrypted and the hash is correct. Such tools will provide you with a basic report, which you then must analyze carefully.

3. Excessive Data Exposure

The next type of vulnerability is related to the fact that APIs can return more data than is displayed by the UI. That is, data filtering takes place on the user interface and not on the back end.

For example, you have an interface that displays three fields: First Name, Position, Email Address, and Photo. However, if you look at the API response, you may find more data, including some sensitive data like Birth Date or Home Address. The second type of Excessive Data Exposure occurs when UI data and API data are both returned correctly, but parameters are filtered on the front end and are not verified in any way on the back end. You may be able to specify in the request what data you need, but the back-end does not check whether you really have permission to access that data.

Again, you have to test for Excessive Data Exposure manually. Unfortunately, nothing but some ordinary tools and your skills will help you here. You need to analyze what data each API returns and see if it returns more data than necessary, and you must give unique scenarios the proper forethought.

For an example of Excessive Data Exposure, consider the vulnerability found in GitLab. They had an API called Events API that returned a lot of data in response while filtering on the UI. Less data was displayed on the UI, and more sensitive data could be accessed on the API.

4. Lack of Resources & Rate Limiting

The next vulnerability type is associated with insufficient resources and rate limits. Improper rate limiting is a type of vulnerability that occurs when an API has no limit on the number of requests it sends to another API or a server.

A simple strategy to control this is to establish that each API should not send more than N requests per second. However, this strategy is not quite correct. If your client generates more traffic than another client, your API should be stable for all clients.

This can be resolved using special status codes, for example, 429 (Too Many Requests). Using this status code, you can implement some form of Rate Limiting. There are also special proprietary headers. For example, GitHub uses its X-RateLimit-*. These headers help regulate how many requests the client can send during a specific unit of time.

The second scenario is related to the fact that you may not have enough parameter checks in the request. Suppose you have an application that returns a list of user types like size=10. What happens if an attacker changes this to 200000? Can the application cope with such a large request?

To find rate limiting vulnerabilities, you can use different fuzzing tools like JBroFuzz or Fuzzapi. Or, you can use the same tools with which you analyze traffic.

5. Broken Function Level Authorization

The next vulnerability is concerned with vertical levels of authorization —the user attempting to gain more access rights than allowed. For example, a regular user trying to become an admin. To find this vulnerability, you must first understand how various roles and objects in the application are connected. Secondly, you must clearly understand the access matrix implemented in the application.

For example, say you have a project related to advertising. Your application has different entities like products, users, channels, and so on. You also have five options to view the list of ads, create ads, delete ads, update ads, and get information about each specific ad. These functions require different access rights. Therefore, the application has a user, authenticated user, manager, and admin.

How can you find bugs and vulnerabilities in this situation? You could test sending a request to delete an ad while logged in as a regular user (instead of a manager) to see what the API returns. If the API returns 403 (Forbidden), then everything is fine. However, if the API returns two-hundredth codes like 200 (OK), 204 (No Content), or 201 (Created), then you have violated the access control matrix in some way.

A similar vulnerability was found in Steam several years ago when a potential attacker was able to pull out game activation keys. The researcher who found this vulnerability was experimenting and sending requests while logged in as a user. After experimenting with various parameters in the request field, he happened to use a “0” — suddenly, the Steam service exposed the entire list of game keys.

6. Mass Assignment

The next type of vulnerability is called Mass Assignment. Many modern development frameworks try to make developers’ lives easier by providing convenient mass assignment functions (when assigning parameters in bulk).

For example, say you have a piece of code where there is a User object that is assigned all parameters passed from the API or UI:


As you can see above, you can pass parameters email and password. However, you also have is_administrator, which, in theory, should be assigned automatically or somehow formed on the back end. If an attacker can perform an HTML injection and set the is_administrator: true, when this code is executed, it will be assigned together with the rest of the parameters.

The correct implementation of this would be to assign each parameter separately. That is, if you want to take only email and password, take only email and password and explicitly indicate this. Doing so does equate to many similar lines of code with different parameters, adding complexity for developers. Thankfully, new frameworks are simplifying this burden, thereby reducing this type of vulnerability.

7. Security Misconfiguration

The next type of vulnerability is related to the misconfiguration of your web servers or API. What can you test here? First of all, unnecessary HTTP methods must be disabled on the server. Do not show any unnecessary user errors at all. Do not pass technical details of the error to the client. If your application uses Cross-Origin Resource Sharing (CORS), that is, if it allows another application from a different domain to access your application’s cookies, then these headers must be appropriately configured to avoid additional vulnerabilities. Any access to internal files must also be disabled. There are special security headers, like Content-Security-Policy, that you can also implement in your applications to increase the security level.

8. Injections

In the OWASP top 10 web application security risks, injections take the first place; however, injections hold the eighth place for APIs. In my opinion, this is because modern frameworks, modern development methods, and architectural patterns block us from the most primitive SQL or XSS injections. For example, you can use the object-relational mapping model to avoid SQL injection. This does not mean that you need to forget about injections at all. Such problems are still possible throughout a huge number of old sites and systems. Besides XSS and SQL, you should look for XML injections, JSON injections, and so on.

You can test for injections using different tools. For example, ReadyAPI provides a paid tool for automatic scanning. Others, like Burp Suite, are partially free. Or, if you use Postman on a project, you could perform basic injection tests using Postman and data-driven testing.

9. Improper Assets Management

The next type of vulnerability is related to the mismanagement of your assets. This flaw is growing as engineers adopt DevOps, continuous testing, and CI/CD pipelines. From a security standpoint, it is essential to configure these CI/CD pipelines correctly.

CI/CD pipelines have access to various secrets and confidential data, such as accounts used to sign the code. Ensure you do not leave hard-coded secrets in the code and don’t “commit” them to the repository, no matter whether it is public or private.

When using virtual machines, containers may be created by the CI/CD pipeline, and microservices may be placed in a separate container. Throughout this process, make sure you don’t have old containers hanging around that everyone has forgotten about — these could easily become additional access points.

10. Insufficient Logging & Monitoring

The last type of vulnerability has to do with insufficient logging and monitoring procedures. The main idea here is that whatever happens to your application, you must be sure that you can track it. You should always have logs that show precisely what the attacker was trying to do. Also, have systems in place to identify suspicious traffic, and so on. Again, OWASP provides detailed checklists to reference to ensure your application is protected.

The DevSecOps Approach

All vulnerabilities discussed above represent the result of insufficient measures or measures that were taken too late. You could hire two, five, ten security experts, run your security test before each release, and hope you don’t find critical vulnerabilities that force you to re-architect, do global refactoring, and then run tons of regression tests. However, today, more and more companies are trying to move security (and all testing) to the left in the development lifecycle. When we start thinking about security from the very beginning of our development process, we call this DevSecOps.

With a DevSecOps mindset, we analyze functional requirements in terms of security. We set additional security requirements for our application. We review our deployment diagrams, architectures, any other diagrams in a 4+1 architectural view model. We model threats and build scenarios where we think about what types of vulnerabilities may appear in the architecture, infrastructure, or application, and we try to predict and prevent them. We scan the code even before it gets into the branch master. There are special static code analyzers that check our developers’ code and the code of third-party libraries. After that, you can do a security test using the API tools mentioned above.

Your engineers must carefully check all configurations of containers, clouds, CI/CD pipelines and avoid the API vulnerabilities mentioned above. It is necessary to conduct frequent training for the project team. It’s also recommended that, once a year, you involve external companies to perform penetration testing to ensure you didn’t miss anything.

All developers and testers should be aware of new vulnerabilities as they appear and regularly learn how to approach security correctly. DevSecOps is a difficult approach that requires a lot of time, resources, and knowledge, but this is the only way to protect your applications properly.

API Security Brings Unique Concerns

API testing requires a certain mindset. Testers should be prepared for the fact that they may not have a UI. They should be prepared to design different server requests from scratch and understand request and response headers. Documentation isn’t always up to date. Therefore, it’s always better to monitor real traffic to know how an application behaves. API testers have to constantly ask themselves questions like, “What if there are other versions of this API? Will the API differ, for example, for a mobile application and the web?” If testers are truly interested in protecting their APIs, they should cultivate this type of security mindset.



My Profile

My photo
can be reached at 09916017317