Skip to content

Commit 9f84336

Browse files
committed
block fake real ip
1 parent 046c5dc commit 9f84336

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

proxy/nginx-malicious.conf

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ if ($http_x_amzn_waf_detection = "datacenter") {
88
return 429;
99
}
1010

11+
if ($invalid_real_ip = 1) {
12+
return 429;
13+
}
14+
1115
error_page 429 @malicious;
1216

1317
location @malicious {

proxy/nginx.conf

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,31 @@ http {
2626
{{env "EXTERNAL_ROUTE_ADMIN"}} {{env "INTERNAL_ROUTE_ADMIN"}};
2727
}
2828

29+
# Extract first IP from X-Forwarded-For header
30+
map $http_x_forwarded_for $first_forwarded_ip {
31+
~^([^,\s]+) $1;
32+
default "";
33+
}
34+
# Check if first IP is invalid real ip
35+
map $first_forwarded_ip $invalid_real_ip {
36+
# RFC 1918 Private IP ranges
37+
~^10\. 1; # 10.0.0.0/8
38+
~^172\.(1[6-9]|2[0-9]|3[01])\. 1; # 172.16.0.0/12
39+
~^192\.168\. 1; # 192.168.0.0/16
40+
41+
# RFC 3927 Link-local addresses
42+
~^169\.254\. 1; # 169.254.0.0/16
43+
44+
# RFC 5735 Loopback
45+
~^127\. 1; # 127.0.0.0/8
46+
47+
# RFC 1122 Current network (only valid as source)
48+
~^0\.0\.0\.0$ 1; # 0.0.0.0/32
49+
50+
# Default case - public IP
51+
default 0;
52+
}
53+
2954
server {
3055
listen {{port}};
3156
root public;

0 commit comments

Comments
 (0)