Advertisement
Omnifarious

Simplistic SQL injection detection

Feb 2nd, 2019
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.95 KB | None | 0 0
  1. import sqlite3
  2.  
  3. scary_data_from_user = "person"
  4. create_sql="CREATE TABLE IF NOT EXISTS person ( name VARCHAR , age INTEGER );"
  5. db = "sample.db"
  6. ins_sql_works="INSERT INTO person (name, age) VALUES (?, ?);"
  7. ins_sql_notworks="INSERT INTO ? (name, age) VALUES (?, ?);"
  8.  
  9. conn = sqlite3.connect(db)
  10. cur = conn.cursor()
  11. cur.execute(create_sql)
  12. cur.execute(ins_sql_works,("omer",9999))
  13. #WORKS without problem
  14.  
  15. if len(cur.execute("select sql from sqlite_master where type = 'table' and name = ?;", (scary_data_from_user,)).fetchall()) < 1:
  16.     raise RuntimeError("The user is trying to hack me! HELP! HELP!")
  17. else:
  18.     # Note that there is no way for the query in the if statement to return any rows if the
  19.     # scary_data_from_user contains something malicious. This is perfectly safe to do here,
  20.     # given the if statement up above.
  21.     cur.execute(f"INSERT INTO {scary_data_from_user} (name, age) VALUES (?, ?);", ("omer", 9999))
  22. conn.commit()
  23. conn.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement