Skip to content

Commit

Permalink
add dash as stdout
Browse files Browse the repository at this point in the history
  • Loading branch information
ZanyMonk authored and epinna committed Jun 18, 2023
1 parent 2dfda54 commit d7aa0b9
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 32 deletions.
24 changes: 14 additions & 10 deletions core/generate.py
Original file line number Diff line number Diff line change
@@ -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'):
Expand Down Expand Up @@ -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 %
Expand Down
28 changes: 21 additions & 7 deletions tests/test_generators.py
Original file line number Diff line number Diff line change
@@ -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("""
Expand All @@ -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'<?php'))
self.assertIn(b'__HALT_COMPILER(); ?>', output)

for i in range(0, 200):
self._randomize_bd()
obfuscated = generate(self.password.decode('utf-8'), self.obfuscator)
Expand Down
33 changes: 18 additions & 15 deletions weevely.py
Original file line number Diff line number Diff line change
@@ -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)
Expand All @@ -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

Expand Down

0 comments on commit d7aa0b9

Please sign in to comment.