Skip to content

Commit 08b0c2e

Browse files
committed
fix: stegano error with odd-length
1 parent 2c28c16 commit 08b0c2e

4 files changed

Lines changed: 33 additions & 31 deletions

File tree

pof/utils/stegano/ipv6encoding.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,8 @@ def encode(cls, string):
3838

3939
for sc in string_chunks:
4040
string_chunk = sc
41-
if len(string_chunk) < cls.IPV6_LEN:
42-
padding = cls.IPV6_LEN - len(string_chunk)
43-
# TODO (deoktr): choose this randomly (anything BUT the padding_byte)
41+
padding = cls.IPV6_LEN - len(string_chunk)
42+
if padding > 0:
4443
string_chunk += b"1"
4544
string_chunk += cls.padding_byte * (padding - 1)
4645

@@ -53,6 +52,11 @@ def encode(cls, string):
5352

5453
ipv6_list.append(ipv6)
5554

55+
if len(hex_string) % cls.IPV6_LEN == 0 and len(hex_string) > 0:
56+
pad_chunk = (b"1" + cls.padding_byte * (cls.IPV6_LEN - 1)).decode()
57+
ipv6_chunks = [pad_chunk[i : i + 4] for i in range(0, len(pad_chunk), 4)]
58+
ipv6_list.append(":".join(ipv6_chunks))
59+
5660
return ipv6_list
5761

5862
@classmethod
@@ -78,7 +82,7 @@ def decode_tokens(cls, encoded_tokens):
7882
"""IPv6 decode tokens.
7983
8084
```
81-
binascii.a2b_hex("".join(["...",]).replace(":", "").strip("0")[:-1])
85+
binascii.a2b_hex("".join(["...",]).replace(":", "").rstrip("0")[:-1])
8286
```.
8387
"""
8488
return [
@@ -100,7 +104,7 @@ def decode_tokens(cls, encoded_tokens):
100104
(STRING, repr("")),
101105
(OP, ")"),
102106
(OP, "."),
103-
(NAME, "strip"),
107+
(NAME, "rstrip"),
104108
(OP, "("),
105109
(STRING, repr(cls.padding_byte.decode())),
106110
(OP, ")"),

pof/utils/stegano/macencoding.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,8 @@ def encode(cls, string):
3737

3838
for sc in string_chunks:
3939
string_chunk = sc
40-
if len(string_chunk) < cls.MAC_LEN:
41-
padding = cls.MAC_LEN - len(string_chunk)
42-
# TODO (deoktr): choose this randomly (anything BUT the padding_byte)
40+
padding = cls.MAC_LEN - len(string_chunk)
41+
if padding > 0:
4342
string_chunk += b"1"
4443
string_chunk += cls.padding_byte * (padding - 1)
4544

@@ -52,6 +51,11 @@ def encode(cls, string):
5251

5352
mac_list.append(mac)
5453

54+
if len(hex_string) % cls.MAC_LEN == 0 and len(hex_string) > 0:
55+
pad_chunk = (b"1" + cls.padding_byte * (cls.MAC_LEN - 1)).decode()
56+
mac_chunks = [pad_chunk[i : i + 2] for i in range(0, len(pad_chunk), 2)]
57+
mac_list.append("-".join(mac_chunks))
58+
5559
return mac_list
5660

5761
@classmethod
@@ -77,7 +81,7 @@ def decode_tokens(cls, encoded_tokens):
7781
"""MAC encoding decode tokens.
7882
7983
```
80-
binascii.a2b_hex("".join(["...",]).replace("-", "").strip("0")[:-1])
84+
binascii.a2b_hex("".join(["...",]).replace("-", "").rstrip("0")[:-1])
8185
```.
8286
"""
8387
return [
@@ -99,7 +103,7 @@ def decode_tokens(cls, encoded_tokens):
99103
(STRING, repr("")),
100104
(OP, ")"),
101105
(OP, "."),
102-
(NAME, "strip"),
106+
(NAME, "rstrip"),
103107
(OP, "("),
104108
(STRING, repr(cls.padding_byte.decode())),
105109
(OP, ")"),

pof/utils/stegano/uuidencoding.py

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,8 @@ def encode(cls, string):
4040

4141
for string_chunk in string_chunks:
4242
sc = string_chunk
43-
if len(sc) < cls.UUID_LEN:
44-
padding = cls.UUID_LEN - len(sc)
45-
# TODO (deoktr): choose this randomly (anything BUT the padding_byte)
43+
padding = cls.UUID_LEN - len(sc)
44+
if padding > 0:
4645
sc += b"1"
4746
sc += cls.padding_byte * (padding - 1)
4847

@@ -58,6 +57,17 @@ def encode(cls, string):
5857

5958
uuid_list.append(uuid)
6059

60+
if len(hex_string) % cls.UUID_LEN == 0 and len(hex_string) > 0:
61+
pc = (b"1" + cls.padding_byte * (cls.UUID_LEN - 1)).decode()
62+
uuid_chunks = [
63+
pc[0:8],
64+
pc[8:12],
65+
pc[12:16],
66+
pc[16:20],
67+
pc[20:],
68+
]
69+
uuid_list.append("-".join(uuid_chunks))
70+
6171
return uuid_list
6272

6373
@classmethod
@@ -83,7 +93,7 @@ def decode_tokens(cls, encoded_tokens):
8393
"""UUID decode tokens.
8494
8595
```
86-
binascii.a2b_hex("".join(["...",]).replace("-", "").strip("0")[:-1])
96+
binascii.a2b_hex("".join(["...",]).replace("-", "").rstrip("0")[:-1])
8797
```
8898
"""
8999
return [
@@ -105,7 +115,7 @@ def decode_tokens(cls, encoded_tokens):
105115
(STRING, '""'),
106116
(OP, ")"),
107117
(OP, "."),
108-
(NAME, "strip"),
118+
(NAME, "rstrip"),
109119
(OP, "("),
110120
(STRING, repr(cls.padding_byte.decode())),
111121
(OP, ")"),

tests/obfuscator/conftest.py

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -97,22 +97,6 @@ class SkipEntry:
9797
"*",
9898
"Fails: replaces inner function names with globals() lookups (KeyError on nested scope)",
9999
),
100-
# TODO: fix all
101-
SkipEntry(
102-
"IPv6Obfuscator",
103-
"*",
104-
"Fails: binascii.Error on odd-length source",
105-
),
106-
SkipEntry(
107-
"UUIDObfuscator",
108-
"*",
109-
"Fails: binascii.Error on odd-length source",
110-
),
111-
SkipEntry(
112-
"MACObfuscator",
113-
"*",
114-
"Fails: binascii.Error on odd-length source",
115-
),
116100
]
117101

118102

0 commit comments

Comments
 (0)