Nenhuma descrição

sql.py 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. from flask import Flask, render_template, request
  2. import mysql.connector
  3. import hashlib
  4. app = Flask(__name__)
  5. @app.route('/phone', methods=['GET' , 'POST'])
  6. def pbook():
  7. if request.method == "POST":
  8. name = request.form['name']
  9. try:
  10. db = mysql.connector.connect(host="localhost",
  11. user="root",
  12. passwd="python1234",
  13. db="sqlinjection")
  14. cursor = db.cursor()
  15. query = "select * from phonebook where name = '"
  16. query += name
  17. query += "';"
  18. print query
  19. count = 0
  20. login = False
  21. for result in cursor.execute(query, multi=True):
  22. if result.with_rows:
  23. print("Rows produced by statement '{}':".format(
  24. result.statement))
  25. ls = result.fetchall()
  26. print ls
  27. if len(ls) > 0 and count == 0:
  28. login = True
  29. else:
  30. print("Number of rows affected by statement '{}': {}".format(
  31. result.statement, result.rowcount))
  32. count += 1
  33. db.commit()
  34. cursor.close()
  35. db.close()
  36. if login:
  37. returnString = "<h1>Results:</h1>"
  38. for item in ls:
  39. returnString += '<h3>' + item[0] + " " + item[1] + '</h3>'
  40. returnString += "<a href='http://localhost:5000/phone'>Back to Phone Book</a>"
  41. return returnString
  42. else:
  43. return "<h1>Person Not Found</h1><a href='http://localhost:5000/phone'>Back to Phone Book</a>"
  44. except Exception as e:
  45. strval = str(e)
  46. msg = "<h1>"
  47. msg += strval
  48. msg += "</h1><a href='http://localhost:5000/phone'>Back to Phone Book</a>"
  49. return msg
  50. else:
  51. return render_template('phone.html')
  52. @app.route('/auth', methods=['GET' , 'POST'])
  53. def send():
  54. if request.method == "POST":
  55. user = request.form['user']
  56. pwd = request.form['pwd']
  57. pwd = hashlib.md5(pwd).hexdigest()
  58. try:
  59. db = mysql.connector.connect(host="localhost",
  60. user="root",
  61. passwd="python1234",
  62. db="sqlinjection")
  63. cursor = db.cursor()
  64. query = "select * from login where password = '"
  65. query += pwd
  66. query += "' and username = '"
  67. query += user
  68. query += "';"
  69. print query
  70. count = 0
  71. login = False
  72. for result in cursor.execute(query, multi=True):
  73. if result.with_rows:
  74. print("Rows produced by statement '{}':".format(
  75. result.statement))
  76. ls = result.fetchall()
  77. print ls
  78. if len(ls) > 0 and count == 0:
  79. login = True
  80. else:
  81. print("Number of rows affected by statement '{}': {}".format(
  82. result.statement, result.rowcount))
  83. count += 1
  84. db.commit()
  85. cursor.close()
  86. db.close()
  87. if login:
  88. return '<h1>Authentication Succesful</h1><h1>Welcome Back, ' + user + "</h1><a href='http://localhost:5000/auth'>Back to Login</a>"
  89. else:
  90. return "<h1>Authentication Failed</h1><a href='http://localhost:5000/auth'>Back to Login</a>"
  91. except Exception as e:
  92. strval = str(e)
  93. msg = "<h1>"
  94. msg += strval
  95. msg += "</h1><a href='http://localhost:5000/auth'>Back to Login</a>"
  96. return msg
  97. else:
  98. return render_template('index.html')
  99. if __name__ == "__main__":
  100. app.run(debug=True)