Advertisement
Guest User

Untitled

a guest
Oct 5th, 2016
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.41 KB | None | 0 0
  1. #!/usr/bin/python
  2.  
  3. import MySQLdb
  4. import ConfigParser
  5. import smtplib
  6. import os
  7.  
  8. #get database settings
  9. # hack to comply with Alienvault config file format:
  10. from io import StringIO
  11. filename = '/etc/ossim/ossim_setup.conf'
  12. vfile = StringIO(u'[misc]\n%s' % open(filename).read())
  13.  
  14. db_config = ConfigParser.ConfigParser()
  15. db_config.readfp(vfile)
  16. db_ip=db_config.get("database", "db_ip")
  17. db_user=db_config.get("database", "user")
  18. db_password=db_config.get("database", "pass")
  19.  
  20. #read settings from config file
  21. ## sample config file
  22. # [settings]
  23. # default_interval_mins=5
  24. # default_threshold=1000
  25. # smtp_server=127.0.0.1
  26. # recipient=someone@example.com
  27. # sender=alienvault@example.com
  28. #
  29. # [thresholds]
  30. # sensorname=1000
  31.  
  32. eps_config = ConfigParser.ConfigParser()
  33. eps_config.read("/etc/ossim/eps_monitor.conf")
  34. default_interval_mins=eps_config.get("settings", "default_interval_mins")
  35. default_threshold=eps_config.get("settings", "default_threshold")
  36. thresholds=eps_config.options("thresholds")
  37.  
  38. db = MySQLdb.connect( db_ip, db_user, db_password, "alienvault" )
  39. cursor = db.cursor()
  40.  
  41. # execute SQL query using execute() method.
  42. cursor.execute("select hex(id),name from sensor")
  43.  
  44. results = cursor.fetchall()
  45. db.close()
  46. for row in results:
  47. print os.linesep
  48. db = MySQLdb.connect( db_ip, db_user, db_password, "alienvault_siem" )
  49. cursor = db.cursor()
  50. sql ='select COUNT(*) from acid_event WHERE device_id in (select id from device where hex(sensor_id)="'+row[0]+'") AND timestamp > now() - INTERVAL '+default_interval_mins+' MINUTE;'
  51. cursor.execute(sql)
  52. result = cursor.fetchone()
  53. print "%s: %s" % (row[1],result[0])
  54.  
  55. if row[1].lower() in thresholds:
  56. sensor_threshold = int(eps_config.get('thresholds', row[1]))
  57. else:
  58. sensor_threshold = int(default_threshold)
  59.  
  60. if result[0] < sensor_threshold:
  61. sender = eps_config.get('settings', 'sender')
  62. recipient = eps_config.get('settings', 'recipient')
  63. smtp_server = eps_config.get('settings', 'smtp_server')
  64.  
  65. message = """From: AlienVault EPS Monitor <%s>
  66. To: <%s>
  67. Subject: AlienVault EPS monitor alert
  68.  
  69. This alert has been triggered since the generated EPS of sensor %s is currently %s
  70. """ % (sender, recipient, row[1], result[0])
  71.  
  72. try:
  73. smtpObj = smtplib.SMTP(smtp_server)
  74. smtpObj.sendmail(sender, recipient, message)
  75. print "Successfully sent email"
  76. except SMTPException:
  77. print "Error: unable to send email"
  78. else:
  79. print "looking good..."
  80.  
  81. db.close()
  82.  
  83. print os.linesep
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement