77from django .urls import reverse
88from django .conf import settings
99from django .middleware .locale import LocaleMiddleware
10+ from django .http import HttpResponseNotFound
1011from django .utils import translation
12+ from django .shortcuts import render
13+
1114logger = logging .getLogger ('admin_logout_logger' )
15+ Honeypot_logger = logging .getLogger ("honeypot_logger" )
1216
1317class AutoLogoutMiddleware (MiddlewareMixin ):
1418 """
@@ -149,16 +153,46 @@ def process_request(self, request):
149153 request .LANGUAGE_CODE = translation .get_language ()
150154
151155
156+
152157class SecurityHeadersMiddleware :
153158 """
154159 Middleware to add security headers that help mitigate vulnerabilities
155160 in third-party JavaScript libraries and prevent common attacks.
156161 """
157-
162+
163+ # Honeypot Middleware
164+
165+ class HoneypotMiddleware :
166+ """
167+ Middleware to detect and log requests to honeypot (fake) paths.
168+ Normal users will never visit these paths.
169+ If triggered, log the attempt and return a 404 page.
170+ """
171+
172+ # Fake paths attackers often try
173+ HONEYPOT_PATHS = [
174+ "/admin-secret" ,
175+ "/Main-admin" ,
176+ "/superuser" ,
177+ "/dashboard-old" ,
178+ "/backup.sql" ,
179+ "/db.dump" ,
180+ "/config.php" ,
181+ "/env.bak" ,
182+ "/administration-login" ,
183+ "/auth-test" ,
184+ "/secure-login" ,
185+ "/test-page" ,
186+ "/debug/" ,
187+ "/old-site/"
188+ ]
189+
190+
158191 def __init__ (self , get_response ):
159192 self .get_response = get_response
160193
161194 def __call__ (self , request ):
195+
162196 response = self .get_response (request )
163197
164198 # Content Security Policy to prevent XSS and code injection
@@ -197,4 +231,31 @@ def __call__(self, request):
197231 'gyroscope=()'
198232 )
199233
200- return response
234+ return response
235+
236+ path = request .path
237+
238+ # Check if the request path is in honeypot traps
239+ if path in self .HONEYPOT_PATHS :
240+ ip = self .get_client_ip (request )
241+ ua = request .META .get ("HTTP_USER_AGENT" , "unknown" )
242+
243+ Honeypot_logger .warning (
244+ f"HONEYPOT TRIGGERED: Path={ path } , IP={ ip } , User-Agent={ ua } "
245+ )
246+
247+ # Return fake 404 so attacker doesn't know it’s a trap
248+ return render (request , "404.html" , status = 404 )
249+
250+ # Otherwise continue normally
251+ return self .get_response (request )
252+
253+ def get_client_ip (self , request ):
254+ """Extract client IP address from headers"""
255+ x_forwarded_for = request .META .get ("HTTP_X_FORWARDED_FOR" )
256+ if x_forwarded_for :
257+ ip = x_forwarded_for .split ("," )[0 ]
258+ else :
259+ ip = request .META .get ("REMOTE_ADDR" , "" )
260+ return ip
261+
0 commit comments