Advertisement
teslariu

SQL Injection example

Sep 14th, 2022
1,139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.07 KB | None | 0 0
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. #
  4. # PREVENCION DE INYECCION DE CODIGO
  5. # Inyeccion de SQL
  6. # >> Nombre: Ale', 22); DELETE FROM personas; --
  7. # >> Edad: 29
  8.  
  9. import sqlite3
  10.  
  11. conn = sqlite3.connect("base.sqlite")
  12. cursor = conn.cursor()
  13.  
  14. cursor.execute("CREATE TABLE IF NOT EXISTS personas (nombre TEXT, edad NUMERIC)")
  15.  
  16. conn.commit()
  17.  
  18. nombre = input("Nombre: ")
  19. edad = int(input("Edad: "))
  20.  
  21. # modo seguro
  22. # cursor.execute("INSERT INTO personas VALUES (?,?)",(nombre,edad))
  23.  
  24. # modo inseguro
  25. # cursor.execute(f"INSERT INTO personas VALUES ('{nombre}', {edad})")
  26. # Lanza un sqlite3.Warning: You can only execute one statement at a time.
  27. """
  28. "INSERT INTO personas VALUES ('Ale', 22); DELETE FROM personas; --', 29)"
  29. Esto son 2 consultas SQL:
  30. 1) INSERT INTO personas VALUES ('Ale', 22)   --> agrega Ale,22
  31. 2) DELETE FROM personas  --> borra todos los datos de la tabla personas
  32. 3) --', 29)   .> no importa -- significa comentario
  33.  
  34. """
  35. # MODO FATAL
  36. cursor.executescript(f"INSERT INTO personas VALUES ('{nombre}', {edad})")
  37. conn.commit()
  38.    
  39. conn.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement