Advertisement
Guest User

Untitled

a guest
Jan 17th, 2016
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.94 KB | None | 0 0
  1. #!/usr/bin/env ruby
  2. require 'rubygems'
  3. require 'json'
  4. require 'pony'
  5. require 'text-table'
  6. # We write a file with the following format:
  7. # ip_address,mac_address,accepted,alerted,first_seen_timestamp
  8. email_to_alert = "email"
  9. gmail_username = 'email'
  10. gmail_app_password = 'password'
  11. filename = ENV['HOME'] + "/.wifi_hosts.txt"
  12. connections_to_alert = []
  13. wifi_host_log_structure = []
  14. if File.exist?(filename) then
  15. #puts "File exists"
  16. File.open(filename, 'r+') do |f|
  17. #puts "Opened file"
  18. wifi_host_log_text = File.read(f)
  19. if wifi_host_log_text.length > 1 then
  20. wifi_host_log_structure = JSON.parse(wifi_host_log_text)
  21. end
  22. end
  23. end
  24. #puts "wifi_host_log_structure"
  25. #puts wifi_host_log_structure.inspect
  26. STDIN.read.split("\n").each do |line|
  27. if /^(\d+)\./.match(line) then
  28. ip, mac = line.split(' ')
  29. #puts "#{ip} :: #{mac}"
  30. wifi_host_log_structure.each do
  31. |log_structure|
  32. #puts "log_structure", log_structure["mac"]
  33. end
  34. found_in_logs = wifi_host_log_structure.any? do
  35. |log_structure|
  36. log_structure["mac"].eql? mac
  37. end
  38. #puts "Found in logs #{found_in_logs}";
  39. if !found_in_logs then
  40. ts = Time.now
  41. connections_to_alert.push({
  42. :ip => ip,
  43. "mac" => mac,
  44. :timestamp => ts
  45. })
  46. wifi_host_log_structure.push({
  47. :ip => ip,
  48. "mac" => mac,
  49. :alerted => 1,
  50. :timestamp => ts.getutc
  51. });
  52. #puts "New guy here: #{mac}"
  53. end
  54. end
  55. end
  56. if connections_to_alert.length > 0 then
  57. #puts connections_to_alert.inspect
  58. email_body = ''
  59. email_table = Text::Table.new
  60. email_table.head = ['MAC Address', 'IP Address', 'Timestamp']
  61. connections_to_alert.each do |connection|
  62. #puts "connection x"
  63. #puts "#{connection[:mac]}"
  64. email_table.rows << [connection['mac'], connection[:ip], connection[:timestamp]]
  65. end
  66. email_body += "We have detected the following new MAC addresses on the network.\n"
  67. email_body += "<pre>#{email_table.to_s}</pre>"
  68. File.open(filename, 'w') do |f|
  69. #puts "Opened file for writing"
  70. f.write(wifi_host_log_structure.to_json)
  71. end
  72. # alright alert here somehow, Twilio, SMTP or gmail
  73. Pony.mail({
  74. :to => email_to_alert,
  75. :subject => 'New MAC address was detected',
  76. :html_body => email_body,
  77. :via => :smtp,
  78. :via_options => {
  79. :address => 'smtp.gmail.com',
  80. :port => '587',
  81. :enable_starttls_auto => true,
  82. :user_name => gmail_username,
  83. :password => gmail_app_password,
  84. :authentication => :plain, # :plain, :login, :cram_md5, no auth by default
  85. :domain => "localhost.localdomain" # the HELO domain provided by the client to the server
  86. }
  87. })
  88. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement