Skip to content

Commit

Permalink
Merge pull request #8 from daniel752/daniel752-patch-2
Browse files Browse the repository at this point in the history
Program now clears logs even if an error occurs
  • Loading branch information
daniel752 authored Jun 8, 2023
2 parents d2f6107 + 723aea7 commit 1e390c3
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 8 deletions.
23 changes: 16 additions & 7 deletions bbis_hide.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ def get_arguments_cli():
# parser.add_argument('-a', '--action', type=str, required=True, help='-a or --action [=decode,=extract] (choose whether you want to decode or extract data)')
parser.add_argument('-d', '--data', type=str, required=True, help='Path to data (file) to hide in executable')
parser.add_argument('-e', '--executable', type=str, required=True, help='Path to executable to hide file within')
parser.add_argument('-p', '--path', type=str, required=False, help='Path of where to put te modified executable')
return parser.parse_args()


Expand Down Expand Up @@ -100,7 +101,7 @@ def get_opcode_conversion(opcode, param):
return int(map_dict.get(param).get(opcode).split()[0]), int(map_dict.get(param).get(opcode).split()[1])


def decode_data_within_executable(buffer, binary_data, offsets):
def decode_data_within_executable(buffer, binary_data, offsets, exe_name=None):
"""Gets buffer and binary input to decode inside buffer
:param buffer: Buffer for executable file.
:param binary_data: File's binary representation to decode inside executable (buffer).
Expand Down Expand Up @@ -133,18 +134,23 @@ def decode_data_within_executable(buffer, binary_data, offsets):
print('The code section in this executable is not enough to hide this message.')
print(f'Still {len(binary_data) - i} bits left to hide.')
print('Program exits')
clear_logs(exe_name)
exit(1)

return buffer


def write_buffer(buffer, executable):
def write_buffer(buffer, executable, path):
"""Write buffer back to hard-disk (physical memory) in current directory"""
if path:
if path[-1] == '/':
path = path[0:-1]
executable = f"{path}/{executable}"
with open(f"{executable}", "wb") as file:
file.write(buffer)


def modify_buffer(buffer, binary_data, offsets_list):
def modify_buffer(buffer, binary_data, offsets_list, exe_name=None):
"""Modify buffer (executable file) according to binary input (file).
:param buffer: executable file to modify.
:param binary_data: binary string to hide inside executable file.
Expand All @@ -159,14 +165,15 @@ def modify_buffer(buffer, binary_data, offsets_list):
except IndexError:
print(f"Not enough offsets to decode bits with, need executable with bigger code section")
print(f"Program exits")
clear_logs(exe_name)
exit(1)

# print(f"Start offset: {int(start_binary,2)}")
# print(f"End offset: {int(end_binary, 2)}")
# Concatenate binary end mark with binary data
full_binary_data = end_binary + binary_data
# Decode 'full_binary_data' within executable file
decode_data_within_executable(buffer, full_binary_data, offsets)
decode_data_within_executable(buffer, full_binary_data, offsets, exe_name)
return buffer


Expand All @@ -184,17 +191,19 @@ def clear_logs(exe_name):
data = args.data
# Path to executable file
executable = args.executable
# Path of modified executable
path = args.path
# Get targeted mnemonics offsets from executable's object data
offsets_list = get_executable_offsets(executable)
# Get file's binary data
binary_data = get_file_binary_data(data)
# Load executable's data into buffer
buffer = get_executable_binary(executable)
# Modify buffer according to 'binary_data'
buffer = modify_buffer(buffer, binary_data, offsets_list)
# Get executable's name from path
exe_name = os.path.basename(executable)
# Modify buffer according to 'binary_data'
buffer = modify_buffer(buffer, binary_data, offsets_list, exe_name)
# Write modified buffer back to hard-disk (looks exactly like original)
write_buffer(buffer, exe_name)
write_buffer(buffer, exe_name, path)
# Delete executable's object data logs
clear_logs(exe_name)
2 changes: 1 addition & 1 deletion testing/test.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
Hello world Hello world Hello world Hello world Hello world Hello world Hello world Hello world Hello world Hello world Hello world Hello world Hello world Hello world Hello world Hello world Hello world Hello world Hello world Hello world Hello world Hello world Hello world Hello world Hello world Hello world
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA

0 comments on commit 1e390c3

Please sign in to comment.