Advertisement
teslariu

sql injection

Jan 19th, 2023
1,449
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.10 KB | None | 0 0
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. #
  4. # SQL INJECTION
  5.  
  6. import sqlite3
  7.  
  8. conn = sqlite3.connect("base.sqlite")
  9.  
  10. cursor = conn.cursor()
  11.  
  12. nombre = input("Ingrese un nombre: ")
  13. edad = int(input("Ingrese la edad: "))
  14.  
  15. """
  16. #Forma segura
  17. cursor.execute("INSERT INTO personas VALUES (?,?)", (nombre,edad))
  18. conn.commit()
  19. """
  20.  
  21.  
  22. #Forma algo insegura
  23. #cursor.execute(f"INSERT INTO personas VALUES ('{nombre}',{edad})")
  24. # conn.commit()
  25.  
  26. # voy a intentar un aatque de sql injection ingresando lo siguiente
  27. # nombre: Juan',25); DELETE FROM personas; --
  28. # edad: 32
  29. # esto ejecuta lo siguiente
  30. # "INSERT INTO personas VALUES ('Juan',25); DELETE FROM personas; --',32)"
  31. # arroja un warning, no un error
  32. # sqlite3.Warning: You can only execute one statement at a time.
  33. # ¿que hubiera pasado si tengo los warnings deshabilitados? --> hubiera vaciado
  34. # la tabla personas
  35.  
  36. # Forma insegura
  37. cursor.executescript(f"INSERT INTO personas VALUES ('{nombre}',{edad})")
  38. conn.commit()
  39.  
  40.  
  41. cursor.execute("SELECT * FROM personas")
  42. personas = cursor.fetchall()
  43. print(personas)
  44.  
  45. # Cierro la base
  46. conn.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement