-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathprinter.py
More file actions
216 lines (155 loc) · 5.08 KB
/
Copy pathprinter.py
File metadata and controls
216 lines (155 loc) · 5.08 KB
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
"""printer.py
Various functions related to printing.
"""
import builtins
from random import choice
import re
from time import perf_counter
import winreg
import config
from extras import (
BANNERS,
BOLD,
BACKGROUND,
BLACK_OR_WHITE,
FOREGROUND,
PERLISISMS,
RESET,
UNDERLINE,
)
def indent(message):
"""Indent message."""
return f"{config.INDENT}{message}"
def printf(*objects, **kwargs):
"""Overload builtins.print() with `tee'-like feature and timestamp."""
try:
with open(f"{config.WORK_DIR}\\{config.TRACE}", "a", encoding="utf-8") as file:
if config.TIMESTAMP:
timestamp = f"[{perf_counter():10.3f}]"
builtins.print(
config.SPACE.join([timestamp, strip(*objects)]), file=file, **kwargs
)
builtins.print(
config.SPACE.join([timestamp, *objects]), flush=True, **kwargs
)
else:
builtins.print(
config.SPACE.join(["", strip(*objects)]), file=file, **kwargs
)
builtins.print(config.SPACE.join(["", *objects]), flush=True, **kwargs)
except FileNotFoundError:
builtins.print(*objects, **kwargs)
def status(message):
"""Marker: (**) status"""
log(f"(**) {message}")
def info(message):
"""Marker: (II) informational"""
log(f"(II) {message}")
def error(message):
"""Marker: (EE) error"""
log(f"(EE) {message}")
def param(name, value):
"""Print parameter and its value."""
log(f"|-- ({name.center(config.FIXED_WIDTH)}) => {value}")
def log(message):
"""Print message based on its content to console."""
def split_call(message):
"""Split call into image and symbol."""
match = re.match(r"Call: (.*)!(.*)\(\)", message)
if match is not None:
image, symbol = match.groups()
return (image.strip(), symbol.strip())
def split_param(message):
"Split parameter into name and value."
match = re.match(r"\s*\|-- \(\s*(.*)\s*\) => (.*)", message)
if match is not None:
name, value = match.groups()
return (name.strip(), value.strip())
if message is None:
return
if "Call:" in message:
image, symbol = split_call(message)
call = {"image": image, "symbol": symbol, "params": {}}
config.JSON_OUTPUT["trace"].append(call)
elif "|--" in message:
name, value = split_param(message)
match = re.match(r"^\d+$", value)
if match is not None:
value = int(value)
if "Action" in name or "Access" in name:
config.JSON_OUTPUT["trace"][-1][name.lower()] = value.lower()
else:
params = config.JSON_OUTPUT["trace"][-1]["params"]
params[name.lower()] = value
if config.FUN:
if "|" in message:
message = message.replace("|", fun("|"))
message = message.replace("--", fun("--"))
if re.match(r"^(\(\*\*\)|Call)", message):
printf(message)
else:
printf(indent(message))
def has_ansi_colors():
"""Check for ANSI colors support."""
supported = False
try:
key = winreg.OpenKey(winreg.HKEY_CURRENT_USER, "Console")
data = winreg.QueryValueEx(key, "VirtualTerminalLevel")[0]
if data == 1:
supported = True
except FileNotFoundError:
pass
return supported
def print_banner():
"""Print WSHooker banner."""
banner = choice(BANNERS)
if config.FUN:
builtins.print(f"{fun(banner)}")
else:
builtins.print(f"{highlight(banner)}")
def print_trace_label(label="Trace"):
"""Print label with a border to indicate the start of a trace."""
def border(label):
return f"+-{'-' * len(label)}-+"
border = border(label)
printf(border)
if config.FUN:
label = ransomize(label)
printf(f"| {label} |")
printf(border)
def bold(text):
"""Bolds text."""
if config.ANSI_COLORS:
text = f"{BOLD}{text}{RESET}"
return text
def fun(text):
"""Rainbow text."""
if config.ANSI_COLORS:
text = "".join([f"{choice(FOREGROUND)}{x}{RESET}" for x in [*text]])
return text
def ransomize(text):
"""Ransomize text like those found in ransom notes."""
def _ransomize(text):
if config.ANSI_COLORS:
text = "".join(
[f"{choice(BACKGROUND)}{BLACK_OR_WHITE[1]}{x}{RESET}" for x in [*text]]
)
return text
return " ".join([_ransomize(word) for word in text.split(" ")])
def epigram():
"""Return a random Perlisism."""
return choice(PERLISISMS)
def highlight(text):
"""Highlights text with a random foreground color."""
if config.ANSI_COLORS:
text = f"{choice(FOREGROUND)}{text}{RESET}"
return text
def underline(text):
"""Underlines text."""
if config.ANSI_COLORS:
text = f"{UNDERLINE}{text}{RESET}"
return text
def strip(text):
"""Strip ANSI escape sequences from text."""
text = re.sub(r"\033\[\d+m", "", text)
return text