I want to prevent SQL injections by using regex to check the input a user gives to ensure they do not do anything malicious.
I am doing this in a python program -- I specifically need help with finding the regex terms that check if there is a ;
, /*
, or */
anywhere in the input string. I am asking this because I am curious how to do it for my own knowledge, even if this may not be the best way to go about preventing SQL injections.
I iterate over a dictionary and check to see if any terms meet the 'malicious term' criteria. This was the best way I could think of at the moment to prevent injections; admittedly I am not well versed in SQL and know of only the basic types of injections. If anyone thinks of other possible injections to be aware of it would be greatly appreciated.
Example:
import re
regex_terms = ["(D|d)(R|r)(O|o)(P|p).*(T|t)(A|a)(B|b)(L|l)(E|e)", "(I|i)(N|n)(S|s)(E|e)(R|r)(T|t).*(I|i)(N|n)(T|t)(O|o)"]
user_input = {"first name": "Bob",
"last name": "DROP TABLE",
"email": "; malicious statement here", # note the semicolon at the beginning -- I would want to be able to look for it anywhere in the string
"example key": "/* */",
"other example": "*/"}
for key in user_input:
for regex_term in regex_terms:
if re.search(regex_term, user_input):
print(f"Malicious Input: {user_input}")