~ubuntu-branches/ubuntu/saucy/nginx/saucy-updates

« back to all changes in this revision

Viewing changes to debian/modules/naxsi/contrib/naxsi-ui/nx_intercept.py

  • Committer: Package Import Robot
  • Author(s): Kartik Mistry, Cyril Lavier, Michael Lustfield, Kartik Mistry
  • Date: 2012-05-14 11:15:00 UTC
  • mfrom: (4.2.49 sid)
  • Revision ID: package-import@ubuntu.com-20120514111500-1y9ij7zulu9xnmry
Tags: 1.2.0-1
[Cyril Lavier]
* New upstream release. (Closes: #670306)
  + 1.2.x is stable release now.
* debian/modules/chunkin-nginx-module:
  + Updated chunkin-nginx-module to v0.23rc2-3-g85eca98.
* debian/modules/headers-more-module:
  + Updated headers-more-module to v0.17rc1-4-g33a82ed.
* debian/modules/nginx-development-kit:
  + Updated nginx-development-kit to v0.2.17-7-g24202b4.
* debian/modules/nginx-echo:
  + Updated nginx-echo to v0.38rc2-7-g080c0a1.
* debian/modules/nginx-lua:
  + Updated nginx-lua to v0.5.0rc25-5-g8d28785.
* debian/modules/nginx-upstream-fair:
  + Updated nginx-upstream-fair to a18b409.
* debian/modules/nginx-upload-progress:
  + Updated nginx-upload-progress to v0.9.0-0-ga788dea.
* debian/modules/naxsi:
  + Updated naxsi to 0.46
* debian/modules/README.Modules-versions:
  + Updated versions and URLs for modules.
* debian/naxsi-ui-extract, debian/naxsi-ui-intercept,
  debian/nginx-naxsi-ui.*, debian/naxsi-ui-extract.1,
  debian/naxsi-ui-intercept.1, debian/rules:
  + Added nginx-naxsi-ui package containing the learning daemon
    and the WebUI.
* debian/nginx-common.nginx.default, debian/nginx-common.nginx.init:
  + Renamed files to be compliant with the nginx-naxsi-ui package.
* debian/po:
  + Added needed files for using po-debconf.
  + Added French translation.
* debian/control:
  + Applied the modifications given after the review by Justin Rye.

[Michael Lustfield]
* debian/conf/uwsgi_params:
  + Added UWSGI_SCHEME to uwsgi_params. (Closes: #664878)
* debian/conf/sites-available/default:
  + Added allow directive for ipv6 localhost. (Closes: #664271)

[Kartik Mistry]
* debian/control:
  + wrap-and-sort.
* debian/copyright:
  + Added missing copyrights, minor formatting fixes.
* debian/nginx-common.nginx.init:
  + Added ulimit for restarts, Thanks to Daniel Roschka
    <danielroschka@phoenitydawn.de> for patch. (Closes: #673580)
* debian/conf/sites-available/default:
  + Added patch to fix deprecated "listen" directive, Thanks to
    Guillaume Plessis <gui@dotdeb.org> for patch. (Closes: #672632)

Show diffs side-by-side

added added

removed removed

Lines of Context:
2
2
from twisted.web import http
3
3
from twisted.internet import protocol
4
4
from twisted.internet import reactor, threads
 
5
from ConfigParser import ConfigParser
 
6
from nx_parser import signature_parser
 
7
 
 
8
import urllib
5
9
import pprint
6
10
import socket
7
 
from nx_parser import signature_parser
8
11
import MySQLConnector
9
12
import MySQLdb
10
13
import getopt
11
14
import sys
 
15
import re
12
16
 
13
 
quiet=False
14
17
 
15
18
class InterceptHandler(http.Request):
16
19
    def process(self):
34
37
        threads.deferToThread(self.background, fullstr, sig)
35
38
        self.finish()
36
39
        return
 
40
 
37
41
    def background(self, fullstr, sig):
38
42
        self.db = MySQLConnector.MySQLConnector().connect()
39
43
        if self.db is None:
51
55
class InterceptFactory(http.HTTPFactory):
52
56
    protocol = InterceptProtocol
53
57
 
 
58
 
54
59
def usage():
55
 
    print 'Usage: python nx_intercept [-h,--help] [-p,--port portnumber] [-a,--add-monitoring ip:1.2.3.4|md5:af794f5e532d7a4fa59c49845af7947e]'
 
60
    print 'Usage: python nx_intercept [-h,--help]  [-a,--add-monitoring ip:1.2.3.4|md5:af794f5e532d7a4fa59c49845af7947e] [-q,--quiet] [-l,--log-file /path/to/logfile]'
56
61
 
57
 
def add_monitoring(arg):
 
62
def add_monitoring(arg, conf_path):
58
63
    l = arg.split('|')
59
64
    ip = None
60
65
    md5 = None
72
77
        except socket.error:
73
78
            print 'ip is not valid ! Nothing will be inserted in db !'
74
79
            return
75
 
    db = MySQLConnector.MySQLConnector().connect()
 
80
    db = MySQLConnector.MySQLConnector(conf_path).connect()
76
81
    cursor = db.cursor()
77
82
    if md5 is not None and ip is not None:
78
83
        cursor.execute("INSERT INTO http_monitor (peer_ip, md5) VALUES (%s, %s)", (ip, md5))
84
89
        cursor.execute("INSERT INTO http_monitor (peer_ip) VALUES (%s)", (ip))
85
90
        return
86
91
 
 
92
def fill_db(filename, conf_path):
 
93
    fd = open(filename, 'r')
 
94
    mysqlh = MySQLConnector.MySQLConnector(conf_path)
 
95
    db = mysqlh.connect()
 
96
    sig = ''
 
97
 
 
98
    if db is None:
 
99
        raise ValueError('Cannot connect to db')
 
100
    cursor = db.cursor()
 
101
    if cursor is None:
 
102
        raise ValueError('Cannot connect to db')
 
103
 
 
104
    if re.match("[a-z0-9]+$", mysqlh.dbname) == False:        
 
105
        print 'bad db name :)'
 
106
        exit(-2)
 
107
    
 
108
    cursor.execute("DROP DATABASE IF EXISTS %s;" % mysqlh.dbname)
 
109
    cursor.execute("CREATE DATABASE %s;" %  mysqlh.dbname)
 
110
    db.select_db(mysqlh.dbname)
 
111
 
 
112
    for line in fd:
 
113
        fullstr = ''
 
114
        if 'NAXSI_FMT' in line:
 
115
            l = line.split(", ")
 
116
            date = ' '.join(l[0].split()[:2])
 
117
            sig = l[0].split('NAXSI_FMT:')[1][1:]
 
118
            l = l[1:]
 
119
            request_args = {}
 
120
            for i in l:
 
121
                s = i.split(':')
 
122
                request_args[s[0]] = urllib.unquote(''.join(s[1:]))
 
123
#            print 'args are ', request_args
 
124
            if request_args:
 
125
                fullstr = request_args['request'][2:-1] + ' Referer : ' + request_args.get('referrer', ' "None"')[2:-1].strip('"\n') + ',Cookie : ' + request_args.get('cookie', ' "None"')[2:-1]
 
126
        if sig != ''  and fullstr != '':
 
127
#            print "adding %s (%s) " % (sig, fullstr)
 
128
            parser = signature_parser(cursor)
 
129
            parser.sig_to_db(fullstr, sig, date=date)
 
130
    fd.close()
 
131
    db.close()
 
132
 
 
133
 
87
134
if __name__ == '__main__':
88
 
#    global quiet
89
 
    port = 8000
90
135
    try:
91
 
        opts, args = getopt.getopt(sys.argv[1:], 'qhp:a:', ['help', 'port', 'add-monitoring', 'quiet'])
 
136
        opts, args = getopt.getopt(sys.argv[1:], 'c:ha:l:', ['conf-file', 'help', 'add-monitoring', 'log-file'])
92
137
    except getopt.GetoptError, err:
93
138
        print str(err)
94
139
        usage()
95
140
        sys.exit(42)
96
141
 
 
142
    has_conf = False
 
143
    conf_path = ''
 
144
 
97
145
    for o, a in opts:
98
146
        if o in ('-h', '--help'):
99
147
            usage()
100
148
            sys.exit(0)
101
 
        if o in ('-p', '--port'):
102
 
            port = int(a)
103
 
        if o in ('-q', '--quiet'):
104
 
            quiet = True
105
149
        if o in ('-a', '--add-monitoring'):
106
 
            add_monitoring(a)
107
 
            exit(42)
 
150
            if has_conf is False:
 
151
                print "Conf File must be specified first !"
 
152
                exit(42)
 
153
            add_monitoring(a, conf_path)
 
154
            exit(42)
 
155
        if o in ('-l', '--log-file'):
 
156
            if has_conf is False:
 
157
                print "Conf File must be specified first !"
 
158
                exit(42)
 
159
            print "Filling database with %s. ALL PREVIOUS CONTENT WILL BE DROPPED !!!!!"
 
160
            fill_db(a, conf_path)
 
161
            print "Done."
 
162
            exit(42)
 
163
        if o in ('-c', '--conf-file'):
 
164
            has_conf = True
 
165
            conf_path = a
 
166
 
 
167
    if has_conf is False:
 
168
        print 'Conf file is mandatory !'
 
169
        exit(-42)
 
170
    fd = open(conf_path, 'r')     
 
171
    conf = ConfigParser()
 
172
    conf.readfp(fd)
 
173
    try:
 
174
       port = int(conf.get('nx_intercept', 'port'))
 
175
    except:
 
176
       print "No port in conf file ! Using default port (8080)"
 
177
       port = 8080
 
178
    fd.close()            
108
179
 
109
180
    reactor.listenTCP(port, InterceptFactory())
110
181
    reactor.run()