Skip to content
This repository has been archived by the owner on Jul 25, 2024. It is now read-only.

Commit

Permalink
Update app.py
Browse files Browse the repository at this point in the history
  • Loading branch information
arbs09 committed Mar 19, 2024
1 parent 857568b commit b85111a
Showing 1 changed file with 14 additions and 0 deletions.
14 changes: 14 additions & 0 deletions app.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
ABUSEIPDB_API_KEY = os.environ.get('API_KEY', 'default_key')
REPORT_INTERVAL = timedelta(minutes=15)
reported_ips = {}
MALICIOUS_USER_AGENTS = ["Go-http-client", "python", "sqlmap", "Nmap Scripting Engine", "pycurl"]

def report_ip(ip, categories, comment):
url = 'https://api.abuseipdb.com/api/v2/report'
Expand Down Expand Up @@ -82,6 +83,19 @@ def check_path():
reported_ips[ip] = datetime.now()
return '404'

@app.before_request
def check_user_agent():
user_agent = request.headers.get("User-Agent")
if user_agent:
for malicious_agent in MALICIOUS_USER_AGENTS:
if malicious_agent in user_agent:
ip = request.client_ip
if ip not in reported_ips or datetime.now() - reported_ips[ip] > REPORT_INTERVAL:
save_to_file(ip)
report_ip(ip, '18,19,21,15', f'Automated report for using malicious user-agent: {user_agent}')
reported_ips[ip] = datetime.now()
break # Exit the loop once a match is found

@app.route('/<path:filename>')
def report_rules(filename):
ip = request.client_ip
Expand Down

0 comments on commit b85111a

Please sign in to comment.