Skip to content

Commit fa8f371

Browse files
committed
Add support for NO_COLOR
1 parent a46edb7 commit fa8f371

File tree

1 file changed

+46
-14
lines changed

1 file changed

+46
-14
lines changed

enum4linux-ng.py

+46-14
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99
# 'rpcclient' and 'smbclient'. Other than the original enum4linux.pl, enum4linux-ng parses all output of
1010
# the previously mentioned commands and (if the user requests so), fills the data in JSON/YAML output.
1111
# The original enum4linux.pl had the additional dependencies 'ldapsearch' and 'polenum.py'. These are
12-
# natively implemented in enum4linux-ng. Console output is colored.
12+
# natively implemented in enum4linux-ng. Console output is colored (can be deactivated by setting the
13+
# environment variable NO_COLOR to an arbitrary value).
1314
#
1415
### CREDITS
1516
# I'd like to thank and give credit to the people at former Portcullis Labs (now Cisco CX Security Labs), namely:
@@ -256,15 +257,40 @@
256257
KNOWN_USERNAMES = "administrator,guest,krbtgt,domain admins,root,bin,none"
257258
TIMEOUT = 5
258259

259-
# global_verbose is the only global variable which should be written to
260+
# global_verbose and global_colors should be the only variables which should be written to
260261
global_verbose = False
262+
global_colors = True
261263

262264
class Colors:
263-
reset = '\033[0m'
264-
red = '\033[91m'
265-
green = '\033[92m'
266-
yellow = '\033[93m'
267-
blue = '\033[94m'
265+
ansi_reset = '\033[0m'
266+
ansi_red = '\033[91m'
267+
ansi_green = '\033[92m'
268+
ansi_yellow = '\033[93m'
269+
ansi_blue = '\033[94m'
270+
271+
@classmethod
272+
def red(cls, msg):
273+
if global_colors:
274+
return f"{cls.ansi_red}{msg}{cls.ansi_reset}"
275+
return msg
276+
277+
@classmethod
278+
def green(cls, msg):
279+
if global_colors:
280+
return f"{cls.ansi_green}{msg}{cls.ansi_reset}"
281+
return msg
282+
283+
@classmethod
284+
def yellow(cls, msg):
285+
if global_colors:
286+
return f"{cls.ansi_yellow}{msg}{cls.ansi_reset}"
287+
return msg
288+
289+
@classmethod
290+
def blue(cls, msg):
291+
if global_colors:
292+
return f"{cls.ansi_blue}{msg}{cls.ansi_reset}"
293+
return msg
268294

269295
class Result:
270296
'''
@@ -2456,7 +2482,7 @@ def valid_workgroup(workgroup):
24562482
### Print Functions and Error Processing
24572483

24582484
def print_banner():
2459-
print(f"{Colors.green}ENUM4LINUX - next generation{Colors.reset}\n")
2485+
print(f"{Colors.green('ENUM4LINUX - next generation')}\n")
24602486

24612487
def print_heading(text, leading_newline=True):
24622488
output = f"| {text} |"
@@ -2469,16 +2495,16 @@ def print_heading(text, leading_newline=True):
24692495
print(" " + "="*(length-2))
24702496

24712497
def print_success(msg):
2472-
print(f"{Colors.green}[+] {msg + Colors.reset}")
2498+
print(Colors.green(f"[+] {msg}"))
24732499

24742500
def print_hint(msg):
2475-
print(f"{Colors.green}[H] {msg + Colors.reset}")
2501+
print(Colors.green(f"[H] {msg}"))
24762502

24772503
def print_error(msg):
2478-
print(f"{Colors.red}[-] {msg + Colors.reset}")
2504+
print(Colors.red(f"[-] {msg}"))
24792505

24802506
def print_info(msg):
2481-
print(f"{Colors.blue}[*] {msg + Colors.reset}")
2507+
print(Colors.blue(f"[*] {msg}"))
24822508

24832509
def print_verbose(msg):
24842510
print(f"[V] {msg}")
@@ -2513,11 +2539,11 @@ def abort(msg):
25132539
This function is used to abort the tool run on error.
25142540
The given error message will be printed out and the tool will abort with exit code 1.
25152541
'''
2516-
print(f"{Colors.red}[!] {msg + Colors.reset}")
2542+
print(Colors.red(f"[!] {msg}"))
25172543
sys.exit(1)
25182544

25192545
def warn(msg):
2520-
print(f"\n{Colors.yellow}[!] {msg + Colors.reset}")
2546+
print("\n"+Colors.yellow(f"[!] {msg}"))
25212547

25222548
def yamlize(msg, sort=False, rstrip=True):
25232549
result = yaml.dump(msg, default_flow_style=False, sort_keys=sort, Dumper=Dumper)
@@ -2534,6 +2560,7 @@ def check_arguments():
25342560
'''
25352561

25362562
global global_verbose
2563+
global global_colors
25372564

25382565
parser = ArgumentParser(description="""This tool is a rewrite of Mark Lowe's enum4linux.pl, a tool for enumerating information from Windows and Samba systems.
25392566
It is mainly a wrapper around the Samba tools nmblookup, net, rpcclient and smbclient. Other than the original tool it allows to export enumeration results
@@ -2640,6 +2667,11 @@ def check_dependencies():
26402667
### Run!
26412668

26422669
def main():
2670+
# The user can disable colored output via environment variable NO_COLOR (see https://no-color.org)
2671+
global global_colors
2672+
if "NO_COLOR" in os.environ:
2673+
global_colors = False
2674+
26432675
print_banner()
26442676

26452677
# Check dependencies and process arguments, make sure yaml can handle OrdereDicts

0 commit comments

Comments
 (0)