def start_packet_capture(): """ Description : View function to start the packet sniffing program in client machines """ url = settings.BASE_URL + "/" form_data = request.json if request.json else request.form if form_data.get('clients'): for client_id in form_data.get('clients'): doc_url = url + client_id result = retrive_document_details({ 'url': doc_url }) if result.get('response_code') != 200: resp_text = "client details not present for {0}".format(client_id) response_data = { 'post_response': { 'response_text' : resp_text}} resp = make_response(jsonify(response_data), 400) return resp else: document = result.get('document_data') rules = document['rules'] document['status_name'] = 'capture_started' document_args = { 'url': doc_url, 'data' : json.dumps(document), 'override_doc' : True } capture_rules = [] for rule in rules: if rule.get('append_type'): capture_rules.extend( [ rule['rule_name'], rule['rule_value'], rule['append_type'] ]) else: capture_rules.extend( [ rule['rule_name'], rule['rule_value'] ]) args = json.dumps({ 'command_to_send' : 'start_packet_sniffing', 'ip': document['ip'], 'port': settings.client_port, 'capture_rules': capture_rules }) p = subprocess.Popen(['python', 'command_sender.py', args]) p.wait() if p.returncode == 0: # upload the constructed document result = upload_document(document_args) # If the document upload is successful #if not result.get('status'): if not result.get('status') or p.returncode !=0 : resp_text = "Error Happened while starting sniffer for {0}".format(client_id) response_data = { 'post_response': { 'response_text' : resp_text}} resp = make_response(jsonify(response_data), 400) return resp resp_text = "Successfully started packet sniffing for all the selected clients" response_data = { 'post_response': { 'response_text' : resp_text}} resp = make_response(jsonify(response_data), 200) return resp
def add_client(): """ Description : View function to Handles the client addition requests """ if request.method == 'GET': response_data = { 'form' : render_template('add_client.html'), 'response_data': {} } resp = make_response(jsonify(response_data), 200) return resp else: # Construct the client request details # into json document for uploading into the db form_data = request.json if request.json else request.form document_data = { '_id' : form_data.get('host_name'), 'host_name': form_data.get('host_name'), 'details': form_data.get('client_details'), 'ip' : form_data.get('ip'), 'status_name': "new_add" } args = json.dumps({ 'command_to_send' : 'receive_packet_sniffer_file', 'ip': document_data['ip'], 'port': settings.client_port }) p = subprocess.Popen(['python', 'command_sender.py', args]) p.wait() if p.returncode == 0: document_args = { 'url': settings.BASE_URL + "/" + form_data.get('host_name'), 'data' : json.dumps(document_data) } # upload the constructed document result = upload_document(document_args) # If the document upload is successful if result.get('status') and not result.get('already_present'): resp_text = result.get('resp_text').format('client') response_data = { 'post_response': { 'response_text' : resp_text}} resp = make_response(jsonify(response_data), 200) # document upload is not successful else: resp_text = result.get('resp_text').format('client') response_data = { 'post_response': { 'response_text' : resp_text}} resp = make_response(jsonify(response_data), 400) else: resp_text = "Unknown error while reaching client" response_data = { 'post_response': { 'response_text' : resp_text}} resp = make_response(jsonify(response_data), 400) return resp
def add_client_rule(): """ Description : View function to Add the packet sniffing rules of the clients """ url = settings.BASE_URL + "/" form_data = request.json if request.json else request.form if form_data.get('clients'): for client in form_data.get('clients'): doc_url = url + client.get('host_name') result = retrive_document_details({ 'url': doc_url }) if result.get('response_code') != 200: resp_text = "client details not present for {0}".format(client.get('host_name', '')) response_data = { 'post_response': { 'response_text' : resp_text}} resp = make_response(jsonify(response_data), 400) return resp else: document = result.get('document_data') document['rules'] = client.get('rule') document['status_name'] = 'rule_configured' document_args = { 'url': settings.BASE_URL + "/" + document['_id'], 'data' : json.dumps(document), 'override_doc' : True } # upload the constructed document result = upload_document(document_args) # If the document upload is successful #if not result.get('status'): if not result.get('status'): resp_text = "client details rule update failes for {0}".format(rule.get('host_name', '')) response_data = { 'post_response': { 'response_text' : resp_text}} resp = make_response(jsonify(response_data), 400) return resp resp_text = "Successfully updated rule for all the selected clients" response_data = { 'post_response': { 'response_text' : resp_text}} resp = make_response(jsonify(response_data), 200) return resp
while start_count <= max_count: data = data + sender_socket.recv(1024) try: json_data = json.loads(data) except ValueError: logger.debug("Decode error while decoding json data") start_count = start_count + 1 json_data = None continue break if json_data: result = retrive_document_details({ 'url': arguements['url'] }) if result.get('response_code') == 200: document = result.get('document_data') document['stats'] = json_data document_args = { 'url': arguements['url'], 'data' : json.dumps(document), 'override_doc' : True } upload_document(document_args) sender_socket.send("stats received") response = sender_socket.recv(3) if response == "end": logger.debug("Stats are received") sender_socket.close() else: logger.debug("Unknown issue while packet stats")