Commit c7f2bcf1 authored by Benjamin "Ziirish" SANS's avatar Benjamin "Ziirish" SANS
Browse files

code refactoring to make it more modulable

parent 8365571b
#!/usr/bin/env python
# -*- coding: utf8 -*-
import os
from optparse import OptionParser
from burpui import app, bui
if __name__ == '__main__':
"""
Main function
"""
parser = OptionParser()
parser.add_option('-v', '--verbose', dest='log', help='verbose output', action='store_true')
parser.add_option('-c', '--config', dest='config', help='configuration file', metavar='CONFIG')
(options, args) = parser.parse_args()
d = options.log
app.config['DEBUG'] = d
if options.config:
conf = options.config
else:
conf = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'burpui.cfg')
app.config['CFG'] = conf
bui.setup(conf)
bui.run(d)
#!/usr/bin/env python
# -*- coding: utf8 -*-
import os
from flask import Flask
from burpui.server import Server as BurpUI
app = Flask(__name__)
app.config['CFG'] = os.path.join(app.root_path, 'burpui.cfg')
app.secret_key = 'VpgOXNXAgcO81xFPyWj07ppN6kExNZeCDRShseNzFKV7ZCgmW2/eLn6xSlt7pYAVBj12zx2Vv9Kw3Q3jd1266A=='
app.jinja_env.globals.update(isinstance=isinstance,list=list)
bui = BurpUI(app)
import burpui.routes
#!/usr/bin/env python
# -*- coding: utf8 -*-
import ConfigParser
import logging
import sys
import os
import subprocess
import re
import socket
import time
import datetime
import collections
from flask import Flask, request, render_template, jsonify, redirect, url_for, abort, flash
from optparse import OptionParser
burpport = 4972
burphost = '127.0.0.1'
port = 5000
bind = '::'
refresh = 15
ssl = False
sslcert = ''
sslkey = ''
sslcontext = None
conf = None
from flask import Flask, request, render_template, jsonify, redirect, url_for, abort, flash, g, session
from burpui import app, bui
running = []
......@@ -30,11 +18,6 @@ running = []
Now this is Burp-UI
"""
app = Flask(__name__)
app.config['CFG'] = os.path.join(app.root_path, 'burpui.cfg')
app.secret_key = 'VpgOXNXAgcO81xFPyWj07ppN6kExNZeCDRShseNzFKV7ZCgmW2/eLn6xSlt7pYAVBj12zx2Vv9Kw3Q3jd1266A=='
app.jinja_env.globals.update(isinstance=isinstance,list=list)
status = {
'i': 'idle',
'r': 'running',
......@@ -139,7 +122,7 @@ def _burp_status(query='\n'):
"""
r = []
try:
socket.inet_aton(burphost)
socket.inet_aton(bui.burphost)
form = socket.AF_INET
except socket.error:
form = socket.AF_INET6
......@@ -149,7 +132,7 @@ def _burp_status(query='\n'):
else:
q = query
s = socket.socket(form, socket.SOCK_STREAM)
s.connect((burphost, burpport))
s.connect((bui.burphost, bui.burpport))
s.send(q)
s.shutdown(socket.SHUT_WR)
f = s.makefile()
......@@ -162,7 +145,7 @@ def _burp_status(query='\n'):
f.close()
return r
except socket.error:
app.logger.error('Cannot contact burp server at %s:%s', burphost, burpport)
app.logger.error('Cannot contact burp server at %s:%s', bui.burphost, bui.burpport)
return r
def _parse_backup_log(f, n, c=None):
......@@ -210,6 +193,7 @@ def _parse_backup_log(f, n, c=None):
continue
found = False
# this method is not optimal, but it is easy to read and to maintain
for key, regex in lookup_easy.iteritems():
r = re.search(regex, line)
if r:
......@@ -224,9 +208,6 @@ def _parse_backup_log(f, n, c=None):
for v in tmp:
fields[i] = int(v)
i += 1
while i < 3:
fields[i] = 0
i += 1
seconds = 0
seconds += fields[0]
seconds += fields[1] * 60
......@@ -235,8 +216,10 @@ def _parse_backup_log(f, n, c=None):
backup[key] = seconds
else:
backup[key] = int(r.group(1))
# break the loop as soon as we find a match
break
# if found is True, we already parsed the line so we can jump to the next one
if found:
continue
......@@ -636,64 +619,3 @@ def home():
Home page
"""
return render_template('clients.html', clients=True, overview=True)
def init(conf=None):
global burpport, burphost, port, bind, refresh, ssl, sslcert, sslkey
if not conf:
conf = app.config['CFG']
config = ConfigParser.ConfigParser({'bport': burpport, 'bhost': burphost, 'port': port, 'bind': bind, 'refresh': refresh, 'ssl': ssl, 'sslcert': sslcert, 'sslkey': sslkey})
with open(conf) as fp:
config.readfp(fp)
burpport = config.getint('Global', 'bport')
burphost = config.get('Global', 'bhost')
port = config.getint('Global', 'port')
bind = config.get('Global', 'bind')
try:
ssl = config.getboolean('Global', 'ssl')
except ValueError:
app.logger.error("Wrong value for 'ssl' key! Assuming 'false'")
ssl = False
sslcert = config.get('Global', 'sslcert')
sslkey = config.get('Global', 'sslkey')
app.config['REFRESH'] = config.getint('UI', 'refresh')
app.logger.info('burp port: %d', burpport)
app.logger.info('burp host: %s', burphost)
app.logger.info('listen port: %d', port)
app.logger.info('bind addr: %s', bind)
app.logger.info('use ssl: %s', ssl)
app.logger.info('sslcert: %s', sslcert)
app.logger.info('sslkey: %s', sslkey)
app.logger.info('refresh: %d', refresh)
if __name__ == '__main__':
"""
Main function
"""
parser = OptionParser()
parser.add_option('-v', '--verbose', dest='log', help='verbose output', action='store_true')
parser.add_option('-c', '--config', dest='config', help='configuration file', metavar='CONFIG')
(options, args) = parser.parse_args()
d = options.log
app.config['DEBUG'] = d
if options.config:
conf = options.config
else:
conf = app.config['CFG']
init(conf)
if ssl:
from OpenSSL import SSL
sslcontext = SSL.Context(SSL.SSLv23_METHOD)
sslcontext.use_privatekey_file(sslkey)
sslcontext.use_certificate_file(sslcert)
if sslcontext:
app.run(host=bind, port=port, debug=d, ssl_context=sslcontext)
else:
app.run(host=bind, port=port, debug=d)
#!/usr/bin/env python
# -*- coding: utf8 -*-
import ConfigParser
g_burpport = 4972
g_burphost = '127.0.0.1'
g_port = 5000
g_bind = '::'
g_refresh = 15
g_ssl = False
g_sslcert = ''
g_sslkey = ''
class Server:
def __init__(self, app=None):
self.init = False
self.app = app
def setup(self, conf=None):
global g_refresh, g_burpport, g_burphost, g_port, g_bind, g_ssl, g_sslcert, g_sslkey
self.sslcontext = None
if not conf:
conf = self.app.config['CFG']
config = ConfigParser.ConfigParser({'bport': g_burpport, 'bhost': g_burphost, 'port': g_port, 'bind': g_bind, 'refresh': g_refresh, 'ssl': g_ssl, 'sslcert': g_sslcert, 'sslkey': g_sslkey})
with open(conf) as fp:
config.readfp(fp)
self.burpport = config.getint('Global', 'bport')
self.burphost = config.get('Global', 'bhost')
self.port = config.getint('Global', 'port')
self.bind = config.get('Global', 'bind')
try:
self.ssl = config.getboolean('Global', 'ssl')
except ValueError:
self.app.logger.error("Wrong value for 'ssl' key! Assuming 'false'")
self.ssl = False
self.sslcert = config.get('Global', 'sslcert')
self.sslkey = config.get('Global', 'sslkey')
self.app.config['REFRESH'] = config.getint('UI', 'refresh')
self.app.logger.info('burp port: %d', self.burpport)
self.app.logger.info('burp host: %s', self.burphost)
self.app.logger.info('listen port: %d', self.port)
self.app.logger.info('bind addr: %s', self.bind)
self.app.logger.info('use ssl: %s', self.ssl)
self.app.logger.info('sslcert: %s', self.sslcert)
self.app.logger.info('sslkey: %s', self.sslkey)
self.app.logger.info('refresh: %d', self.app.config['REFRESH'])
self.init = True
def run(self, debug=False):
if not self.init:
self.setup()
if self.ssl:
from OpenSSL import SSL
self.sslcontext = SSL.Context(SSL.SSLv23_METHOD)
self.sslcontext.use_privatekey_file(self.sslkey)
self.sslcontext.use_certificate_file(self.sslcert)
if self.sslcontext:
self.app.run(host=self.bind, port=self.port, debug=debug, ssl_context=self.sslcontext)
else:
self.app.run(host=self.bind, port=self.port, debug=debug)
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment