Skip to content

Instantly share code, notes, and snippets.

@Sanix-Darker
Created August 19, 2021 00:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Sanix-Darker/19d85eace69e6f312cc2009a6fdd3beb to your computer and use it in GitHub Desktop.
Save Sanix-Darker/19d85eace69e6f312cc2009a6fdd3beb to your computer and use it in GitHub Desktop.
kill sql injection
#/bin/python3
# by d4rk3r
from re import search, sub
def kill_injected_sql(input_string: str) -> str:
"""
A hard killer for sql injection from an incoming string.
params:
input_string : str [the incoming not sure string]
"""
# keys from an actual sql synthax
keys_synthax = [
"CREATE", "DROP", "UPDATE",
"INSERT", "ALTER", "DELETE",
"ATTACH", "DETACH"
]
# to manage with lowercase string too
keys_synthax += list(map(lambda x: x.lower(), keys_synthax))
# The regex to detect that
regex = f"^(?=.*SELECT.*FROM)|(?=.*(?:{'|'.join(keys_synthax)})).*$"
if search(regex, input_string):
# the patch
return sub("[^0-9a-zA-Z]+", "_", input_string)
return input_string
# Some examples
print(kill_injected_sql("SELECT * FROM TESTS"))
print(kill_injected_sql("create TABLE niangua (test not null)"))
# expected outputs
#
# SELECT_FROM_TESTS
# CREATE_TABLE_niangua_test_not_null_
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment