In the previous Build Phase, you identified several vulnerabilities in your web application. You are now going to design and implement an AWS WAF ruleset to help mitigate these vulnerabilities. In this section you will do the following tasks:
Please ensure you are using the improved AWS WAF console and API experience for this workshop.
If needed, go to https://console.aws.amazon.com/console/home. You will be redirected to the AWS Management Console dashboard on successful login:
From the Management Console dashboard, navigate to the AWS WAF & Shield service console. You can do that several ways:
Viewing and Logging Requests: In the Overview tab for your Web ACL, you can view a sample of the requests that have been inspected by the WAF. For each sampled request, you can view detailed data about the request, such as the originating IP address and the headers included in the request. You also can view which rule the request matched, and whether the rule is configured to allow or block requests. You can refer to the sampled requests throughout this exercise to monitor activity and look for suspicious activity. Although not used in this workshop, in the Logging and metrics tab, you can enable full logging to get detailed information about traffic that is analyzed by your web ACL.
You use AWS WAF to control how an Amazon CloudFront distribution, an Amazon API Gateway API, or an Application Load Balancer responds to web requests.
Web ACLs – You use a web access control list (ACL) to protect a set of AWS resources. You create a web ACL and define its protection strategy by adding rules. Rules define criteria for inspecting web requests and specify how to handle requests that match the criteria. You set a default action for the web ACL that indicates whether to block or allow through those requests that pass the rules inspections.
Rules – Each rule contains a statement that defines the inspection criteria, and an action to take if a web request meets the criteria. When a web request meets the criteria, that's a match. You can use rules to block matching requests or to allow matching requests through. You can also use rules just to count matching requests.
Rules groups – You can use rules individually or in reusable rule groups. AWS Managed Rules and AWS Marketplace sellers provide managed rule groups for your use. You can also define your own rule groups.
Rule statements are the part of a rule that tells AWS WAF how to inspect a web request. When AWS WAF finds the inspection criteria in a web request, we say that the web request matches the statement. Every rule statement specifies what to look for and how, according to the statement type.
Every rule in AWS WAF has a single top-level rule statement, which can contain other statements. Rule statements can be very simple. For example, you could have a statement that provides just a set of originating countries to check your web requests for. Rule statements can also be very complex. For example, you could have a statement that combines many other statements with logical AND, OR, and NOT statements.
After you create your web ACL, you can associate it with one or more AWS resources. The resource types that you can protect using AWS WAF web ACLs are Amazon CloudFront distributions, Amazon API Gateway APIs, and Application Load Balancers.
In order to illustrate the process of creating WAF rules, we will walk through the creation of the first rule in your WAF ACL.
Use the following guiding questions when planning WAF rules:
In the AWS WAF console, create a SQL injection rule by clicking the Web ACL, Add rules, Add my own rules and rule groups
Click on Rule builder, provide matchSQLi for the Name and keep Regular rule for Type:
Click on Add Rule, select Save on the Set rule priority page.
For a more comprehensive discussion of common vulnerabilities for web applications, as well as how to mitigate them using AWS WAF, and other AWS services, please refer to the Use AWS WAF to Mitigate OWASP’s Top 10 Web Application Vulnerabilities whitepaper.
In this phase, we will have a set of 6 exercises walking you through the process of building a basic mitigation rule set for common vulnerabilities. We will build these rules from scratch, so you can gain familiarity with the AWS WAF programming model and you can then write rules specific to your applications.
For the exercises below, you will find the high level description and solution configuration for your web ACL. You can test your ACL ruleset at any time using the Red Team Host. For AWS sponsored event, you can also view test results on the WAF Lab Dashboard.
Use the SQL injection, cross-site scripting, as well as string and regex matching to build rules that mitigate injection attacks and cross site scripting attacks.
Consider the following:
How do the requirements derived from the above questions affect your solution?
runscanner
) from your red team host to confirm requests are blockedThus far you have used the Visual Rule Editor to create WAF Rules but every web ACL also has a JSON format representation. In there, you see these special types of rule statements. For rules of any complexity, managing your web ACL using the JSON editor is the easiest way to go. You can retrieve the complete configuration for a web ACL in JSON format, modify it as you need, and then provide it through the console, API, or CLI. AWS WAF supports nesting of rule statements. To combine rule statement results, you nest the statements under logical AND or OR rule statements. The visual editor on the console supports one level of rule statement nesting, which works for many needs. To nest more levels, you can edit the JSON representation of the rule on the console.
Create a new rule named matchXSS and for If a request choose matches at least one of the statements (OR). Add statements:
Edit the rule, click the Rule JSON editor and note the structure and syntax of the rule logic.
Add an exception statement for the XSS rule to allow access to /reportBuilder/Editor.aspx. Note that we are using the JSON editor here due to the nested logic required for the exception.
{
"Name": "matchXSS",
"Priority": 2,
"Action": {
"Block": {}
},
"VisibilityConfig": {
"SampledRequestsEnabled": true,
"CloudWatchMetricsEnabled": true,
"MetricName": "matchXSS"
},
"Statement": {
"AndStatement": {
"Statements": [{
"NotStatement": {
"Statement": {
"ByteMatchStatement": {
"SearchString": "/reportBuilder/Editor.aspx",
"FieldToMatch": {
"UriPath": {}
},
"TextTransformations": [{
"Priority": 0,
"Type": "NONE"
}],
"PositionalConstraint": "STARTS_WITH"
}
}
}
},
{
"OrStatement": {
"Statements": [{
"XssMatchStatement": {
"FieldToMatch": {
"QueryString": {}
},
"TextTransformations": [{
"Priority": 0,
"Type": "URL_DECODE"
}]
}
},
{
"XssMatchStatement": {
"FieldToMatch": {
"Body": {}
},
"TextTransformations": [{
"Priority": 0,
"Type": "HTML_ENTITY_DECODE"
},
{
"Priority": 1,
"Type": "URL_DECODE"
}
]
}
},
{
"XssMatchStatement": {
"FieldToMatch": {
"SingleHeader": {
"Name": "cookie"
}
},
"TextTransformations": [{
"Priority": 0,
"Type": "URL_DECODE"
}]
}
}
]
}
}
]
}
}
}
Click Save rule
Re-run the WAF test script (runscanner) from your red team host to confirm requests are blocked
If your Systems Manager Session Manager session times out, create a new session.
Use the string and regex matching to build rules that block specific patterns indicative of unwanted path traversal or file inclusion.
Build rules that ensure the relevant HTTP request components used for input into paths do not contain known path traversal patterns.
/include
, url_decode../
, url_decode://
, url_decodeUse the string and regex matching, size constraints and IP address matching to build rules that block non-conforming or low value HTTP requests.
Are there limits to the size of the various HTTP request components relevant to your web application? For example, does your application ever use URIs that are longer than 100 characters in size?
Are there specific HTTP request components without which your application cannot operate effectively (e.g. CSRF token header, authorization header, referrer header)?
Build rules that ensure the requests your application ends up processing are valid, conforming and valuable.
In the left pane, choose Regex pattern sets, Create regex pattern set
csrf
, Regular expressions \^\[0-9a-f\]{40}\$
Create a new rule and choose Rule JSON editor
Delete any existing text and paste the following JSON below. Review the statements in the JSON
Update the region, AWS account Id and Regex pattern ID with the one created in the previous step
Nested Statement with Request Hygiene Solution
{
"Name": "matchCSRF",
"Priority": 3,
"Action": {
"Block": {}
},
"VisibilityConfig": {
"SampledRequestsEnabled": true,
"CloudWatchMetricsEnabled": true,
"MetricName": "matchCSRF"
},
"Statement": {
"AndStatement": {
"Statements": [{
"NotStatement": {
"Statement": {
"RegexPatternSetReferenceStatement": {
"ARN": "arn:aws:wafv2:YOUR_REGION:ACCOUNT_ID:regional/regexpatternset/csrf/YOUR_REGEX_PATTERN_ID",
"FieldToMatch": {
"SingleHeader": {
"Name": "x-csrf-token"
}
},
"TextTransformations": [{
"Priority": 0,
"Type": "URL_DECODE"
}]
}
}
}
},
{
"OrStatement": {
"Statements": [{
"ByteMatchStatement": {
"SearchString": "/form.php",
"FieldToMatch": {
"UriPath": {}
},
"TextTransformations": [{
"Priority": 0,
"Type": "NONE"
}],
"PositionalConstraint": "STARTS_WITH"
}
},
{
"ByteMatchStatement": {
"SearchString": "/form.php",
"FieldToMatch": {
"UriPath": {}
},
"TextTransformations": [{
"Priority": 0,
"Type": "NONE"
}],
"PositionalConstraint": "EXACTLY"
}
}
]
}
}
]
}
}
}
Click Save rule and then select Save on the Set rule priority page.
Re-run the WAF test script (runscanner) from your red team host to confirm requests are blocked
The remaining exercises below are optional. You should proceed to the Verify Phase and come back to the content below if time permits.
Use geo matching to build rules that limit the attack footprint against the exposed components of your application.
Consider the following:
Does your web application have server-side include components in the public web path?
Does your web application have components at exposed paths that are not used (or dependencies have such functions)?
Do you have administrative, management, status or health check paths and components that aren’t meant for end user access?
You should consider blocking access to such elements, or limiting access to known sources, either whitelisted IP addresses or geographic locations.
Create a new rule and choose Rule JSON editor
Nested Statement with Limit Attack Footprint Solution
{
"Name": "matchAdminNotAffiliate",
"Priority": 4,
"Action": {
"Block": {}
},
"VisibilityConfig": {
"SampledRequestsEnabled": true,
"CloudWatchMetricsEnabled": true,
"MetricName": "matchAdminNotAffiliate"
},
"Statement": {
"AndStatement": {
"Statements": [{
"NotStatement": {
"Statement": {
"GeoMatchStatement": {
"CountryCodes": [
"US",
"RO"
]
}
}
}
},
{
"ByteMatchStatement": {
"FieldToMatch": {
"UriPath": {}
},
"PositionalConstraint": "STARTS_WITH",
"SearchString": "/admin",
"TextTransformations": [{
"Type": "NONE",
"Priority": 0
}]
}
}
]
}
}
}
What constitutes an anomaly in regards to your web application? A few common anomaly patterns are:
Do you have mechanisms in place to detect such patterns? If so, can you build rules to mitigate them?
Reputation lists (whitelists or blacklists) are a good way to filter and stop servicing low value requests. This can reduce operating costs, and reduce exposure to attack vectors. Reputation lists can be self-maintained: lists of identifiable actors that you have determined are undesired. They can be identified any number of ways:
Build blacklists of such actors using the relevant statements and set up rules to match and block them. An example IP-based blacklist already exists in your sandbox environment.
Reputation lists can also be maintained by third parties. The AWS WAF Security Automations allow you to implement IP-based reputation lists.
You can now proceed to the Verify Phase.
This video has no audio