Skip to content
This repository has been archived by the owner on Aug 10, 2022. It is now read-only.

Latest commit

 

History

History

Lab 3

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 

Security : Threat Protection

Duration : 15 mins

Persona : API Team / Security

Use case

You have an existing Apigee API proxy that takes requests from the internet and forwards them to an existing service. You have a requirement to ensure the integrity of the API message content, by protecting against threats such as JSON/XML/SQL injection and other malicious payload manipulation.

How can Apigee Edge help?

Message content is a significant attack vector used by malicious actors. Apigee Edge provides a set of out-of-the-box policies that help mitigate the potential for your backend services to be compromised by attackers or by malformed request payloads.

In this lab we will see how to use the following policies:

  • JSON Threat Protection policy
  • Regular Expression Protection policy

Prerequisites

Instructions

JSON Threat Protection

  1. For this lab we will be using a mock target API. An initial Apigee API proxy has been created for you. Download the API proxy here.

  2. Go to https://apigee.com/edge and log in. This is the Edge management UI.

  3. Select Develop → API Proxies in the side navigation menu:

image alt text

  1. Click the +Proxy button on the top-right corner to invoke the Create Proxy wizard:

image alt text

  1. Select Upload Proxy Bundle and then click Next to import an existing proxy from a zip archive:

image alt text

  1. Click on Choose File and select the Mock-Target-API.zip that was previously downloaded and click Next.

image alt text

  1. Click on Create to upload the the proxy.

image alt text

  1. Confirm that the proxy was uploaded successfully and click on Edit Proxy:

image alt text

  1. On the Proxy Overview page, click the Deployment drop down, and select the test environment. Click Deploy in the confirmation pop-up. Then click on the Develop tab:

image alt text

  1. Click on the "Send request and view request headers and body" flow under Proxy Endpoints → default, and then click on +Step on the upper right of the Request flow to attach a JSON Threat Protection policy:

image alt text

  1. Select JSON Threat Protection policy under Security. Click on the Add button to add the policy to the selected flow's request pipeline:

image alt text

  1. Select the policy to display the policy's XML configuration in the editor:

image alt text

  1. Change the policy's XML configuration to the below snippet to enforce protection against JSON payload manipulation threats:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<JSONThreatProtection async="false" continueOnError="false" enabled="true" name="JSON-Threat-Protection-1">
    <DisplayName>JSON Threat Protection-1</DisplayName>
    <Properties/>
    <ObjectEntryCount>5</ObjectEntryCount>
    <Source>request</Source>
</JSONThreatProtection>

In the above example, we use the JSON Threat Protection policy to ensure that the incoming API request JSON payload does not contain more than 5 fields. If the incoming payload contains more than 5 fields, the API proxy returns an error response. For a full list of JSON integrity checks that can be performed using this policy, see the JSON Threat Protection policy documentation.

  1. Click on Save to save the API Proxy changes:

image alt text

Test JSON Threat Protection:

  1. To test the changes made, first click on Trace tab of the API proxy dashboard:

image alt text

  1. Click on Start Trace Session button to begin tracing:

image alt text

  1. Now, send a POST request to your API endpoint at http://{{your-organization}}-{{your-environment}}.apigee.net/mock-target-api/echo with the following format:
POST /mock-target-api/echo HTTP/1.1
Host: {{your org}}-{{your env}}.apigee.net
Content-Type: application/json

{
"field1": "test_value1",
"field2": "test_value2",
"field3": "test_value3",
"field4": "test_value4",
"field5": "test_value5",
"field6": "test_value6"
}

You can make this call either using a REST client like the one here, or using a terminal.

Example curl command:

curl -X POST "http://{{your-org}}-{{your-env}}.apigee.net/mock-target-api/echo" -H "Content-Type: application/json" -d '{"field1": "test_value1", "field2": "test_value2", "field3": "test_value3", "field4": "test_value4", "field5": "test_value5", "field6": "test_value6"}'
  • Note: If you are using a REST client, make sure that your HTTP request has a Header name/value pair of Content-Type: application/json as shown below

image alt text

  1. The response received will be an error, since we attempted to send more than 5 fields in the POST request payload.

image alt text

On the Trace screen we also see that the JSON Threat Protection policy was triggered to return this error response:

image alt text

  1. You can now test for a successful API call, by sending the API endpoint a similar POST request, but this time with 5 or fewer fields in the JSON payload.
POST /mock-target-api/echo HTTP/1.1
Host: {{your-org}}-{{your-env}}.apigee.net
Content-Type: application/json

{
"field1": "test_value1",
"field2": "test_value2",
"field3": "test_value3",
"field4": "test_value4",
"field5": "test_value5"
}

Again, you can make this call either using a REST client like the one here, or using a terminal.

Example curl command:

curl -X POST "http://{{your-org}}-{{your-env}}.apigee.net/mock-target-api/echo" -H "Content-Type: application/json" -d '{"field1": "test_value1", "field2": "test_value2", "field3": "test_value3", "field4": "test_value4", "field5": "test_value5"}'
  1. The response received will be a successful one, since we attempted to send fewer fields in the POST request payload:

image alt text

On the Trace screen we also see that the JSON Threat Protection policy allowed the request to go through and hit the API target:

image alt text

Regular Expression Protection

Add Protection Against SQL Injection Attacks

  1. Click on the "View IP address" flow under Proxy Endpoints → default. Click on +Step on the upper right of the Request flow and attach a Regular Expression Protection policy.

image alt text

  1. Select Regular Expression Protection policy. Click on Add button to add the policy to the selected flow's request pipeline.

image alt text

  1. Select the policy to display the policy's XML configuration in the editor.

image alt text

  1. Change the policy's XML configuration to the below snippet to protect against SQL injections.
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<RegularExpressionProtection async="false" continueOnError="false" enabled="true" name="Regular-Expression-Protection-1">
    <Source>request</Source>
    <QueryParam name="query">
        <Pattern>[\s]*(?i)((delete)|(exec)|(drop\s*table)|(insert)|(shutdown)|(update)|(\bor\b))</Pattern>
    </QueryParam>
</RegularExpressionProtection>

In the above example, the Regular Expression Protection policy has been configured with a pattern that matches common SQL injection attacks. This pattern will be checked against the value of the query parameter named query, and if there is a match, the policy will return an error response. Note that the policy lets you check the pattern against all types of input parameters and body content.

For other sample patterns, reference the Regular Expression Protection policy documentation.

  1. Click on Save to save the API Proxy changes.

image alt text

Test Regular Expression Protection:

  1. To test the changes made, first click on Trace tab of the API proxy dashboard, and click on Start Trace Session button.

image alt text

  1. Now, send a GET request to the API endpoint at http://{{your-organization}}-{{your-environment}}.apigee.net/mock-target-api/ip?query= with any of the following entries in the query parameter. Try out all of the entries, and see if you can determine what each attack is trying to do!
query=delete
query=password’ OR 1=1
query=5; DROP TABLE USERS;

You can make this call either using a REST client like the one here, or using a terminal.

Example curl command:

curl "http://{{your-org}}-{{your-env}}.apigee.net/mock-target-api/ip?query={{insert SQL injection attack here}}"

The response received will be an error, since we attempted to send a malicious attack that we have configured our policy to recognize:

image alt text

We can also confirm from the Trace screen that the Regular Expression Protection policy was triggered to return this error response:

image alt text

Lab Video

If you like to learn by watching, here are some short 4 minute videos on using the policies explained above:

Earn Extra points

Now that you have tried the JSON and Regular Expression Threat Protection policies, try out the XML Threat Protection policy that helps you check the API payload content integrity in the case of XML payloads.

Summary

That completes this hands-on lesson. In this simple lab you learned how to protect your APIs against payload content based threats.

References

You may now proceed to Lab 4