No Description

SQLInjection.py 1.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. from SQLLexer import *
  2. from SQLParser import *
  3. class SQLInjection():
  4. def __init__(self):
  5. self.lexer = SQLLexer()
  6. self.lexer.build()
  7. self.parser = SQLParser()
  8. self.u_tok_counter = None
  9. self.s_tok_counter = None
  10. self.u_ast = None
  11. self.s_ast = None
  12. def validateLex(self, sample_sql, user_sql):
  13. self.s_tok_counter = self.lexer.getTokensHash()
  14. self.u_tok_counter = self.lexer.getTokensHash()
  15. for tok in self.lexer.tokenize(sample_sql):
  16. self.s_tok_counter[tok.type] += 1
  17. for tok in self.lexer.tokenize(user_sql):
  18. self.u_tok_counter[tok.type] += 1
  19. return self.s_tok_counter == self.u_tok_counter
  20. def getLastTokCounters(self):
  21. return self.s_tok_counter, self.u_tok_counter
  22. def validateParser(self, sample_sql, user_sql):
  23. self.s_ast = self.parser.parse(sample_sql)
  24. self.u_ast = self.parser.parse(user_sql)
  25. return self.s_ast == self.u_ast
  26. def getLastAsts(self):
  27. return self.s_ast, self.u_ast
  28. def print_ast(self, ast):
  29. Q = ast
  30. while len(Q) > 0:
  31. NQ = []
  32. for node in Q:
  33. if type(node) == tuple:
  34. print node[0],
  35. for i in range(1, len(node)):
  36. NQ.append(node[i])
  37. else:
  38. print node,
  39. Q = NQ
  40. print
  41. if __name__ == '__main__':
  42. sqlI = SQLInjection()
  43. # Test 1
  44. print sqlI.validateLex("""select cat from dog where casa=1 ;""", """select cat from dog where casa=1 ;""")
  45. # Test 2
  46. print sqlI.validateLex("""select cat from dog where casa=1 ;""", """select cat from dog where casa=1 and cat="miau" ;""")
  47. # Test 3
  48. print sqlI.validateParser("""select cat from dog where casa=1 ;""", """select cat from dog where casa=1 ;""")
  49. # Test 2
  50. print sqlI.validateParser("""select cat from dog where casa=1 ;""", """select cat from dog where casa=1 and cat="miau" ;""")