Advertisement
Guest User

Untitled

a guest
Apr 15th, 2019
244
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.23 KB | None | 0 0
  1. from selenium import webdriver
  2. import pika
  3. import sqlite3
  4. import json
  5. import requests
  6. import time
  7. import datetime
  8. import pytz
  9. import csv
  10.  
  11.  
  12. # function for getting data from database
  13. def data_from_database():
  14. global browser_name
  15. global os_name
  16. global range
  17. global host
  18. global user
  19. global password
  20. global queue
  21. global exchange
  22. global reporter_queue
  23. global slack_channel
  24.  
  25. conn = sqlite3.connect('kaspersky.sqlite')
  26. c = conn.cursor()
  27.  
  28. browser_name = c.execute('SELECT value FROM configurations WHERE key_data=\'browser_name\' LIMIT 1;').fetchone()[0]
  29. os_name = c.execute('SELECT value FROM configurations WHERE key_data=\'os_name\' LIMIT 1;').fetchone()[0]
  30. range = c.execute('SELECT value FROM configurations WHERE key_data=\'comparative_index\' LIMIT 1;').fetchone()[0]
  31. host = c.execute('SELECT value FROM configurations WHERE key_data=\'rabbit_mq_host\' LIMIT 1;').fetchone()[0]
  32. user = c.execute('SELECT value FROM configurations WHERE key_data=\'rabbit_mq_customer_user\' LIMIT 1;').fetchone()[
  33. 0]
  34. password = \
  35. c.execute('SELECT value FROM configurations WHERE key_data=\'rabbit_mq_customer_pass\' LIMIT 1;').fetchone()[0]
  36. queue = \
  37. c.execute(
  38. 'SELECT value FROM configurations WHERE key_data=\'rabbit_mq_kaspersky_quoue\' LIMIT 1;').fetchone()[0]
  39. exchange = \
  40. c.execute(
  41. 'SELECT value FROM configurations WHERE key_data=\'rabbit_mq_customer_exchange\' LIMIT 1;').fetchone()[0]
  42. reporter_queue = \
  43. c.execute('SELECT value FROM configurations WHERE key_data=\'rabbit_mq_reporter_queue\' LIMIT 1;').fetchone()[0]
  44. slack_channel = c.execute('SELECT value FROM configurations WHERE key_data=\'slack_channel\' LIMIT 1;').fetchone()[
  45. 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. body = json.loads(body)
  65. print(" [x] Starting Kaspersky test for url: " + body['address']) # print to log
  66. ch.basic_ack(delivery_tag=method.delivery_tag) # keep alive and send feedback
  67. test(body)
  68.  
  69.  
  70. def test(body):
  71. domain = 'http://' + body['address']
  72. driver = webdriver.Firefox()
  73. driver.get(domain)
  74. elem_alert_heading = ''
  75.  
  76. try:
  77. elem_alert_heading = driver.find_element_by_xpath("//*[contains(text(), 'Access denied')]")
  78. except Exception as e:
  79. pass
  80.  
  81. if elem_alert_heading == '':
  82. try:
  83. elem_alert_heading = driver.find_element_by_xpath("//*[contains(text(), 'This website is not safe')]")
  84. except Exception as e:
  85. pass
  86.  
  87. if 'Kaspersky' in driver.title:
  88. if elem_alert_heading and elem_alert_heading.text != '':
  89. print('Kaspersky has reported ' + str(domain) + ' flagged!')
  90.  
  91. write_into_csv(str(body['address']), str(body['identity_name']), 'Kaspersky Reporter',
  92. 'URL has been reported as FLAGGED by Kaspersky!')
  93.  
  94. result = update_domain(body)
  95. if result.ok:
  96. create_slack_notification(body, True)
  97. else:
  98. create_slack_notification(body, False)
  99.  
  100. time.sleep(2)
  101. driver.close()
  102.  
  103.  
  104. def write_into_csv(domain, identity_name, error_code, message):
  105. with open('kaspersky_reporter_log.csv', mode='a') as reporter_log:
  106. reporter_writer = csv.writer(reporter_log, delimiter=';', quotechar='"', quoting=csv.QUOTE_MINIMAL)
  107.  
  108. utc = pytz.UTC
  109. now = utc.localize(datetime.datetime.now())
  110.  
  111. # time;domain;identity_name;error_code;message
  112.  
  113. reporter_writer.writerow([str(now), domain, identity_name, error_code, message])
  114.  
  115.  
  116. # function for making Slack notification
  117. def create_slack_notification(body, success):
  118.  
  119. text = 'Python script detected flag on url: ' + str(body['address']) + ', Indentity: ' + str(body['identity_name']) + ' by Kaspersky!'
  120.  
  121. if not success:
  122. text += ' Database update FAILED'
  123.  
  124. post_fields = {
  125. 'text': 'URL has been reported as FLAGGED by Kaspersky!',
  126. 'attachments': [
  127. {
  128. 'fallback': ' ',
  129. 'author_name': 'schedule check',
  130. 'title': 'URL FLAGGED | Kaspersky',
  131. 'text': text
  132. }
  133. ]}
  134.  
  135. response = requests.post(
  136. 'https://hooks.slack.com/services/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', data=json.dumps(post_fields),
  137. headers={'Content-Type': 'application/json'}
  138. )
  139. if response.status_code != 200:
  140. print('Unsuccessfully created Slack notification!')
  141.  
  142.  
  143. # update flagged domain
  144. def update_domain(body):
  145. auth_data = {
  146. 'grant_type': 'client_credentials',
  147. 'client_id': 'M6YE13UwxRw9wsvvJ7k0kRiErZ3558F6s64XAxF3',
  148. 'client_secret': 'PrtByEX7fA8SlztUUXREUYcH5RHbSjnVCGQYtWtd8oEMPif7eT5nWVhHi6Np7I0N6FeeyCkdAjB28LQEX67bLtppOhntCmD0W9lwrOosOFUHriNI7DyEg9c3ZS4cPSca'
  149. }
  150.  
  151. auth_data = {
  152. 'grant_type': 'password',
  153. 'client_id': 'coinis_v2_api_testing',
  154. 'username': 'coinis_v2_api_testing',
  155. 'password': 'i1k7jlwSV9b8vNrAcpm4xmSuB1ckZ2'
  156. }
  157.  
  158. auth_url = 'https://dash.webb.co.me/api/auth/token/'
  159.  
  160. r = requests.post(auth_url, data=auth_data)
  161. response = json.loads(r.text)
  162. access_token = response['access_token']
  163. expires_in = response['expires_in']
  164.  
  165. auth_string = 'Bearer ' + access_token
  166.  
  167. headers = {
  168. 'Authorization': auth_string
  169. }
  170.  
  171. now = datetime.datetime.now()
  172. data = {
  173. "status": "notok",
  174. "description": "Flagged by VM Test Kaspersky",
  175. "flagged_time": str(now.isoformat()),
  176. "testing_platform_code": "vm_kaspersky"
  177. }
  178.  
  179. r = requests.patch("https://dash.webb.co.me/api/domains/" + str(body['id'] + '/'), headers=headers, data=data)
  180. return r
  181.  
  182.  
  183. # get domains from database
  184. def get_domains_from_api():
  185.  
  186. auth_data = {
  187. 'grant_type': 'client_credentials',
  188. 'client_id': 'M6YE13UwxRw9wsvvJ7k0kRiErZ3558F6s64XAxF3',
  189. 'client_secret': 'PrtByEX7fA8SlztUUXREUYcH5RHbSjnVCGQYtWtd8oEMPif7eT5nWVhHi6Np7I0N6FeeyCkdAjB28LQEX67bLtppOhntCmD0W9lwrOosOFUHriNI7DyEg9c3ZS4cPSca'
  190. }
  191.  
  192. auth_data = {
  193. 'grant_type': 'password',
  194. 'client_id': 'coinis_v2_api_testing',
  195. 'username': 'coinis_v2_api_testing',
  196. 'password': 'i1k7jlwSV9b8vNrAcpm4xmSuB1ckZ2'
  197. }
  198.  
  199. auth_url = 'https://dash.webb.co.me/api/auth/token/'
  200.  
  201. r = requests.post(auth_url, data=auth_data)
  202. response = json.loads(r.text)
  203. access_token = response['access_token']
  204. expires_in = response['expires_in']
  205.  
  206. auth_string = 'Bearer ' + access_token
  207.  
  208. headers = {
  209. 'Authorization': auth_string
  210. }
  211.  
  212. r = requests.get("https://dash.webb.co.me/api/domains/?is_live=1&limit=2000",
  213. headers=headers)
  214.  
  215. result_urls = []
  216. for result in r.json()['results']:
  217. result_urls.append(result['address'])
  218.  
  219. return result_urls
  220.  
  221.  
  222. # function for loading rabbit MQ
  223. def load_rabbitmq(data):
  224. report_connection = pika.BlockingConnection(pika.ConnectionParameters(
  225. host=host, port=5672, credentials=credentials)) # open connection
  226. report_channel = report_connection.channel() # get channel
  227. report_channel.queue_declare(queue=reporter_queue) # set queue
  228. report_channel.basic_publish(exchange='',
  229. routing_key=reporter_queue,
  230. body=data,
  231. properties=pika.BasicProperties(delivery_mode=2)) # publish message to customers
  232. report_connection.close() # close connection
  233.  
  234.  
  235. # function for consuming from rabbitmq
  236. def start_consuming():
  237. channel = connect_to_rabbit_mq_server()
  238. channel.basic_qos(prefetch_count=1)
  239. channel.basic_consume(run_test, queue=queue) # run function when called
  240. channel.start_consuming() # start consuming
  241.  
  242.  
  243. # main function
  244. def main():
  245. data_from_database() # load data from database
  246.  
  247. try:
  248. start_consuming()
  249. except Exception as e:
  250. print(e)
  251. start_consuming()
  252.  
  253.  
  254. main() # run script
  255.  
  256. # # main function
  257. # def main():
  258. # data_from_database() # load data from database
  259. #
  260. # try:
  261. # start_consuming()
  262. # except Exception as e:
  263. # print(e)
  264. # start_consuming()
  265. #
  266. #
  267. # # main() # run script | THIS IS IF WE USE RABBITMQ
  268. #
  269. # # function for running test forever
  270. # def run_test():
  271. #
  272. # while True:
  273. # run_in_circle()
  274. # time.sleep(5)
  275. #
  276. #
  277. # # function for testing all domains from api
  278. # def run_in_circle():
  279. #
  280. # domains_from_api = get_domains_from_api()
  281. # random.shuffle(domains_from_api)
  282. #
  283. # test('yoursdealaptitudetheclicks.icu')
  284. # # for domain in domains_from_api:
  285. # # test(domain)
  286. #
  287. #
  288. # run_test()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement