sqlmap/thirdparty/ansistrm/ansistrm.py

217 lines
8.0 KiB
Python
Raw Normal View History

2012-07-14 18:53:15 +04:00
#
# Copyright (C) 2010-2012 Vinay Sajip. All rights reserved. Licensed under the new BSD license.
# (Note: 2018 modifications by @stamparm)
2012-07-14 18:53:15 +04:00
#
2012-07-14 18:53:15 +04:00
import logging
import os
import re
2016-01-14 00:47:42 +03:00
import sys
2012-07-14 18:53:15 +04:00
2019-03-27 04:08:43 +03:00
from lib.core.settings import IS_WIN
2012-07-31 13:03:44 +04:00
2019-03-27 04:08:43 +03:00
if IS_WIN:
2016-01-14 00:47:42 +03:00
import ctypes
import ctypes.wintypes
# Reference: https://gist.github.com/vsajip/758430
# https://github.com/ipython/ipython/issues/4252
# https://msdn.microsoft.com/en-us/library/windows/desktop/ms686047%28v=vs.85%29.aspx
ctypes.windll.kernel32.SetConsoleTextAttribute.argtypes = [ctypes.wintypes.HANDLE, ctypes.wintypes.WORD]
ctypes.windll.kernel32.SetConsoleTextAttribute.restype = ctypes.wintypes.BOOL
2019-05-20 12:21:31 +03:00
def stdoutEncode(data): # Cross-referenced function
2019-06-19 00:16:53 +03:00
return data
2016-01-14 00:47:42 +03:00
2012-07-14 18:53:15 +04:00
class ColorizingStreamHandler(logging.StreamHandler):
# color names to indices
color_map = {
'black': 0,
'red': 1,
'green': 2,
'yellow': 3,
'blue': 4,
'magenta': 5,
'cyan': 6,
'white': 7,
}
# levels to (background, foreground, bold/intense)
2016-01-14 00:47:42 +03:00
level_map = {
logging.DEBUG: (None, 'blue', False),
logging.INFO: (None, 'green', False),
logging.WARNING: (None, 'yellow', False),
logging.ERROR: (None, 'red', False),
logging.CRITICAL: ('red', 'white', False)
}
2012-07-14 18:53:15 +04:00
csi = '\x1b['
reset = '\x1b[0m'
2018-07-06 17:18:04 +03:00
bold = "\x1b[1m"
2012-08-16 00:31:25 +04:00
disable_coloring = False
2012-07-14 18:53:15 +04:00
@property
def is_tty(self):
isatty = getattr(self.stream, 'isatty', None)
2012-08-16 00:31:25 +04:00
return isatty and isatty() and not self.disable_coloring
2012-07-14 18:53:15 +04:00
def emit(self, record):
try:
2019-05-20 12:21:31 +03:00
message = stdoutEncode(self.format(record))
2012-07-14 18:53:15 +04:00
stream = self.stream
if not self.is_tty:
2012-08-07 12:57:29 +04:00
if message and message[0] == "\r":
message = message[1:]
2012-07-14 18:53:15 +04:00
stream.write(message)
else:
self.output_colorized(message)
stream.write(getattr(self, 'terminator', '\n'))
self.flush()
except (KeyboardInterrupt, SystemExit):
raise
except IOError:
pass
2012-07-14 18:53:15 +04:00
except:
self.handleError(record)
2019-03-27 04:08:43 +03:00
if not IS_WIN:
2012-07-14 18:53:15 +04:00
def output_colorized(self, message):
self.stream.write(message)
else:
ansi_esc = re.compile(r'\x1b\[((?:\d+)(?:;(?:\d+))*)m')
nt_color_map = {
0: 0x00, # black
1: 0x04, # red
2: 0x02, # green
3: 0x06, # yellow
4: 0x01, # blue
5: 0x05, # magenta
6: 0x03, # cyan
7: 0x07, # white
}
def output_colorized(self, message):
parts = self.ansi_esc.split(message)
h = None
fd = getattr(self.stream, 'fileno', None)
if fd is not None:
fd = fd()
if fd in (1, 2): # stdout or stderr
h = ctypes.windll.kernel32.GetStdHandle(-10 - fd)
while parts:
text = parts.pop(0)
if text:
self.stream.write(text)
self.stream.flush()
2012-07-14 18:53:15 +04:00
if parts:
params = parts.pop(0)
if h is not None:
params = [int(p) for p in params.split(';')]
color = 0
for p in params:
if 40 <= p <= 47:
color |= self.nt_color_map[p - 40] << 4
elif 30 <= p <= 37:
color |= self.nt_color_map[p - 30]
elif p == 1:
color |= 0x08 # foreground intensity on
elif p == 0: # reset to default color
color = 0x07
else:
pass # error condition ignored
ctypes.windll.kernel32.SetConsoleTextAttribute(h, color)
2018-09-14 10:45:04 +03:00
def _reset(self, message):
if not message.endswith(self.reset):
reset = self.reset
elif self.bold in message: # bold
reset = self.reset + self.bold
else:
reset = self.reset
return reset
def colorize(self, message, levelno):
if levelno in self.level_map and self.is_tty:
bg, fg, bold = self.level_map[levelno]
2012-07-14 18:53:15 +04:00
params = []
if bg in self.color_map:
params.append(str(self.color_map[bg] + 40))
if fg in self.color_map:
params.append(str(self.color_map[fg] + 30))
if bold:
params.append('1')
if params and message:
2018-07-06 17:22:19 +03:00
match = re.search(r"\A(\s+)", message)
prefix = match.group(1) if match else ""
message = message[len(prefix):]
2012-07-14 18:53:15 +04:00
2018-07-06 17:18:04 +03:00
match = re.search(r"\[([A-Z ]+)\]", message) # log level
if match:
level = match.group(1)
2018-07-06 17:18:04 +03:00
if message.startswith(self.bold):
message = message.replace(self.bold, "")
reset = self.reset + self.bold
params.append('1')
else:
reset = self.reset
message = message.replace(level, ''.join((self.csi, ';'.join(params), 'm', level, reset)), 1)
2018-07-06 17:18:04 +03:00
match = re.search(r"\A\s*\[([\d:]+)\]", message) # time
if match:
time = match.group(1)
2018-09-14 10:45:04 +03:00
message = message.replace(time, ''.join((self.csi, str(self.color_map["cyan"] + 30), 'm', time, self._reset(message))), 1)
2018-07-06 17:18:04 +03:00
match = re.search(r"\[(#\d+)\]", message) # counter
if match:
counter = match.group(1)
2018-09-14 10:45:04 +03:00
message = message.replace(counter, ''.join((self.csi, str(self.color_map["yellow"] + 30), 'm', counter, self._reset(message))), 1)
2018-07-18 18:00:34 +03:00
if level != "PAYLOAD":
2018-09-06 14:59:07 +03:00
if any(_ in message for _ in ("parsed DBMS error message",)):
2018-10-03 12:02:13 +03:00
match = re.search(r": '(.+)'", message)
if match:
string = match.group(1)
message = message.replace("'%s'" % string, "'%s'" % ''.join((self.csi, str(self.color_map["white"] + 30), 'm', string, self._reset(message))), 1)
2018-09-06 14:59:07 +03:00
else:
2019-06-01 13:22:59 +03:00
match = re.search(r"\bresumed: '(.+\.\.\.)", message)
2019-05-25 00:51:58 +03:00
if match:
2018-09-06 14:59:07 +03:00
string = match.group(1)
2019-06-01 13:22:59 +03:00
message = message.replace("'%s" % string, "'%s" % ''.join((self.csi, str(self.color_map["white"] + 30), 'm', string, self._reset(message))), 1)
2019-05-25 00:51:58 +03:00
else:
2019-06-01 13:22:59 +03:00
match = re.search(r" \('(.+)'\)\Z", message)
if match:
2019-05-25 00:51:58 +03:00
string = match.group(1)
message = message.replace("'%s'" % string, "'%s'" % ''.join((self.csi, str(self.color_map["white"] + 30), 'm', string, self._reset(message))), 1)
2019-06-01 13:22:59 +03:00
else:
for match in re.finditer(r"[^\w]'([^']+)'", message): # single-quoted
string = match.group(1)
message = message.replace("'%s'" % string, "'%s'" % ''.join((self.csi, str(self.color_map["white"] + 30), 'm', string, self._reset(message))), 1)
2018-07-06 17:18:04 +03:00
else:
message = ''.join((self.csi, ';'.join(params), 'm', message, self.reset))
2012-07-14 18:53:15 +04:00
2018-07-06 17:18:04 +03:00
if prefix:
message = "%s%s" % (prefix, message)
2018-07-05 15:21:32 +03:00
2018-09-14 10:45:04 +03:00
message = message.replace("%s]" % self.bold, "]%s" % self.bold) # dirty patch
2012-07-14 18:53:15 +04:00
return message
def format(self, record):
message = logging.StreamHandler.format(self, record)
return self.colorize(message, record.levelno)