forked from ErickPO89/PlagueScanner
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwindows_defender-agent.py
75 lines (65 loc) · 2.45 KB
/
windows_defender-agent.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#
# Copyright 2014, 2015 Robert Simmons
#
# This file is part of PlagueScanner.
#
# PlagueScanner is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Foobar is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Foobar. If not, see <http://www.gnu.org/licenses/>.
import configparser
import io
import os
import re
import tempfile
import subprocess
import requests
import zmq
working_dir = os.path.dirname(os.path.realpath(__file__))
config = configparser.ConfigParser()
config_file = os.path.join(working_dir, 'plaguescanner.conf')
config.read(config_file)
port = int(config['PlagueScanner']['Port'])
context = zmq.Context()
socket = context.socket(zmq.REP)
socket.bind('tcp://*:{}'.format(port))
def get_scanner_results(sample):
scanner_output = scan_file(sample)
results = parse_output(scanner_output)
return results
def scan_file(sample):
sample_data = sample.read()
with tempfile.NamedTemporaryFile(delete=True) as fp:
fp.write(sample_data)
scanner = subprocess.Popen(['C:\Program Files\Windows Defender\MpCmdRun.exe', '-Scan', '-ScanType', '3', '-File', fp.name, '-DisableRemediation'], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
stdout, stderr = scanner.communicate()
fp.close()
return stdout
def parse_output(output):
response = {'engine': 'Windows Defender'}
results = re.search(b'Threat\s+: (.+)\s', output, flags=re.MULTILINE)
pest_name = results.group(1).decode(encoding='utf-8').strip()
if pest_name:
response['pest_name'] = pest_name
else:
response['pest_name'] = None
return response
while True:
message = socket.recv_string()
print('Received request: {}'.format(message))
message_parts = re.match('SCAN:(.+):(.+)', message)
file = message_parts.group(2)
response = requests.get('http://{}/{}'.format(config['PlagueScanner']['IP'], file))
sample = io.BytesIO(response.content)
reply = get_scanner_results(sample)
socket.send_json(reply)