def _write_string(self, text, direction, timestamp, encoding=None): colorTag = '' # Print TimestampcolorTag if self._COLORMODE == 'HTML' and timestamp != None: self._htmlwrite('<div class="timestamp">\n%s UTC:</div>' % datetime.datetime.utcfromtimestamp(timestamp)) #if self._hexmode: self._htmlwrite("<br>") elif self._COLORMODE == 'TTY' and self._timemode and timestamp != None: self.fh.write('\x1b[36m%s UTC:\x1b[0m\n' % datetime.datetime.utcfromtimestamp(timestamp)) # Set Direction if direction.lower() == 'cs': if self._COLORMODE == 'HTML': self._htmlwrite('<span style="color:red;">') elif self._COLORMODE == 'TTY': colorTag = '\x1b[0;31m' elif direction.lower() == 'sc': if self._COLORMODE == 'HTML': self._htmlwrite('<span style="color:blue;">') elif self._COLORMODE == 'TTY': colorTag = '\x1b[0;32m' # Hex Mode Data if self._hexmode: # Hex Output dlen = len(text) if direction.lower() == 'cs': msgOffset = self._CS_offset self._CS_offset += dlen elif direction.lower() == 'sc': msgOffset = self._SC_offset self._SC_offset += dlen else: msgOffset = self._NODIR_offset self._NODIR_offset += dlen text = util.hexPlusAscii(str(text), 16, msgOffset) if self._COLORMODE == 'HTML': text = cgi.escape(text) self._htmlwrite(text) elif self._COLORMODE == 'TTY': self._write_tty(text, colorTag) else: self.fh.write(text) # Plain Text else: if type(text) == unicode: text = util.printableUnicode(text).encode('utf-8') elif encoding: try: utext = unicode(text, encoding) text = util.printableUnicode(utext).encode('utf-8') except: text = util.printableText(str(text)) else: text = util.printableText(str(text)) if self._COLORMODE == 'HTML': text = cgi.escape(text) self._htmlwrite(text) elif self._COLORMODE == 'TTY': self._write_tty(text, colorTag) else: self.fh.write(text) # Close direction if self._COLORMODE == 'HTML' and direction != '': self._htmlwrite("</span>")
def _write_string(self, text, direction, timestamp, encoding=None): colorTag = '' # Print TimestampcolorTag if self._COLORMODE == 'HTML' and timestamp != None: self._htmlwrite('<div class="timestamp">\n%s UTC:</div>' % datetime.datetime.utcfromtimestamp(timestamp)) #if self._hexmode: self._htmlwrite("<br>") elif self._COLORMODE == 'TTY' and self._timemode and timestamp != None: self.fh.write('\x1b[36m%s UTC:\x1b[0m\n' % datetime.datetime.utcfromtimestamp(timestamp)) if self.nobuffer: self.fh.flush() # Set Direction if direction.lower() == 'cs': if self._COLORMODE == 'HTML': self._htmlwrite('<span style="color:red;">') elif self._COLORMODE == 'TTY': colorTag = '\x1b[0;31m' elif direction.lower() == 'sc': if self._COLORMODE == 'HTML': self._htmlwrite('<span style="color:blue;">') elif self._COLORMODE == 'TTY': colorTag = '\x1b[0;32m' # Hex Mode Data if self._hexmode: # Hex Output dlen = len(text) if direction.lower() == 'cs': msgOffset = self._CS_offset self._CS_offset += dlen elif direction.lower() == 'sc': msgOffset = self._SC_offset self._SC_offset += dlen else: msgOffset = self._NODIR_offset self._NODIR_offset += dlen text = util.hexPlusAscii(str(text), 16, msgOffset) if self._COLORMODE == 'HTML': text = cgi.escape(text) self._htmlwrite(text) elif self._COLORMODE == 'TTY': self._write_tty(text, colorTag) else: self.fh.write(text) if self.nobuffer: self.fh.flush() # Plain Text else: if type(text) == unicode: text = util.printableUnicode(text).encode('utf-8') elif encoding: try: utext = unicode(text, encoding) text = util.printableUnicode(utext).encode('utf-8') except: text = util.printableText(str(text)) else: text = util.printableText(str(text)) if self._COLORMODE == 'HTML': text = cgi.escape(text) self._htmlwrite(text) elif self._COLORMODE == 'TTY': self._write_tty(text, colorTag) else: self.fh.write(text) # Close direction if self._COLORMODE == 'HTML' and direction != '': self._htmlwrite("</span>")
def HTTPHandler(self, conn, request, response, requesttime, responsetime): host = '' loc = '' uri = '' lastmodified = '' #request_time, request, response = self.httpDict[conn.addr] # extract method,uri,host from response host = util.getHeader(request, 'host') if host == '': host = conn.serverip try: status = response.status except: status = '' try: reason = response.reason except: reason = '' if self.urlfilter: if not re.search(self.urlfilter, host + request.uri): return if '?' in request.uri: [uri_location, uri_data] = request.uri.split('?', 1) else: uri_location = request.uri uri_data = '' if self.maxurilen > 0 and len(uri_location) > self.maxurilen: uri_location = uri_location[:self.maxurilen] + '[truncated]' else: uri_location = uri_location if response == None: response_message = "%s (%s) %s%s" % ( request.method, 'NO RESPONSE', host, uri_location) else: response_message = "%s (%s) %s%s (%s)" % ( request.method, response.status, host, uri_location, util.getHeader(response, 'content-type')) urlParams = util.URLDataToParameterDict(uri_data) postParams = util.URLDataToParameterDict(request.body) clientCookies = self._parseCookies(util.getHeader(request, 'cookie')) serverCookies = self._parseCookies( util.getHeader(response, 'set-cookie')) self.alert(response_message, urlParams=urlParams, postParams=postParams, clientCookies=clientCookies, serverCookies=serverCookies, **conn.info() ) referer = util.getHeader(request, 'referer') if len(referer): self.out.write(' Referer: %s\n' % referer) if clientCookies: self.out.write(' Client Transmitted Cookies:\n', direction='cs') for key in clientCookies: self.out.write(' %s -> %s\n' % (util.printableUnicode(key), util.printableUnicode(clientCookies[key])), direction='cs') if serverCookies: self.out.write(' Server Set Cookies:\n', direction='sc') for key in serverCookies: self.out.write(' %s -> %s\n' % (util.printableUnicode(key), util.printableUnicode(serverCookies[key])), direction='sc') if urlParams: self.out.write(' URLParameters:\n', direction='cs') for key in urlParams: self.out.write(' %s -> %s\n' % (util.printableUnicode(key), util.printableUnicode(urlParams[key])), direction='cs') if postParams: self.out.write(' POSTParameters:\n', direction='cs') for key in postParams: self.out.write(' %s -> %s\n' % (util.printableUnicode(key), util.printableUnicode(postParams[key])), direction='cs') elif len(request.body): self.out.write(' POST Body:\n', direction='cs') if len(request.body) > self.maxpost and self.maxpost > 0: self.out.write('%s[truncated]\n' % util.printableUnicode( request.body[:self.maxpost]), direction='cs') else: self.out.write( util.printableUnicode(request.body) + u"\n", direction='cs') if self.showcontent or self.showhtml: if self.showhtml and 'html' not in util.getHeader(response, 'content-type'): return if 'gzip' in util.getHeader(response, 'content-encoding'): content = self.decompressGzipContent(response.body) if content == None: content = '(gunzip failed)\n' + response.body else: content = '(gzip encoded)\n' + content else: content = response.body self.out.write("Body Content:\n", direction='sc') self.out.write( util.printableUnicode(content) + u"\n", direction='sc')
def HTTPHandler(self,conn,request,response,requesttime,responsetime): host = '' loc = '' uri = '' lastmodified = '' #request_time, request, response = self.httpDict[conn.addr] # extract method,uri,host from response host = util.getHeader(request, 'host') if host == '': host = conn.serverip try: status = response.status except: status = '' try: reason = response.reason except: reason = '' if self.urlfilter: if not re.search(self.urlfilter, host+request.uri): return if '?' in request.uri: [uri_location, uri_data] = request.uri.split('?',1) else: uri_location = request.uri uri_data = '' if self.maxurilen > 0 and len(uri_location)> self.maxurilen: uri_location = uri_location[:self.maxurilen]+'[truncated]' else: uri_location = uri_location if response == None: response_message = "%s (%s) %s%s" % (request.method, 'NO RESPONSE', host, uri_location) else: response_message = "%s (%s) %s%s (%s)" % (request.method, response.status, host, uri_location, util.getHeader(response, 'content-type')) urlParams = util.URLDataToParameterDict(uri_data) postParams = util.URLDataToParameterDict(request.body) clientCookies = self._parseCookies(util.getHeader(request, 'cookie')) serverCookies = self._parseCookies(util.getHeader(response, 'set-cookie')) self.alert(response_message, urlParams=urlParams, postParams=postParams, clientCookies=clientCookies, serverCookies=serverCookies, **conn.info() ) referer = util.getHeader(request, 'referer') if len(referer): self.out.write(' Referer: %s\n' % referer) if clientCookies: self.out.write(' Client Transmitted Cookies:\n', direction='cs') for key in clientCookies: self.out.write(' %s -> %s\n' % (util.printableUnicode(key),util.printableUnicode(clientCookies[key])), direction='cs') if serverCookies: self.out.write(' Server Set Cookies:\n', direction='sc') for key in serverCookies: self.out.write(' %s -> %s\n' % (util.printableUnicode(key),util.printableUnicode(serverCookies[key])), direction='sc') if urlParams: self.out.write(' URLParameters:\n', direction='cs') for key in urlParams: self.out.write(' %s -> %s\n' % (util.printableUnicode(key),util.printableUnicode(urlParams[key])), direction='cs') if postParams: self.out.write(' POSTParameters:\n', direction='cs') for key in postParams: self.out.write(' %s -> %s\n' % (util.printableUnicode(key),util.printableUnicode(postParams[key])), direction='cs') elif len(request.body): self.out.write(' POST Body:\n', direction='cs') if len(request.body) > self.maxpost and self.maxpost > 0: self.out.write('%s[truncated]\n' % util.printableUnicode(request.body[:self.maxpost]), direction='cs') else: self.out.write(util.printableUnicode(request.body) + u"\n", direction='cs') if self.showcontent or self.showhtml: if self.showhtml and 'html' not in util.getHeader(response, 'content-type'): return if 'gzip' in util.getHeader(response, 'content-encoding'): content = self.decompressGzipContent(response.body) if content == None: content = '(gunzip failed)\n' + response.body else: content = '(gzip encoded)\n' + content else: content = response.body self.out.write("Body Content:\n", direction='sc') self.out.write(util.printableUnicode(content) + u"\n", direction='sc')