Basically, I'm trying to find ways to explore SQL Injection vulnerabilities in the code presented below. The code has another layer, presentation, where it asks the user for board_name input. Table_name is an inside variable, user doesn't have control over it.
def find_board_by_name(cls, connector, board_name, table_name: str):
query = "SELECT * FROM {} WHERE name = '{}'".format(table_name,board_name.title())
print(query)
try:
connector.mycursor.execute(query)
res = connector.mycursor.fetchone()
if res:
return res[1]
return None
except Exception as e:
raise e
Tried to :
1- board_name as: ' OR '1'='1
It worked, program returned to me me first item on the table (it wasn't supposed to, since I didn't provide any name)
2- Tried to put board_name as: ';Delete Table BoardGames; It didn't work, mysql.connector.errors.InterfaceError: Use multi=True when executing multiple statements was presented to me.
So my question is " mysql.connector" is protecting me against the majorities of the SQL Injections? Since I didn't set the "multi" tag in the code to True? Could you show me other ways to explore vulnerabilities in this code? Besides the 1 and 2.
I guess I know how to fix the SQL Injection problem: "connector.mycursor.execute(query, values)" probably will do the job. But, I'm trying to understand better what kind of SQL Vulnerabilities I would have with this original code without this fix.
multi=True. Does this protect you against some specific forms of sql injection? Sure - as you have discovered. Does that mean its safe to not use parameters in your sql and just setmulti=False- No!