Rev 78 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
2 | daniel-mar | 1 | <?php |
2 | |||
3 | /* |
||
4 | * VNag - Nagios Framework for PHP |
||
5 | * Developed by Daniel Marschall, ViaThinkSoft <www.viathinksoft.com> |
||
6 | * Licensed under the terms of the Apache 2.0 license |
||
7 | * |
||
76 | daniel-mar | 8 | * Revision 2023-10-13 |
2 | daniel-mar | 9 | */ |
10 | |||
11 | // QUE: should we allow usernames to have wildcards, regexes or comma separated? |
||
12 | |||
13 | declare(ticks=1); |
||
14 | |||
15 | class LastCheck extends VNag { |
||
16 | private $argUser = null; |
||
17 | private $argRegex = null; |
||
18 | private $argWtmpFiles = null; |
||
19 | private $argEmptyOk = null; |
||
20 | private $argIpInfoToken = null; |
||
21 | |||
22 | private $cache = null; |
||
23 | private $cacheFile = null; |
||
24 | private $cacheDirty = false; |
||
25 | |||
26 | public function __construct() { |
||
27 | parent::__construct(); |
||
28 | |||
29 | if ($this->is_http_mode()) { |
||
30 | // Don't allow the standard arguments via $_REQUEST |
||
31 | $this->registerExpectedStandardArguments(''); |
||
32 | } else { |
||
33 | $this->registerExpectedStandardArguments('Vhtv'); |
||
34 | } |
||
35 | |||
36 | $this->addExpectedArgument($this->argUser = new VNagArgument('u', 'user', VNagArgument::VALUE_REQUIRED, 'user', 'The Linux username. If the argument is missing, all users will be checked.', null)); |
||
37 | $this->addExpectedArgument($this->argRegex = new VNagArgument('R', 'regex', VNagArgument::VALUE_REQUIRED, 'regex', 'The regular expression (in PHP: preg_match) which is applied on IP, Hostname, Country, AS number or ISP name. If the regular expression matches, the login will be accepted, otherweise an alert will be triggered. Example: /DE/ismU or /Telekom/ismU', null)); |
||
38 | $this->addExpectedArgument($this->argWtmpFiles = new VNagArgument('f', 'wtmpfile', VNagArgument::VALUE_REQUIRED, 'wtmpfile', 'Filemask of the wtmp file (important if you use logrotate), e.g. \'/var/log/wtmp*\'', '/var/log/wtmp*')); |
||
39 | $this->addExpectedArgument($this->argEmptyOk = new VNagArgument('e', 'emptyok', VNagArgument::VALUE_FORBIDDEN, null, 'Treat an empty result (e.g. empty wtmp file after rotation) as success; otherwise treat it as status "Unknown"', null)); |
||
40 | $this->addExpectedArgument($this->argIpInfoToken = new VNagArgument(null, 'ipInfoToken', VNagArgument::VALUE_REQUIRED, 'token', 'If you have a token for ipinfo.io, please enter it here. Without token, you can query the service approx 1,000 times per day (which should be enough)', null)); |
||
41 | |||
42 | $this->getHelpManager()->setPluginName('vnag_last'); |
||
79 | daniel-mar | 43 | $this->getHelpManager()->setVersion('2023-10-13'); |
2 | daniel-mar | 44 | $this->getHelpManager()->setShortDescription('This plugin checks the logs of the tool "LAST" an warns when users have logged in with an unexpected IP/Country/ISP.'); |
45 | $this->getHelpManager()->setCopyright('Copyright (C) 2011-$CURYEAR$ Daniel Marschall, ViaThinkSoft.'); |
||
46 | $this->getHelpManager()->setSyntax('$SCRIPTNAME$ [-v] [-e] [-u username] [-R regex] [--ipInfoToken token]'); |
||
47 | $this->getHelpManager()->setFootNotes('If you encounter bugs, please contact ViaThinkSoft at www.viathinksoft.com'); |
||
48 | |||
78 | daniel-mar | 49 | $this->cacheFile = $this->get_cache_dir().'/'.hash('sha256','LastCheck:last_ip_cache'); |
59 | daniel-mar | 50 | if (!file_exists($this->cacheFile)) @touch($this->cacheFile); |
2 | daniel-mar | 51 | $this->cache = $this->cacheFile ? json_decode(file_get_contents($this->cacheFile),true) : array(); |
52 | } |
||
53 | |||
54 | public function __destruct() { |
||
55 | if ($this->cacheFile && $this->cacheDirty) { |
||
56 | @file_put_contents($this->cacheFile, json_encode($this->cache)); |
||
57 | } |
||
58 | } |
||
59 | |||
60 | private function getCountryAndOrg($ip) { |
||
61 | if (isset($this->cache[$ip])) return $this->cache[$ip]; |
||
62 | |||
63 | $url = 'https://ipinfo.io/'.urlencode($ip).'/json'; |
||
64 | $token = $this->argIpInfoToken->getValue(); |
||
65 | if ($token) $url .= '?token='.urlencode($token); |
||
66 | |||
67 | // fwrite(STDERR, "Note: Will query $url\n"); |
||
76 | daniel-mar | 68 | $cont = $this->url_get_contents($url); |
69 | if ($cont === false) return array(); |
||
77 | daniel-mar | 70 | if (($data = @json_decode($cont, true)) === false) return array(); |
2 | daniel-mar | 71 | if (isset($data['error'])) return array(); |
72 | |||
73 | if (isset($data['bogon']) && ($data['bogon'])) { |
||
74 | // Things like 127.0.0.1 do not belong to anyone |
||
75 | $res = array(); |
||
76 | } else { |
||
77 | $res = array(); |
||
78 | if (isset($data['hostname'])) $res[] = $data['hostname']; |
||
79 | if (isset($data['country'])) $res[] = $data['country']; |
||
80 | list($as, $orgName) = explode(' ', $data['org'], 2); |
||
81 | $res[] = $as; |
||
82 | $res[] = $orgName; |
||
83 | } |
||
84 | |||
85 | $this->cache[$ip] = $res; |
||
86 | $this->cacheDirty = true; |
||
87 | return $res; |
||
88 | } |
||
89 | |||
90 | private function getLastLoginIPs($username) { |
||
91 | $cont = ''; |
||
92 | $files = glob($this->argWtmpFiles->getValue()); |
||
93 | foreach ($files as $file) { |
||
94 | if (trim($username) == '') { |
||
95 | $cont .= shell_exec('last -f '.escapeshellarg($file).' -F -w '); // all users |
||
96 | } else { |
||
97 | $cont .= shell_exec('last -f '.escapeshellarg($file).' -F -w '.escapeshellarg($username)); |
||
98 | } |
||
99 | } |
||
70 | daniel-mar | 100 | |
2 | daniel-mar | 101 | preg_match_all('@^(\S+)\s+(\S+)\s+(\S+)\s+(.+)$@ismU', $cont, $m, PREG_SET_ORDER); |
102 | foreach ($m as $key => &$a) { |
||
103 | if (($a[2] === 'system') && ($a[3] === 'boot')) { |
||
104 | // reboot system boot 4.9.0-8-amd64 Fri Oct 12 02:10 still running |
||
70 | daniel-mar | 105 | // reboot system boot 6.1.0-11-amd64 Fri Sep 8 13:10:27 2023 - Sat Sep 9 17:40:50 2023 (1+04:30) |
2 | daniel-mar | 106 | unset($m[$key]); |
70 | daniel-mar | 107 | //} else if ($a[2] === 'begins') { |
108 | } else if (substr($a[1],0,4) === 'wtmp') { |
||
109 | // wtmp.1 begins Fri Oct 12 02:10:43 2018 (English) |
||
110 | // wtmp beginnt Wed Aug 16 11:43:03 2023 (German) |
||
2 | daniel-mar | 111 | unset($m[$key]); |
112 | } else { |
||
113 | array_shift($a); |
||
114 | } |
||
115 | } |
||
116 | return $m; |
||
117 | } |
||
118 | |||
119 | protected function cbRun() { |
||
120 | if (!`which which`) { |
||
121 | throw new VNagException("Program 'which' is not installed on your system"); |
||
122 | } |
||
123 | |||
124 | if (!`which last`) { |
||
125 | throw new VNagException("Program 'last' (usually included in package smartmontools) is not installed on your system"); |
||
126 | } |
||
127 | |||
128 | $username = $this->argUser->available() ? $this->argUser->getValue() : ''; |
||
129 | $regex = $this->argRegex->available() ? $this->argRegex->getValue() : null; |
||
130 | |||
131 | if (($username != '') && function_exists('posix_getpwnam') && !posix_getpwnam($username)) { |
||
132 | $this->setStatus(VNag::STATUS_WARNING); |
||
133 | $this->addVerboseMessage("WARNING: Currently, there is no Linux user with name '$username'.", VNag::VERBOSITY_SUMMARY); |
||
134 | } |
||
135 | |||
136 | $count_total = 0; |
||
137 | $count_ok = 0; |
||
138 | $count_warning = 0; |
||
139 | |||
140 | foreach ($this->getLastLoginIPs($username) as list($username, $pts, $ip, $times)) { |
||
36 | daniel-mar | 141 | // IP ":pts/0:S.0" means that there is a screen session |
142 | if (strpos($ip,':pts/') === 0) continue; |
||
143 | |||
2 | daniel-mar | 144 | $count_total++; |
145 | |||
146 | $fields = $this->getCountryAndOrg($ip); |
||
147 | $fields[] = $ip; |
||
148 | |||
149 | if (is_null($regex)) { |
||
150 | // No regex. Just show the logins for information (status stays VNag::STATUS_UNKNOWN) |
||
151 | $this->addVerboseMessage("INFO: ".implode(' ',$fields)." @ $username, $pts $times", VNag::VERBOSITY_ADDITIONAL_INFORMATION); |
||
152 | } else { |
||
153 | $match = false; |
||
154 | foreach ($fields as $f) { |
||
155 | if (preg_match($regex, $f, $dummy)) { |
||
156 | $match = true; |
||
157 | break; |
||
158 | } |
||
159 | } |
||
160 | |||
161 | if ($match) { |
||
162 | $count_ok++; |
||
163 | $this->addVerboseMessage("OK: ".implode(' ',$fields)." @ $username $pts $times", VNag::VERBOSITY_ADDITIONAL_INFORMATION); |
||
164 | $this->setStatus(VNag::STATUS_OK); |
||
165 | } else { |
||
166 | $count_warning++; |
||
167 | $this->addVerboseMessage("WARNING: ".implode(' ',$fields)." @ $username $pts $times", VNag::VERBOSITY_SUMMARY); |
||
168 | $this->setStatus(VNag::STATUS_WARNING); |
||
169 | } |
||
170 | } |
||
171 | } |
||
172 | |||
173 | if (is_null($regex)) { |
||
174 | $this->setHeadline("Checked $count_total logins (No checks done because argument 'Regex' is missing)"); |
||
175 | } else { |
||
176 | if (($count_total == 0) && ($this->argEmptyOk->count() > 0)) { |
||
177 | $this->setStatus(VNag::STATUS_OK); |
||
178 | } |
||
179 | $this->setHeadline("Checked $count_total logins ($count_ok OK, $count_warning Warning)"); |
||
180 | } |
||
181 | } |
||
182 | } |