Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 16 additions & 9 deletions batch_deobfuscator/batch_interpreter.py
Original file line number Diff line number Diff line change
Expand Up @@ -980,7 +980,7 @@ def normalize_command(self, command):
stack.append(state)
state = "var_s"
elif char == "!":
variable_start = len(normalized_com)
variable_start_ex = len(normalized_com)
normalized_com += char
stack.append(state)
state = "var_s_2"
Expand All @@ -996,7 +996,7 @@ def normalize_command(self, command):
stack.append("str_s")
state = "var_s" # seen %
elif char == "!":
variable_start = len(normalized_com)
variable_start_ex = len(normalized_com)
normalized_com += char
stack.append("str_s")
state = "var_s_2" # seen !
Expand Down Expand Up @@ -1043,15 +1043,20 @@ def normalize_command(self, command):
elif state == "var_s_2":
if char == "!" and normalized_com[-1] != char:
normalized_com += char
value = self.get_value(normalized_com[variable_start:])
normalized_com = normalized_com[:variable_start]
value = self.get_value(normalized_com[variable_start_ex:])
normalized_com = normalized_com[:variable_start_ex]
if len(normalized_com) == 0:
traits["start_with_var"] = True
normalized_com += self.normalize_command(value)
traits["var_used"] += 1
state = stack.pop()
elif char == "!":
normalized_com += char
elif char == "%":
variable_start = len(normalized_com)
normalized_com += char
state = "var_s"
stack.append("var_s_2")
elif char == "^":
state = "escape"
stack.append("var_s_2")
Expand All @@ -1077,20 +1082,22 @@ def normalize_command(self, command):
state = "var_s"
elif char == "!":
if state == "var_s_2":
value = self.get_value(normalized_com[variable_start:])
normalized_com = normalized_com[:variable_start]
value = self.get_value(normalized_com[variable_start_ex:])
normalized_com = normalized_com[:variable_start_ex]
if len(normalized_com) == 0:
traits["start_with_var"] = True
normalized_com += self.normalize_command(value)
traits["var_used"] += 1
state = stack.pop()
else:
variable_start = len(normalized_com) - 1
variable_start_ex = len(normalized_com) - 1
stack.append(state)
state = "var_s_2"

if state in ["var_s", "var_s_2"]:
normalized_com = normalized_com[:variable_start] + normalized_com[variable_start + 1 :]
if state == "var_s":
normalized_com = normalized_com[:variable_start] + normalized_com[variable_start + 1:]
elif state == "var_s_2":
normalized_com = normalized_com[:variable_start_ex] + normalized_com[variable_start_ex + 1:]
elif state == "escape":
normalized_com += "^"

Expand Down
Empty file added tests/__init__.py
Empty file.
1 change: 1 addition & 0 deletions tests/test_unittests.py
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,7 @@ def test_simple_set_a():
("set EXP=43", "echo ^!EX^P!", "echo 43"),
# ("set EXP=43", "echo ^%EXP^%", "echo 43"), # That's wrong... it actually prints the next line. Ignoring.
("set EXP=43", "echo ^!EXP^!", "echo 43"),
("set EXP=43", "echo !EX%nothing%P!", "echo 43"),
],
)
def test_set_command(var, echo, result):
Expand Down