Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
8f8e075541
|
||
|
|
62c7398b1c
|
||
|
|
182faaf3c6
|
@@ -5,7 +5,7 @@ def read_file(fname):
|
|||||||
return f.read()
|
return f.read()
|
||||||
|
|
||||||
setup(name = "uvscand",
|
setup(name = "uvscand",
|
||||||
version = "0.0.1",
|
version = "0.0.2",
|
||||||
author = "Thomas Oettli",
|
author = "Thomas Oettli",
|
||||||
author_email = "spacefreak@noop.ch",
|
author_email = "spacefreak@noop.ch",
|
||||||
description = "A python daemon to perform virus scans with uvscan (McAfee) over TCP socket.",
|
description = "A python daemon to perform virus scans with uvscan (McAfee) over TCP socket.",
|
||||||
|
|||||||
Executable → Regular
+46
-21
@@ -31,28 +31,37 @@ from subprocess import Popen, PIPE
|
|||||||
uvscan_regex = re.compile(r"Found:?(?: the| potentially unwanted program| (?:virus|trojan) or variant)? (.+?)(?:\.| (?:virus |trojan )?)", re.MULTILINE)
|
uvscan_regex = re.compile(r"Found:?(?: the| potentially unwanted program| (?:virus|trojan) or variant)? (.+?)(?:\.| (?:virus |trojan )?)", re.MULTILINE)
|
||||||
|
|
||||||
|
|
||||||
async def run(uvscan, filename):
|
async def uvscan_worker(queue):
|
||||||
proc = await asyncio.create_subprocess_exec(uvscan, "--secure", "--mime", "--noboot", "--panalyse", "--manalyse", filename, stdout=asyncio.subprocess.PIPE)
|
while True:
|
||||||
stdout, _ = await proc.communicate()
|
job = await queue.get()
|
||||||
os.remove(filename)
|
if job is None:
|
||||||
if proc.returncode == 13:
|
await queue.put(None)
|
||||||
match = uvscan_regex.search(stdout.decode())
|
break
|
||||||
name = match.group(1) if match else "UNKNOWN"
|
uvscan, filename, cb = job
|
||||||
result = "stream: {} FOUND".format(name)
|
proc = await asyncio.create_subprocess_exec(uvscan, "--secure", "--mime", "--noboot", "--panalyse", "--manalyse", filename, stdout=asyncio.subprocess.PIPE)
|
||||||
else:
|
stdout, _ = await proc.communicate()
|
||||||
result = "stream: OK"
|
if proc.returncode == 13:
|
||||||
return result
|
match = uvscan_regex.search(stdout.decode())
|
||||||
|
name = match.group(1) if match else "UNKNOWN"
|
||||||
|
result = "stream: {} FOUND".format(name)
|
||||||
|
else:
|
||||||
|
result = "stream: OK"
|
||||||
|
cb(result)
|
||||||
|
|
||||||
|
|
||||||
class AIO(asyncio.Protocol):
|
class AIO(asyncio.Protocol):
|
||||||
config = None
|
config = None
|
||||||
|
queue = asyncio.Queue()
|
||||||
separator = b"\x00"
|
separator = b"\x00"
|
||||||
|
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
if not AIO.config:
|
if not AIO.config:
|
||||||
raise RuntimeError("configuration not set")
|
raise RuntimeError("configuration not set")
|
||||||
|
if not AIO.queue:
|
||||||
|
raise RuntimeError("queue not set")
|
||||||
self.logger = logging.getLogger(__name__)
|
self.logger = logging.getLogger(__name__)
|
||||||
self.data = bytearray()
|
self.data = bytearray()
|
||||||
|
self.tmpfile = None
|
||||||
|
|
||||||
def connection_made(self, transport):
|
def connection_made(self, transport):
|
||||||
self.peer = transport.get_extra_info("peername")
|
self.peer = transport.get_extra_info("peername")
|
||||||
@@ -79,26 +88,22 @@ class AIO(asyncio.Protocol):
|
|||||||
pos += 1
|
pos += 1
|
||||||
if command == "zINSTREAM":
|
if command == "zINSTREAM":
|
||||||
# save data chunks to temporary file
|
# save data chunks to temporary file
|
||||||
tmpfile = os.path.join(AIO.config["tmpdir"], "uvscan_{}_{}".format(self.request_time, str(self.peer[1])))
|
self.tmpfile = os.path.join(AIO.config["tmpdir"], "uvscan_{}_{}".format(self.request_time, str(self.peer[1])))
|
||||||
self.logger.debug("save data from {} in temporary file {}".format(self.peer, tmpfile))
|
self.logger.debug("save data from {} in temporary file {}".format(self.peer, self.tmpfile))
|
||||||
with open(tmpfile, "wb") as f:
|
with open(self.tmpfile, "wb") as f:
|
||||||
while True:
|
while True:
|
||||||
length = struct.unpack(">I", self.data[pos:pos + 4])[0]
|
length = struct.unpack(">I", self.data[pos:pos + 4])[0]
|
||||||
if length == 0: break
|
if length == 0: break
|
||||||
pos += 4
|
pos += 4
|
||||||
f.write(self.data[pos:pos + length])
|
f.write(self.data[pos:pos + length])
|
||||||
pos += length
|
pos += length
|
||||||
self.logger.debug("starting uvscan for file {}".format(tmpfile))
|
self.logger.debug("starting uvscan for file {}".format(self.tmpfile))
|
||||||
task = asyncio.async(run(AIO.config["uvscan_path"], tmpfile))
|
asyncio.async(AIO.queue.put((AIO.config["uvscan_path"], self.tmpfile, self.send_response)))
|
||||||
task.add_done_callback(self.handle_uvscan_result)
|
|
||||||
else:
|
else:
|
||||||
raise RuntimeError("unknown command")
|
raise RuntimeError("unknown command")
|
||||||
except (RuntimeError, IndexError, IOError, struct.error) as e:
|
except (RuntimeError, IndexError, IOError, struct.error) as e:
|
||||||
self.send_response(str(e))
|
self.send_response(str(e))
|
||||||
|
|
||||||
def handle_uvscan_result(self, task):
|
|
||||||
self.send_response(task.result())
|
|
||||||
|
|
||||||
def send_response(self, response):
|
def send_response(self, response):
|
||||||
response = response.encode()
|
response = response.encode()
|
||||||
response += AIO.separator
|
response += AIO.separator
|
||||||
@@ -106,6 +111,12 @@ class AIO(asyncio.Protocol):
|
|||||||
self.transport.write(response)
|
self.transport.write(response)
|
||||||
self.transport.close()
|
self.transport.close()
|
||||||
|
|
||||||
|
def connection_lost(self, exc):
|
||||||
|
if self.tmpfile:
|
||||||
|
self.logger.debug("removing temporary file {}".format(self.tmpfile))
|
||||||
|
os.remove(self.tmpfile)
|
||||||
|
self.logger.debug("closed connection to {}".format(self.peer))
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
"Run uvscand."
|
"Run uvscand."
|
||||||
@@ -114,6 +125,8 @@ def main():
|
|||||||
formatter_class=lambda prog: argparse.HelpFormatter(prog, max_help_position=45, width=140))
|
formatter_class=lambda prog: argparse.HelpFormatter(prog, max_help_position=45, width=140))
|
||||||
parser.add_argument("-c", "--config", help="List of config files to read.", nargs="+",
|
parser.add_argument("-c", "--config", help="List of config files to read.", nargs="+",
|
||||||
default=["/etc/uvscand.conf"])
|
default=["/etc/uvscand.conf"])
|
||||||
|
parser.add_argument("-m", "--maxprocs", help="Maximum number of parallel scan processes.",
|
||||||
|
type=int, default=8)
|
||||||
parser.add_argument("-d", "--debug", help="Log debugging messages.", action="store_true")
|
parser.add_argument("-d", "--debug", help="Log debugging messages.", action="store_true")
|
||||||
args = parser.parse_args()
|
args = parser.parse_args()
|
||||||
|
|
||||||
@@ -163,8 +176,15 @@ def main():
|
|||||||
if not os.path.isfile(config["uvscan_path"]) or not os.access(config["uvscan_path"], os.X_OK):
|
if not os.path.isfile(config["uvscan_path"]) or not os.access(config["uvscan_path"], os.X_OK):
|
||||||
logger.error("uvscan binary '{}' does not exist or is not executable".format(config["uvscan_path"]))
|
logger.error("uvscan binary '{}' does not exist or is not executable".format(config["uvscan_path"]))
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
loop = asyncio.get_event_loop()
|
|
||||||
|
# setup protocol
|
||||||
AIO.config = config
|
AIO.config = config
|
||||||
|
|
||||||
|
# start uvscan workers
|
||||||
|
loop = asyncio.get_event_loop()
|
||||||
|
workers = [loop.create_task(uvscan_worker(AIO.queue)) for _ in range(args.maxprocs)]
|
||||||
|
|
||||||
|
# start server
|
||||||
coro = loop.create_server(AIO, config["bind_address"], config["bind_port"])
|
coro = loop.create_server(AIO, config["bind_address"], config["bind_port"])
|
||||||
server = loop.run_until_complete(coro)
|
server = loop.run_until_complete(coro)
|
||||||
logger.info("uvscand started")
|
logger.info("uvscand started")
|
||||||
@@ -174,8 +194,13 @@ def main():
|
|||||||
except KeyboardInterrupt:
|
except KeyboardInterrupt:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
# close server
|
||||||
server.close()
|
server.close()
|
||||||
loop.run_until_complete(server.wait_closed())
|
loop.run_until_complete(server.wait_closed())
|
||||||
|
|
||||||
|
# shutdown uvscan workers
|
||||||
|
loop.run_until_complete(AIO.queue.put(None))
|
||||||
|
loop.run_until_complete(asyncio.wait(workers))
|
||||||
loop.close()
|
loop.close()
|
||||||
logger.info("uvscand stopped")
|
logger.info("uvscand stopped")
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user