2021-06-07 16:07:08 +02:00
#!/usr/bin/env python3
# -*-coding:UTF-8 -*
"""
The SQLInjectionDetection Module
== == == == == == == == == == == == == == == ==
This module is consuming the Redis - list created by the Urls module .
It test different possibility to makes some sqlInjection .
"""
import os
import sys
import re
import urllib . request
2024-12-09 13:54:54 +01:00
# from pyfaup.faup import Faup
2022-10-25 16:25:19 +02:00
from urllib . parse import unquote
2021-06-07 16:07:08 +02:00
sys . path . append ( os . environ [ ' AIL_BIN ' ] )
##################################
# Import Project packages
##################################
from modules . abstract_module import AbstractModule
2024-12-09 13:54:54 +01:00
# from lib.ConfigLoader import ConfigLoader
2023-03-30 15:23:41 +02:00
# from lib import Statistics
2021-06-07 16:07:08 +02:00
class SQLInjectionDetection ( AbstractModule ) :
""" docstring for SQLInjectionDetection module. """
# # TODO: IMPROVE ME
# Reference: https://github.com/stamparm/maltrail/blob/master/core/settings.py
SQLI_REGEX = r " information_schema|sysdatabases|sysusers|floor \ (rand \ (|ORDER BY \ d+| \ bUNION \ s+(ALL \ s+)?SELECT \ b| \ b(UPDATEXML|EXTRACTVALUE) \ (| \ bCASE[^ \ w]+WHEN.*THEN \ b| \ bWAITFOR[^ \ w]+DELAY \ b| \ bCONVERT \ (|VARCHAR \ (| \ bCOUNT \ ( \ * \ )| \ b(pg_)?sleep \ (| \ bSELECT \ b.* \ bFROM \ b.* \ b(WHERE|GROUP|ORDER) \ b| \ bSELECT \ w+ FROM \ w+| \ b(AND|OR|SELECT) \ b.*/ \ *.* \ */|/ \ *.* \ */.* \ b(AND|OR|SELECT) \ b| \ b(AND|OR)[^ \ w]+ \ d+[ ' \" ) ]?[=><][ ' \" ( ]? \ d+|ODBC;DRIVER| \ bINTO \ s+(OUT|DUMP)FILE "
def __init__ ( self ) :
super ( SQLInjectionDetection , self ) . __init__ ( )
2024-12-09 13:54:54 +01:00
# self.faup = Faup()
2021-06-07 16:07:08 +02:00
2023-05-12 15:29:53 +02:00
self . logger . info ( f " Module: { self . module_name } Launched " )
2021-06-07 16:07:08 +02:00
def compute ( self , message ) :
2023-06-22 15:38:04 +02:00
url = message
2021-06-07 16:07:08 +02:00
if self . is_sql_injection ( url ) :
2024-12-09 13:54:54 +01:00
# self.faup.decode(url)
# url_parsed = self.faup.get()
2021-06-07 16:07:08 +02:00
2024-12-09 13:54:54 +01:00
print ( f " Detected SQL in URL: { self . obj . get_global_id ( ) } " )
2021-06-07 16:07:08 +02:00
print ( urllib . request . unquote ( url ) )
# Tag
2024-03-08 13:54:14 +01:00
tag = f ' infoleak:automatic-detection= " sql-injection " '
2023-06-22 15:38:04 +02:00
self . add_message_to_queue ( message = tag , queue = ' Tags ' )
2021-06-07 16:07:08 +02:00
# statistics
2023-03-30 15:23:41 +02:00
# tld = url_parsed['tld']
# if tld is not None:
# # # TODO: # FIXME: remove me
# try:
# tld = tld.decode()
# except:
# pass
# date = datetime.now().strftime("%Y%m")
# Statistics.add_module_tld_stats_by_date(self.module_name, date, tld, 1)
2021-06-07 16:07:08 +02:00
2022-10-25 16:25:19 +02:00
# Try to detect if the url passed might be an sql injection by applying the regex
2021-06-07 16:07:08 +02:00
# defined above on it.
def is_sql_injection ( self , url_parsed ) :
2022-10-25 16:25:19 +02:00
line = unquote ( url_parsed )
2021-06-07 16:07:08 +02:00
return re . search ( SQLInjectionDetection . SQLI_REGEX , line , re . I ) is not None
if __name__ == " __main__ " :
module = SQLInjectionDetection ( )
module . run ( )