/burp-to-sqlmap.py

https://github.com/Miladkhoshdel/burp-to-sqlmap · Python · 191 lines · 166 code · 21 blank · 4 comment · 36 complexity · 299eb3ac238ad03c778b1731624850a1 MD5 · raw file

  1. try:
  2. import sys
  3. import os
  4. from bs4 import BeautifulSoup
  5. import os.path
  6. import argparse
  7. import codecs
  8. except Exception as e:
  9. print(e)
  10. exit()
  11. def banner():
  12. print(" ")
  13. print(" #######################################################################")
  14. print(" # #")
  15. print(" # \______ \ | \______ \______ \ \__ ___/\_____ \ #")
  16. print(" # | | _/ | /| _/| ___/ | | / | \ #")
  17. print(" # | | \ | / | | \| | | | / | \ #")
  18. print(" # |______ /______/ |____|_ /|____| |____| \_______ / #")
  19. print(" # \/ \/ \/ #")
  20. print(" # _________________ .____ _____ _____ __________ #")
  21. print(" # / _____/\_____ \ | | / \ / _ \\\______ \ #")
  22. print(" # \_____ \ / / \ \| | / \ / \ / /_\ \| ___/ #")
  23. print(" # / \/ \_/. \ |___/ Y \/ | \ | #")
  24. print(" # /_______ /\_____\ \_/_______ \____|__ /\____|__ /____| #")
  25. print(" # \/ \__> \/ \/ \/ #")
  26. print(" # #")
  27. print(" # Created By: Milad Khoshdel E-Mail: miladkhoshdel@gmail.com #")
  28. print(" # #")
  29. print(" #######################################################################")
  30. print(" ")
  31. def usage():
  32. print(" ")
  33. print(" Usage: ./burp-to-sqlmap.py [options]")
  34. print(" Options: -f, --file <BurpSuit State File>")
  35. print(" Options: -o, --outputdirectory <Output Directory>")
  36. print(" Options: -s, --sqlmappath <SQLMap Path>")
  37. print(" Options: -p, --proxy <Use Proxy>")
  38. print(" Options: -r, --risk <SqlMap Risk>")
  39. print(" Options: -l, --level <SqlMap Level>")
  40. print(" Options: -t, --tamper <SqlMap Tamper List>")
  41. print(" Example: python burp-to-sqlmap.py -f [BURP-STATE-FILE] -o [OUTPUT-DIRECTORY] -s [SQLMap-Path]")
  42. print(" ")
  43. def main():
  44. parser = argparse.ArgumentParser()
  45. parser.add_argument("-f", "--file")
  46. parser.add_argument("-o", "--outputdirectory")
  47. parser.add_argument("-s", "--sqlmappath")
  48. parser.add_argument("-p", "--proxy")
  49. parser.add_argument("-r", "--risk")
  50. parser.add_argument("-l", "--level")
  51. parser.add_argument("-t", "--tamper")
  52. args = parser.parse_args()
  53. if not args.file or not args.outputdirectory or not args.sqlmappath:
  54. banner()
  55. usage()
  56. sys.exit(0)
  57. if args.proxy:
  58. proxyvalue = "--proxy " + args.proxy + " "
  59. else:
  60. proxyvalue = ""
  61. if args.risk:
  62. risk_value = "--risk " + args.risk + " "
  63. else:
  64. risk_value = ""
  65. if args.level:
  66. level_value = "--level " + args.level + " "
  67. else:
  68. level_value = ""
  69. if args.level:
  70. tamper_value = "--tamper=" + args.tamper + " "
  71. else:
  72. tamper_value = ""
  73. vulnerablefiles = []
  74. banner()
  75. filename = args.file
  76. directory = args.outputdirectory
  77. sqlmappath = args.sqlmappath
  78. if not os.path.exists(directory):
  79. os.makedirs(directory)
  80. if sys.platform.startswith("win32"):
  81. runWindows(filename, directory, sqlmappath, proxyvalue, vulnerablefiles, risk_value, level_value, tamper_value)
  82. elif sys.platform.startswith("linux"):
  83. runLinux(filename, directory, sqlmappath, proxyvalue, vulnerablefiles, risk_value, level_value, tamper_value)
  84. else:
  85. print("[+] Error: Unsupported OS Detected!")
  86. def runWindows(filename, directory, sqlmappath, proxyvalue, vulnerablefiles, risk_value, level_value, tamper_value):
  87. packetnumber = 0
  88. print(" [+] Exporting Packets ...")
  89. with open(filename, 'r') as f:
  90. soup = BeautifulSoup(f.read(), "html.parser")
  91. for i in soup.find_all("request"):
  92. packetnumber = packetnumber + 1
  93. print(" [-] Packet " + str(packetnumber) + " Exported.")
  94. outfile = open(os.path.join(directory, str(packetnumber) + ".txt"), "w")
  95. outfile.write(i.text.strip())
  96. print(" ")
  97. print(str(packetnumber) + " Packets Exported Successfully.")
  98. print(" ")
  99. print(" [+] Testing SQL Injection on packets ... (Based on your network connection Test can take up to 5 minutes.)")
  100. for file in os.listdir(directory):
  101. print(" [-] Performing SQL Injection on packet number " + file[:-4] + ". Please Wait ...")
  102. os.system("python " + sqlmappath + "\sqlmap.py -r " + os.path.dirname(os.path.realpath(
  103. __file__)) + "\\" + directory + "\\" + file + " --batch " + proxyvalue + risk_value + level_value + tamper_value + " > " + os.path.dirname(
  104. os.path.realpath(__file__)) + "\\" + directory + "\\testresult" + file)
  105. if 'is vulnerable' in open(directory + "\\testresult" + file).read() or "Payload:" in open(
  106. directory + "\\testresult" + file).read():
  107. print(" - URL is Vulnerable.")
  108. vulnerablefiles.append(file)
  109. else:
  110. print(" - URL is not Vulnerable.")
  111. print(" - Output saved in " + directory + "\\testresult" + file)
  112. print(" ")
  113. print("--------------")
  114. print("Test Done.")
  115. print("Result:")
  116. if not vulnerablefiles:
  117. print("No vulnerabilities found on your target.")
  118. else:
  119. for items in vulnerablefiles:
  120. print("Packet " + items[:-4] + " is vulnerable to SQL Injection. for more information please see " + items)
  121. print("--------------")
  122. print(" ")
  123. def runLinux(filename, directory, sqlmappath, proxyvalue, vulnerablefiles, risk_value, level_value, tamper_value):
  124. packetnumber = 0
  125. print(" [+] Exporting Packets ...")
  126. with open(filename, 'r') as f:
  127. soup = BeautifulSoup(f.read(), "html.parser")
  128. for i in soup.find_all("request"):
  129. packetnumber = packetnumber + 1
  130. print(" [-] Packet " + str(packetnumber) + " Exported.")
  131. outfile = codecs.open(os.path.join(directory, str(packetnumber) + ".txt"), "w", "utf-16le")
  132. outfile.write(i.text.strip())
  133. print(" ")
  134. print(str(packetnumber) + " Packets Exported Successfully.")
  135. print(" ")
  136. print(" [+] Testing SQL Injection on packets ... (Based on your network connection Test can take up to 5 minutes.)")
  137. for file in os.listdir(directory):
  138. #The following few lines solves an issue with the character encoding.
  139. #Burp in Kali exports the HTTP history as UTF-16LE which was resulting
  140. #in the individual request files not being read successfully by sqlmap
  141. #There is probably a cleaner way to do this.
  142. cmd = "iconv -f utf-16le -t ascii %s > %s_ascii" % (os.path.dirname(os.path.realpath(__file__)) + "/" + directory + "/" + file,os.path.dirname(os.path.realpath(__file__)) + "/" + directory + "/" + file)
  143. os.system(cmd)
  144. cmd = "cat %s_ascii > %s" % (os.path.dirname(os.path.realpath(__file__)) + "/" + directory + "/" + file,os.path.dirname(os.path.realpath(__file__)) + "/" + directory + "/" + file)
  145. os.system(cmd)
  146. cmd = "rm %s_ascii" % (os.path.dirname(os.path.realpath(__file__)) + "/" + directory + "/" + file)
  147. os.system(cmd)
  148. print(" [-] Performing SQL Injection on packet number " + file[:-4] + ". Please Wait ...")
  149. cmd = "python " + sqlmappath + "/sqlmap.py -r " + os.path.dirname(os.path.realpath(__file__)) + "/" + directory + "/" + file + " --batch " + proxyvalue + risk_value + level_value + tamper_value + " > " + os.path.dirname(os.path.realpath(__file__)) + "/" + directory + "/testresult" + "_" + file
  150. os.system(cmd)
  151. if 'is vulnerable' in open(directory + "/testresult" + "_" + file).read() or "Payload:" in open(
  152. directory + "/testresult" + "_" + file).read():
  153. print(" - URL is Vulnerable.")
  154. vulnerablefiles.append(file)
  155. else:
  156. print(" - URL is not Vulnerable.")
  157. print(" - Output saved in " + directory + "/testresult" + file)
  158. print(" ")
  159. print("--------------")
  160. print("Test Done.")
  161. print("Result:")
  162. if not vulnerablefiles:
  163. print("No vulnerabilities found on your target.")
  164. else:
  165. for items in vulnerablefiles:
  166. print("Packet " + items[:-4] + " is vulnerable to SQL Injection. for more information please see " + items)
  167. print("--------------")
  168. print(" ")
  169. if __name__ == "__main__":
  170. main()