Advertisement
Guest User

Untitled

a guest
Apr 11th, 2019
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.65 KB | None | 0 0
  1. import pika
  2. import pyautogui
  3. import cv2
  4. import time
  5. import pyperclip as pyperclip
  6. import requests
  7. import sqlite3
  8. import json
  9. import cv2 as cv
  10. import numpy
  11. from urllib.parse import urlparse
  12.  
  13.  
  14. # function for getting data from database
  15. def data_from_database():
  16. global browser_name
  17. global os_name
  18. global range
  19. global host
  20. global user
  21. global password
  22. global queue
  23. global exchange
  24. global reporter_queue
  25.  
  26. conn = sqlite3.connect('phishing.sqlite')
  27. c = conn.cursor()
  28.  
  29. browser_name = c.execute('SELECT value FROM configurations WHERE key_data=\'browser_name\' LIMIT 1;').fetchone()[0]
  30. os_name = c.execute('SELECT value FROM configurations WHERE key_data=\'os_name\' LIMIT 1;').fetchone()[0]
  31. range = c.execute('SELECT value FROM configurations WHERE key_data=\'comparative_index\' LIMIT 1;').fetchone()[0]
  32. host = c.execute('SELECT value FROM configurations WHERE key_data=\'rabbit_mq_host\' LIMIT 1;').fetchone()[0]
  33. user = c.execute('SELECT value FROM configurations WHERE key_data=\'rabbit_mq_customer_user\' LIMIT 1;').fetchone()[
  34. 0]
  35. password = \
  36. c.execute('SELECT value FROM configurations WHERE key_data=\'rabbit_mq_customer_pass\' LIMIT 1;').fetchone()[0]
  37. if browser_name == 'chrome':
  38. queue = 'phishing_new'
  39. else:
  40. queue = \
  41. c.execute('SELECT value FROM configurations WHERE key_data=\'rabbit_mq_customer_edge_queue\' LIMIT 1;').fetchone()[0]
  42. exchange = \
  43. c.execute('SELECT value FROM configurations WHERE key_data=\'rabbit_mq_customer_exchange\' LIMIT 1;').fetchone()[0]
  44. reporter_queue = \
  45. c.execute('SELECT value FROM configurations WHERE key_data=\'rabbit_mq_reporter_queue\' LIMIT 1;').fetchone()[0]
  46.  
  47. conn.close()
  48.  
  49.  
  50. # function for connection to RabbitMQ server
  51. def connect_to_rabbit_mq_server():
  52. global credentials
  53.  
  54. credentials = pika.PlainCredentials(user, password)
  55. connection = pika.BlockingConnection(pika.ConnectionParameters(
  56. host=host, port=5672, credentials=credentials)) # open connection
  57. channel = connection.channel() # get the channel
  58. channel.queue_declare(queue=queue) # set the queue
  59. return channel
  60.  
  61.  
  62. # main function
  63. def run_test(ch, method, properties, body):
  64. data = json.loads(body)
  65. data['system'] = 'new_system'
  66. data['domain'] = data['address']
  67.  
  68. print(" [x] Starting phishing test for url: " + data['domain']) # print to log
  69. ch.basic_ack(delivery_tag=method.delivery_tag) # keep alive and send feedback
  70. if test_url(data, 1): # test availability
  71. # if test_redirect(url_parsed): # test redirect
  72. if browser_name == 'chrome':
  73. control_browser(os_name, data['domain']) # control Chrome to url
  74. else:
  75. control_browser(os_name, data['address']) # control Chrome to url
  76. get_image_difference(data) # check pictures
  77. clear_downloads() # clear Downloads folder
  78.  
  79.  
  80. # function for deleting files in Downloads folder
  81. def clear_downloads():
  82. import os
  83. import glob
  84.  
  85. files = ''
  86. if os_name == 'macos':
  87. files = '/Users/' + os.getlogin() + '/Downloads/*'
  88. if os_name == 'windows':
  89. files = glob.glob('C:/Users/' + os.getlogin() + '/Downloads/*')
  90. for f in files:
  91. try:
  92. os.remove(f)
  93. except Exception as e:
  94. print(e)
  95.  
  96.  
  97. # function for testing url availability, try 5 times and report if fails
  98. def test_url(data, current_try):
  99. try:
  100. requests.get('http://' + data['address'], timeout=(10, 30))
  101. return True
  102. except Exception as e:
  103. if current_try > 5:
  104. print('Url ' + data['domain'] + ' cant be reached, error: ' + str(e))
  105. report_invalid_url(data, str(e))
  106. return False
  107. else:
  108. current_try += 1
  109. test_url(data, current_try)
  110.  
  111.  
  112. # function for testing redirect | if url is different then report
  113. def test_redirect(url):
  114.  
  115. # test availability
  116. try:
  117. r = requests.get(url, timeout=(10, 30))
  118.  
  119. # test redirect
  120. if '{uri.scheme}://{uri.netloc}'.format(uri=urlparse(url)) in r.url:
  121. return True
  122. else:
  123. print('Url ' + url + ' has redirection, error: ' + "Redirected url: " + r.url)
  124. report_invalid_redirect(url, "Redirected url: " + r.url)
  125. return False
  126. except Exception as e:
  127. print('Url ' + url + ' is invalid, error: ' + str(e))
  128. return False
  129.  
  130.  
  131. # function for opening browser
  132. def open_browser():
  133.  
  134. if browser_name == 'chrome':
  135.  
  136. if os_name == 'macos':
  137. pyautogui.hotkey('ctrl', 'shift', ']')
  138. if os_name == 'windows':
  139. pyautogui.hotkey('ctrl', 'alt', ']')
  140.  
  141. elif browser_name == 'edge':
  142.  
  143. if os_name == 'macos':
  144. pyautogui.hotkey('ctrl', 'shift', '[')
  145. if os_name == 'windows':
  146. pyautogui.hotkey('ctrl', 'alt', '[')
  147.  
  148. time.sleep(4)
  149.  
  150.  
  151. # function for controlling browser for getting screenshots
  152. def control_browser(os_name, url):
  153.  
  154. # open new tab
  155. if os_name == 'macos':
  156. pyautogui.hotkey('command', 't')
  157. if os_name == 'windows':
  158. pyautogui.hotkey('ctrl', 't')
  159. time.sleep(1)
  160.  
  161. # type url
  162. if os_name == 'macos':
  163. pyautogui.hotkey('command', 'l')
  164. if os_name == 'windows':
  165. pyautogui.hotkey('ctrl', 'l')
  166. time.sleep(1)
  167.  
  168. # fast text enter
  169. pyperclip.copy(str(url))
  170. if os_name == 'macos':
  171. pyautogui.hotkey('command', 'v')
  172. if os_name == 'windows':
  173. pyautogui.hotkey('ctrl', 'v')
  174.  
  175. pyautogui.press('enter')
  176. time.sleep(5)
  177.  
  178. # make screenshow
  179. pyautogui.screenshot('fresh_screenshot_phished_screen.png')
  180. time.sleep(2)
  181.  
  182. # close tab
  183. if os_name == 'macos':
  184. pyautogui.hotkey('command', 'w')
  185. if os_name == 'windows':
  186. pyautogui.hotkey('ctrl', 'w')
  187. time.sleep(1)
  188.  
  189.  
  190. # function for closing browser
  191. def close_browser():
  192. if os_name == 'windows':
  193. pyautogui.hotkey('alt', 'f4')
  194.  
  195.  
  196. # function for creating screenshot for saving
  197. def generate_screenshot_for_saving(phished):
  198. import os
  199.  
  200. try:
  201. path = ''
  202.  
  203. if phished:
  204. folder = 'Phished'
  205. else:
  206. folder = 'Clean'
  207.  
  208. if os_name == 'macos':
  209. path = '/Users/' + os.getlogin() + '/Desktop/Screenshots/' + folder
  210. elif os_name == 'windows':
  211. path = 'C:/Users/' + os.getlogin() + '/Desktop/Screenshots/' + folder
  212.  
  213. if not os.path.exists(path):
  214. os.makedirs(path)
  215.  
  216. pyautogui.screenshot(path + '/screenshot_' + time.strftime("%Y_%m_%d-%H_%M_%S") + '.png')
  217. except Exception as e:
  218. print(e)
  219.  
  220.  
  221. # function for closing Allow / Block pop-up
  222. def close_allow_block_popup():
  223. try:
  224.  
  225. x_coordinate = 0
  226. y_coordinate = 0
  227.  
  228. img_rgb = cv.imread('fresh_screenshot_phished_screen.png')
  229. img_gray = cv.cvtColor(img_rgb, cv.COLOR_BGR2GRAY)
  230. template = cv.imread('allow_block_template.png', 0)
  231. res = cv.matchTemplate(img_gray, template, cv.TM_CCOEFF_NORMED)
  232. threshold = 0.8
  233. loc = numpy.where(res >= threshold)
  234. for pt in zip(*loc[::-1]):
  235. x_coordinate = pt[0] + 5
  236. y_coordinate = pt[1] + 5
  237.  
  238. if x_coordinate != 0 or y_coordinate != 0:
  239. pyautogui.moveTo(x_coordinate + 5, y_coordinate + 5)
  240. pyautogui.click()
  241. time.sleep(2)
  242.  
  243. except Exception as e:
  244. print(e)
  245.  
  246.  
  247. # function for defining difference percentage
  248. def get_image_difference(data):
  249. image_1 = cv2.imread('fresh_screenshot_phished_screen.png', 0)
  250. if browser_name == 'chrome':
  251. image_2 = cv2.imread('template_for_phished_site3.png', 0)
  252. else:
  253. image_2 = cv2.imread('phished_template_' + browser_name + '.png', 0)
  254.  
  255. first_image_hist = cv2.calcHist([image_1], [0], None, [256], [0, 256])
  256. second_image_hist = cv2.calcHist([image_2], [0], None, [256], [0, 256])
  257.  
  258. img_hist_diff = cv2.compareHist(first_image_hist, second_image_hist, cv2.HISTCMP_BHATTACHARYYA)
  259. img_template_probability_match = \
  260. cv2.matchTemplate(first_image_hist, second_image_hist, cv2.TM_CCOEFF_NORMED)[0][0]
  261. img_template_diff = 1 - img_template_probability_match
  262.  
  263. # taking only 10% of histogram diff, since it's less accurate than template method
  264. commutative_image_diff = (img_hist_diff / 10) + img_template_diff
  265. report_phishing(commutative_image_diff, data)
  266.  
  267.  
  268. # function for reporting phishing
  269. def report_phishing(comparative_index, data):
  270. if comparative_index < float(range):
  271. print("Url: " + data['domain'] + " has been phished!")
  272. print("Match percentage: " + str(comparative_index))
  273. data['error_type'] = 'phishing'
  274. data['error_message'] = ''
  275. data['browser_name'] = browser_name
  276. load_rabbitmq(json.dumps(data))
  277. else:
  278. close_allow_block_popup()
  279.  
  280.  
  281. # function for url unavailable
  282. def report_invalid_url(data, error):
  283. print("Url: " + data['domain'] + " cant be reached!")
  284. data['error_type'] = 'invalid'
  285. data['error_message'] = error
  286. load_rabbitmq(json.dumps(data))
  287.  
  288.  
  289. # function for invalid redirect
  290. def report_invalid_redirect(data, error):
  291. print("Url: " + data['domain'] + " has invalid redirect!")
  292. data['error_type'] = 'redirect'
  293. data['error_message'] = error
  294. load_rabbitmq(json.dumps(data))
  295.  
  296.  
  297. # function for loading rabbit MQ
  298. def load_rabbitmq(data):
  299. report_connection = pika.BlockingConnection(pika.ConnectionParameters(
  300. host=host, port=5672, credentials=credentials)) # open connection
  301. report_channel = report_connection.channel() # get channel
  302. report_channel.queue_declare(queue=reporter_queue) # set queue
  303. report_channel.basic_publish(exchange='',
  304. routing_key=reporter_queue,
  305. body=data,
  306. properties=pika.BasicProperties(delivery_mode=2)) # publish message to customers
  307. report_connection.close() # close connection
  308.  
  309.  
  310. # function for consuming from rabbitmq
  311. def start_consuming():
  312. channel = connect_to_rabbit_mq_server()
  313. channel.basic_qos(prefetch_count=1)
  314. channel.basic_consume(run_test, queue=queue) # run function when called
  315. channel.start_consuming() # start consuming
  316.  
  317.  
  318. # main function
  319. def main():
  320. data_from_database() # load data from database
  321. open_browser() # open chrome
  322.  
  323. try:
  324. start_consuming()
  325. except Exception as e:
  326. print(e)
  327. start_consuming()
  328.  
  329. close_browser() # close Chrome
  330.  
  331.  
  332. main() # run script
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement