-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathniernen.py
174 lines (121 loc) · 6.2 KB
/
niernen.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
import random
# Color codes
RED = "\033[1;31m"
GREEN = "\033[1;32m"
RESET = "\033[0;0m"
# Encode shellcode using XOR, ADD, SUB, ROL, and ROR
def encode_shellcode(shellcode, key):
encoded_shellcode = bytearray()
for i, byte in enumerate(shellcode):
# XOR
xored_byte = byte ^ key[i % len(key)]
# ADD
added_byte = (xored_byte + key[(i + 1) % len(key)]) % 256
# SUB
subbed_byte = (added_byte - key[(i + 2) % len(key)]) % 256
# ROL
rolled_byte = ((subbed_byte << 1) | (subbed_byte >> 7)) % 256
# ROR
ror_byte = ((rolled_byte >> 1) | (rolled_byte << 7)) % 256
encoded_shellcode.append(ror_byte)
return bytes(encoded_shellcode)
# Decode shellcode using ROR, ROL, SUB, ADD, and XOR
def decode_shellcode(encoded_shellcode, key):
decoded_shellcode = bytearray()
for i, byte in enumerate(encoded_shellcode):
# ROR
ror_byte = ((byte >> 7) | (byte << 1)) % 256
# ROL
rolled_byte = ((ror_byte >> 1) | (ror_byte << 7)) % 256
# SUB
subbed_byte = (rolled_byte + key[(i + 2) % len(key)]) % 256
# ADD
added_byte = (subbed_byte - key[(i + 1) % len(key)]) % 256
# XOR
xored_byte = added_byte ^ key[i % len(key)]
decoded_shellcode.append(xored_byte)
return bytes(decoded_shellcode)
# Generate a random key of given length and convert it into bytes
def generate_key(length):
return bytes([random.randint(0, 255) for i in range(length)])
def info():
print(f"""{GREEN}
_ _________ _______ _______ _ _______ _
( ( /| \__ __/ ( ____ \ ( ____ ) ( ( /| ( ____ \ ( ( /|
| \ ( | ) ( | ( \/ | ( )| | \ ( | | ( \/ | \ ( |
| \ | | | | | (__ | (____)| | \ | | | (__ | \ | |
| (\ \) | | | | __) | __) | (\ \) | | __) | (\ \) |
| | \ | | | | ( | (\ ( | | \ | | ( | | \ |
| ) \ | ___) (___ | (____/\ | ) \ \__ | ) \ | | (____/\ | ) \ |
|/ )_) \_______/ (_______/ |/ \__/ |/ )_) (_______/ |/ )_)
{RESET} {RED}
Author: Abhijeet Kumar
Github: github.com/wand3rlust
{RESET}""")
# Menu options
def main():
info()
while True:
print(f"\n"
f"{GREEN}1. Encode shellcode\n")
print("2. Decode shellcode\n")
print("3. Exit\n")
choice = input(f"Enter your choice [1-3]: {RESET}")
if choice == "1":
plaintext_shellcode = input("\nEnter plaintext shellcode: ")
# Encode the user input into UTF-8 and change from string to byte
shellcode = plaintext_shellcode.encode()
# Generate same length key as shellcode hex
key = generate_key(len(shellcode))
# Call encode_shellcode function with 2 arguments i.e, UTF-8 shellcode and key
encoded_shellcode = encode_shellcode(shellcode, key)
print(f"\nOriginal shellcode (in hex): {RED}", shellcode.hex())
print(f"\n{RESET}Key (in hex): {RED}", key.hex())
print(f"\n{RESET}Encoded shellcode (in hex): {RED}", encoded_shellcode.hex())
# Convert byte format to string
encoded_shellcode = encoded_shellcode.hex()
# Append \x after every 2nd character
encoded_shellcode = "\\x" + "\\x".join(encoded_shellcode[i:i + 2]
for i in range(0, len(encoded_shellcode), 2))
print(f"\n{RESET}Encoded shellcode (with \\x): {RED}", encoded_shellcode)
print(f"{RESET}\n")
elif choice == "2":
print(f"\n{GREEN}Choose the hex format: ")
print("\na. Normal hex")
print("\nb. \\x format")
choice = input(f"\nEnter your choice [a-b]: {RESET}")
if choice == "a":
# Convert user input hex to byte
encoded_shellcode = bytes.fromhex(input("\nEnter encoded shellcode (in hex): "))
# Convert user input key into byte
key = bytes.fromhex(input("\nEnter key (in hex): "))
# Call decode_shellcode function with 2 arguments i.e, shellcode and key
decoded_shellcode = decode_shellcode(encoded_shellcode, key)
# Convert encoded shellcode to hex and display
print(f"\nEncoded shellcode (in hex): {RED}", encoded_shellcode.hex())
# Convert key to hex and display
print(f"\n{RESET}Key (in hex): {RED}", key.hex())
# Convert decoded shellcode to hex and display
print(f"\n{RESET}Decoded shellcode (in hex): {RED}", decoded_shellcode.hex())
print("\n")
elif choice == "b":
encoded_shellcode = input("\nEnter encoded shellcode (with \\x): ")
# Remove \x from hex shellcode and convert to byte
encoded_shellcode = bytes.fromhex(encoded_shellcode.replace("\\x", ""))
# Convert encoded shellcode to hex and display
print(f"\nEncoded shellcode (without \\x): {RED}", encoded_shellcode.hex())
# Convert user input key into byte
key = bytes.fromhex(input(f"\n{RESET}Enter key (in hex): "))
# Call decode_shellcode function with 2 arguments i.e, shellcode and key
decoded_shellcode = decode_shellcode(encoded_shellcode, key)
print(f"\nDecoded shellcode (in hex): {RED}", decoded_shellcode.hex())
print(f"{RESET}\n")
else:
print(f"{RED}Invalid selection, start again{RESET}\n")
elif choice == "3":
print(f"\n{GREEN}Sayonara...{RESET}\n")
break
else:
print(f"\n{RED}404: choice not found{RESET}\n")
if __name__ == "__main__":
main()