glastwitter.diff

Ben L., 02/20/2010 03:32 pm

Download (5.5 kB)

 
logmodules/glastwitter.py (working copy)
16 16
            "username" : config.get("twitter","username"),
17 17
            "password" : config.get("twitter","password")
18 18
            }
19
        
19

  
20 20
        self.mysql_opts = {
21 21
            "host" : config.get("mysql", "host"),
22 22
            "port" : config.get("mysql", "port"),
......
24 24
            "pass" : config.get("mysql", "pass"),
25 25
            "db" : config.get("mysql", "db")
26 26
            }
27
        
28
        logtwitter = config.get("log","logtwitter")
29
        
27

  
30 28
        self.username = self.twitter_opts["username"]
31 29
        self.password = self.twitter_opts["password"]
32
        self.timed = Timed()
33
        if logtwitter == "True":
34
            print "Twitter logging module loaded"
35
            time.sleep(1)
36
        
30

  
37 31
    def tweet(self, message):
38 32
        data = urllib.urlencode({"status" : message})
39 33
        try:
40 34
            urllib.urlopen("http://%s:%s@twitter.com/statuses/update.xml"
41 35
                                 % (self.username,self.password), data)
42
            if options.verbose == True: print "Message submitted to Twitter!" 
36
            if options.verbose == True: print "Message submitted to Twitter!"
43 37
        except:
44 38
            print "Twitter connection error!"
45 39
            pass
46
    
40

  
47 41
    def dbconnect(self):
48 42
        try:
49 43
            mysql = MySQLdb.connect(
......
56 50
        except MySQLdb.Error, e:
57 51
            print "Twitter Error %d: %s" % (e.args[0], e.args[1])
58 52
            pass
59
        mysql.threadsafety = 2 
53
        mysql.threadsafety = 2
60 54
        return mysql
61
            
55

  
62 56
    def squawk_ips(self):
63 57
        # Unique IPs in the database
64
        mysql = twitter.dbconnect()
58
        mysql = self.dbconnect()
65 59
        cursor = mysql.cursor()
66 60
        sql1 = """
67
                SELECT ip FROM log 
61
                SELECT ip FROM log
68 62
                GROUP BY ip
69 63
                """
70 64
        cursor.execute(sql1)
71
        message = "There are " + str(cursor.rowcount) + " non-recurring " \
72
                "IPs in the database."
73
        twitter.tweet(message)
65
        message = "There are %d non-recurring IPs in the database." % (cursor.rowcount,)
66
        self.tweet(message)
74 67
        mysql.close()
75
        
68

  
76 69
    def squawk_paths(self):
77 70
        # Unique google dorks in the database
78
        mysql = twitter.dbconnect()
71
        mysql = self.dbconnect()
79
        cursor = mysql.cursor()            
72
        cursor = mysql.cursor()
80 73
        sql2 = """
81
                SELECT vicpath FROM path 
74
                SELECT vicpath FROM path
82 75
                """
83 76
        cursor.execute(sql2)
84
        message = "There are " + str(cursor.rowcount) + " non-recurring " \
85
                "vulnerable paths (google dorks) in the database."
86
        twitter.tweet(message)
77
        message = "There are %d non-recurring " \
78
                  "vulnerable paths (google dorks) in the database." \
79
                  % (cursor.rowcount,)
80
        self.tweet(message)
87 81
        mysql.close()
88
        
82

  
89 83
    def squawk_attacks(self):
90 84
        # Attacks in the last 30 minutes
91 85
        lasttime = (datetime.datetime.now() - datetime.timedelta(minutes=30)).strftime("%Y-%m-%d %X")
92
        mysql = twitter.dbconnect()
86
        mysql = self.dbconnect()
93 87
        cursor = mysql.cursor()
94 88
        sql3 = """
95
                SELECT id FROM log 
89
                SELECT id FROM log
96 90
                WHERE attime > %s
97 91
                """
98 92
        try:
99 93
            cursor.execute(sql3, (lasttime,))
100 94
        except MySQLdb.Error, e:
101 95
            print "Twitter Error %d: %s" % (e.args[0], e.args[1])
102
        message = "Got " + str(cursor.rowcount) + " attacks in the last " \
103
                "30 minutes!"
104
        twitter.tweet(message)
96
        message = "Got %d attacks in the last 30 minutes!" % (cursor.rowcount,)
97
        self.tweet(message)
105 98
        mysql.close()
106 99

  
107 100

  
108 101
class Timed():
109
   
102

  
103
    def __init__(self, twitter):
104
        self.twitter = twitter
105

  
110 106
    def action(self):
111 107
        if not self.canceled:
112
            twitter = Twitter()
113
            twitter.squawk_attacks()
108
            self.twitter.squawk_attacks()
114 109
            time.sleep(1)
115
            twitter.squawk_paths()
110
            self.twitter.squawk_paths()
116 111
            time.sleep(1)
117
            twitter.squawk_ips()
118
            self.timed.start_timer()
119
   
112
            self.twitter.squawk_ips()
113
            self.start_timer()
114

  
120 115
    def start_timer(self):
121 116
        self.canceled = False
122
        self.timed = Timed()
123
        self.t = threading.Timer(1800, timed.action)
117
        self.t = threading.Timer(1800, self.action)
124 118
        self.t.start()
125
       
119

  
126 120
    def stop_timer(self):
127 121
        self.t.cancel()
128 122
        self.canceled = True
129 123

  
130
twitter = Twitter()
131
timed = Timed()
132
timed.start_timer()
133 124

  
134
def cancelit():
135
    timed.stop_timer()
125
config = ConfigParser.ConfigParser()
126
config.read("conf/glastopf.cfg")
127

  
128
logtwitter = config.get("log","logtwitter")
129
if logtwitter == "True":
130
    print "Twitter logging module loaded"
131
    time.sleep(1)
132
    twitter = Twitter()
133
    timed = Timed(twitter)
134
    timed.start_timer()
135

  
136
    def cancelit():
137
        timed.stop_timer()
138
else:
139
    def cancelit():
140
		pass