Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from selenium import webdriver
- import pika
- import sqlite3
- import json
- import requests
- import time
- import datetime
- import pytz
- import csv
- # function for getting data from database
- def data_from_database():
- global browser_name
- global os_name
- global range
- global host
- global user
- global password
- global queue
- global exchange
- global reporter_queue
- global slack_channel
- conn = sqlite3.connect('kaspersky.sqlite')
- c = conn.cursor()
- browser_name = c.execute('SELECT value FROM configurations WHERE key_data=\'browser_name\' LIMIT 1;').fetchone()[0]
- os_name = c.execute('SELECT value FROM configurations WHERE key_data=\'os_name\' LIMIT 1;').fetchone()[0]
- range = c.execute('SELECT value FROM configurations WHERE key_data=\'comparative_index\' LIMIT 1;').fetchone()[0]
- host = c.execute('SELECT value FROM configurations WHERE key_data=\'rabbit_mq_host\' LIMIT 1;').fetchone()[0]
- user = c.execute('SELECT value FROM configurations WHERE key_data=\'rabbit_mq_customer_user\' LIMIT 1;').fetchone()[
- 0]
- password = \
- c.execute('SELECT value FROM configurations WHERE key_data=\'rabbit_mq_customer_pass\' LIMIT 1;').fetchone()[0]
- queue = \
- c.execute(
- 'SELECT value FROM configurations WHERE key_data=\'rabbit_mq_kaspersky_quoue\' LIMIT 1;').fetchone()[0]
- exchange = \
- c.execute(
- 'SELECT value FROM configurations WHERE key_data=\'rabbit_mq_customer_exchange\' LIMIT 1;').fetchone()[0]
- reporter_queue = \
- c.execute('SELECT value FROM configurations WHERE key_data=\'rabbit_mq_reporter_queue\' LIMIT 1;').fetchone()[0]
- slack_channel = c.execute('SELECT value FROM configurations WHERE key_data=\'slack_channel\' LIMIT 1;').fetchone()[
- 0]
- conn.close()
- # function for connection to RabbitMQ server
- def connect_to_rabbit_mq_server():
- global credentials
- credentials = pika.PlainCredentials(user, password)
- connection = pika.BlockingConnection(pika.ConnectionParameters(
- host=host, port=5672, credentials=credentials)) # open connection
- channel = connection.channel() # get the channel
- channel.queue_declare(queue=queue) # set the queue
- return channel
- # main function
- def run_test(ch, method, properties, body):
- body = json.loads(body)
- print(" [x] Starting Kaspersky test for url: " + body['address']) # print to log
- ch.basic_ack(delivery_tag=method.delivery_tag) # keep alive and send feedback
- test(body)
- def test(body):
- domain = 'http://' + body['address']
- driver = webdriver.Firefox()
- driver.get(domain)
- elem_alert_heading = ''
- try:
- elem_alert_heading = driver.find_element_by_xpath("//*[contains(text(), 'Access denied')]")
- except Exception as e:
- pass
- if elem_alert_heading == '':
- try:
- elem_alert_heading = driver.find_element_by_xpath("//*[contains(text(), 'This website is not safe')]")
- except Exception as e:
- pass
- if 'Kaspersky' in driver.title:
- if elem_alert_heading and elem_alert_heading.text != '':
- print('Kaspersky has reported ' + str(domain) + ' flagged!')
- write_into_csv(str(body['address']), str(body['identity_name']), 'Kaspersky Reporter',
- 'URL has been reported as FLAGGED by Kaspersky!')
- result = update_domain(body)
- if result.ok:
- create_slack_notification(body, True)
- else:
- create_slack_notification(body, False)
- time.sleep(2)
- driver.close()
- def write_into_csv(domain, identity_name, error_code, message):
- with open('kaspersky_reporter_log.csv', mode='a') as reporter_log:
- reporter_writer = csv.writer(reporter_log, delimiter=';', quotechar='"', quoting=csv.QUOTE_MINIMAL)
- utc = pytz.UTC
- now = utc.localize(datetime.datetime.now())
- # time;domain;identity_name;error_code;message
- reporter_writer.writerow([str(now), domain, identity_name, error_code, message])
- # function for making Slack notification
- def create_slack_notification(body, success):
- text = 'Python script detected flag on url: ' + str(body['address']) + ', Indentity: ' + str(body['identity_name']) + ' by Kaspersky!'
- if not success:
- text += ' Database update FAILED'
- post_fields = {
- 'text': 'URL has been reported as FLAGGED by Kaspersky!',
- 'attachments': [
- {
- 'fallback': ' ',
- 'author_name': 'schedule check',
- 'title': 'URL FLAGGED | Kaspersky',
- 'text': text
- }
- ]}
- response = requests.post(
- 'https://hooks.slack.com/services/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', data=json.dumps(post_fields),
- headers={'Content-Type': 'application/json'}
- )
- if response.status_code != 200:
- print('Unsuccessfully created Slack notification!')
- # update flagged domain
- def update_domain(body):
- auth_data = {
- 'grant_type': 'client_credentials',
- 'client_id': 'M6YE13UwxRw9wsvvJ7k0kRiErZ3558F6s64XAxF3',
- 'client_secret': 'PrtByEX7fA8SlztUUXREUYcH5RHbSjnVCGQYtWtd8oEMPif7eT5nWVhHi6Np7I0N6FeeyCkdAjB28LQEX67bLtppOhntCmD0W9lwrOosOFUHriNI7DyEg9c3ZS4cPSca'
- }
- auth_data = {
- 'grant_type': 'password',
- 'client_id': 'coinis_v2_api_testing',
- 'username': 'coinis_v2_api_testing',
- 'password': 'i1k7jlwSV9b8vNrAcpm4xmSuB1ckZ2'
- }
- auth_url = 'https://dash.webb.co.me/api/auth/token/'
- r = requests.post(auth_url, data=auth_data)
- response = json.loads(r.text)
- access_token = response['access_token']
- expires_in = response['expires_in']
- auth_string = 'Bearer ' + access_token
- headers = {
- 'Authorization': auth_string
- }
- now = datetime.datetime.now()
- data = {
- "status": "notok",
- "description": "Flagged by VM Test Kaspersky",
- "flagged_time": str(now.isoformat()),
- "testing_platform_code": "vm_kaspersky"
- }
- r = requests.patch("https://dash.webb.co.me/api/domains/" + str(body['id'] + '/'), headers=headers, data=data)
- return r
- # get domains from database
- def get_domains_from_api():
- auth_data = {
- 'grant_type': 'client_credentials',
- 'client_id': 'M6YE13UwxRw9wsvvJ7k0kRiErZ3558F6s64XAxF3',
- 'client_secret': 'PrtByEX7fA8SlztUUXREUYcH5RHbSjnVCGQYtWtd8oEMPif7eT5nWVhHi6Np7I0N6FeeyCkdAjB28LQEX67bLtppOhntCmD0W9lwrOosOFUHriNI7DyEg9c3ZS4cPSca'
- }
- auth_data = {
- 'grant_type': 'password',
- 'client_id': 'coinis_v2_api_testing',
- 'username': 'coinis_v2_api_testing',
- 'password': 'i1k7jlwSV9b8vNrAcpm4xmSuB1ckZ2'
- }
- auth_url = 'https://dash.webb.co.me/api/auth/token/'
- r = requests.post(auth_url, data=auth_data)
- response = json.loads(r.text)
- access_token = response['access_token']
- expires_in = response['expires_in']
- auth_string = 'Bearer ' + access_token
- headers = {
- 'Authorization': auth_string
- }
- r = requests.get("https://dash.webb.co.me/api/domains/?is_live=1&limit=2000",
- headers=headers)
- result_urls = []
- for result in r.json()['results']:
- result_urls.append(result['address'])
- return result_urls
- # function for loading rabbit MQ
- def load_rabbitmq(data):
- report_connection = pika.BlockingConnection(pika.ConnectionParameters(
- host=host, port=5672, credentials=credentials)) # open connection
- report_channel = report_connection.channel() # get channel
- report_channel.queue_declare(queue=reporter_queue) # set queue
- report_channel.basic_publish(exchange='',
- routing_key=reporter_queue,
- body=data,
- properties=pika.BasicProperties(delivery_mode=2)) # publish message to customers
- report_connection.close() # close connection
- # function for consuming from rabbitmq
- def start_consuming():
- channel = connect_to_rabbit_mq_server()
- channel.basic_qos(prefetch_count=1)
- channel.basic_consume(run_test, queue=queue) # run function when called
- channel.start_consuming() # start consuming
- # main function
- def main():
- data_from_database() # load data from database
- try:
- start_consuming()
- except Exception as e:
- print(e)
- start_consuming()
- main() # run script
- # # main function
- # def main():
- # data_from_database() # load data from database
- #
- # try:
- # start_consuming()
- # except Exception as e:
- # print(e)
- # start_consuming()
- #
- #
- # # main() # run script | THIS IS IF WE USE RABBITMQ
- #
- # # function for running test forever
- # def run_test():
- #
- # while True:
- # run_in_circle()
- # time.sleep(5)
- #
- #
- # # function for testing all domains from api
- # def run_in_circle():
- #
- # domains_from_api = get_domains_from_api()
- # random.shuffle(domains_from_api)
- #
- # test('yoursdealaptitudetheclicks.icu')
- # # for domain in domains_from_api:
- # # test(domain)
- #
- #
- # run_test()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement