From d7aa0b96b319476624acbcee1222e4719a6db189 Mon Sep 17 00:00:00 2001 From: Lucien A Date: Fri, 16 Jun 2023 21:48:51 +0200 Subject: [PATCH] add dash as stdout --- core/generate.py | 24 ++++++++++++++---------- tests/test_generators.py | 28 +++++++++++++++++++++------- weevely.py | 33 ++++++++++++++++++--------------- 3 files changed, 53 insertions(+), 32 deletions(-) diff --git a/core/generate.py b/core/generate.py index bf5be02..3771d19 100644 --- a/core/generate.py +++ b/core/generate.py @@ -1,9 +1,13 @@ -from core.config import agent_templates_folder_path, obfuscators_templates_folder_path +import base64 +import os +import sys + from mako.template import Template -from core.weexceptions import FatalException + from core import messages -import os -import base64 +from core.config import agent_templates_folder_path, obfuscators_templates_folder_path +from core.weexceptions import FatalException + def generate(password, obfuscator = 'phar', agent = 'obfpost_php'): @@ -42,14 +46,14 @@ def generate(password, obfuscator = 'phar', agent = 'obfpost_php'): def save_generated(obfuscated, output): - + b64 = obfuscated[:4] == 'b64:' + final = base64.b64decode(obfuscated[4:]) if b64 else obfuscated.encode('utf-8') try: - if obfuscated[:4] == 'b64:': - with open(output, 'wb') as genfile: - genfile.write(base64.b64decode(obfuscated[4:])) + if output == '-': + sys.stdout.buffer.write(final) else: - with open(output, 'w') as genfile: - genfile.write(obfuscated) + with open(output, 'wb') as outfile: + outfile.write(final) except Exception as e: raise FatalException( messages.generic.error_creating_file_s_s % diff --git a/tests/test_generators.py b/tests/test_generators.py index 0e01120..ee37677 100644 --- a/tests/test_generators.py +++ b/tests/test_generators.py @@ -1,12 +1,17 @@ -from tests.config import base_folder, base_url -from core.generate import generate, save_generated -from core.channels.channel import Channel -from unittest import TestCase -import subprocess -import utils -import random +from contextlib import redirect_stdout import hashlib import os +import random +import subprocess +from contextlib import redirect_stdout +from io import TextIOWrapper, BytesIO +from unittest import TestCase + +import utils +from core.channels.channel import Channel +from core.generate import generate, save_generated +from tests.config import base_folder, base_url + def setUpModule(): subprocess.check_output(""" @@ -22,6 +27,15 @@ def setUpModule(): class TestGenerators(TestCase): def test_generators(self): + with TextIOWrapper(buffer=BytesIO()) as buf, redirect_stdout(buf): + obfuscated = generate('dummy', 'phar') + save_generated(obfuscated, '-') + buf.buffer.seek(0) + output = buf.buffer.read() + + self.assertTrue(output.startswith(b'', output) + for i in range(0, 200): self._randomize_bd() obfuscated = generate(self.password.decode('utf-8'), self.obfuscator) diff --git a/weevely.py b/weevely.py index c08534a..78a7270 100755 --- a/weevely.py +++ b/weevely.py @@ -1,18 +1,19 @@ #!/usr/bin/env python3 -from core.terminal import Terminal -from core.weexceptions import FatalException -from core.loggers import log, dlog -from core.sessions import SessionURL, SessionFile -from core.config import agent_templates_folder_path, obfuscators_templates_folder_path -from core import generate -from core import modules -from core import messages -from core.argparsers import CliParser -import pprint import glob import os +import pprint import sys +from core import generate +from core import messages +from core import modules +from core.argparsers import CliParser +from core.config import agent_templates_folder_path, obfuscators_templates_folder_path +from core.loggers import log, dlog +from core.sessions import SessionURL, SessionFile +from core.terminal import Terminal +from core.weexceptions import FatalException + if sys.stdout.encoding is None: print("Please set PYTHONIOENCODING=UTF-8 running 'export PYTHONIOENCODING=UTF-8' before starting Weevely.") exit(1) @@ -30,11 +31,13 @@ def main(arguments): generate.save_generated(obfuscated, arguments.path) - log.info( - messages.generate.generated_backdoor_with_password_s_in_s_size_i % - (arguments.path, - arguments.password, len(obfuscated)) - ) + if arguments.path != '-': + log.info(messages.generate.generated_backdoor_with_password_s_in_s_size_i % + (arguments.path, + arguments.password, + len(obfuscated) + ) + ) return