Class: Snackhack2::AuthLog

Inherits:
Object
  • Object
show all
Defined in:
lib/snackhack2/webserver_log_cleaner.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path: File.join("/var", "log", "auth.log.2"), user: "root") ⇒ AuthLog

Returns a new instance of AuthLog.



31
32
33
34
35
36
# File 'lib/snackhack2/webserver_log_cleaner.rb', line 31

def initialize(path: File.join("/var", "log", "auth.log.2"), user: "root")
  @ip = ip
  @user = user 
  @user_list = ["user", "admin", "nobody", "proxy", "test", "tom", "frank", "irc", "sshd", "mail"]
  @path = path
end

Instance Attribute Details

#ip ⇒ Object

Returns the value of attribute ip.



30
31
32
# File 'lib/snackhack2/webserver_log_cleaner.rb', line 30

def ip
  @ip
end

#user ⇒ Object

Returns the value of attribute user.



30
31
32
# File 'lib/snackhack2/webserver_log_cleaner.rb', line 30

def user
  @user
end

Instance Method Details

#remove_all ⇒ Object



78
79
80
81
# File 'lib/snackhack2/webserver_log_cleaner.rb', line 78

def remove_all
  remove_ip
  remove_username
end

#remove_ip ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/snackhack2/webserver_log_cleaner.rb', line 57

def remove_ip
  out = ''
    # generate random IP
    new_ip = Array.new(4) { rand(256) }.join('.')
    File.readlines(@path).each do |line|
      # Uses .match and regex to match IPs
      old_ip = line.match(/((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)/)
      out += if old_ip.to_s == @ip
              # replace the foud ip with a randomly generated IP
               line.gsub(old_ip.to_s, new_ip)
             else
              # if it does not find the inputted IP
              # it will return the line
               line
             end
    end
    # deletes the @path
    File.delete(@path)
    # saves the new log to the old path
    File.open(@path, 'w+') { |file| file.write(out) }
end

#remove_username ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/snackhack2/webserver_log_cleaner.rb', line 37

def remove_username
  # replaces the username inputted with one
  # 'randomly' picked from @user_list
  out = ""
  # loops through the given file
  File.readlines(@path).each do |user|
    new_user = user.match(/#{@user}/)
    if new_user.to_s.eql?(@user)
      # replaces the matched user with a 
      # 'randomly' ( .sample) user
      out += user.gsub(new_user.to_s, @user_list.sample)
    else
      # if it does not match it will add the non matched
      # line to the newly created one
      out += user
    end
  end
File.delete(@path)
File.open(@path, 'w+') { |file| file.write(out) }
end