Daemons are background programs that run without any user interaction. The terminology comes from Maxwell's demon, an imaginary entity that works in the background to help with experiments.
Any non-privileged user can query a daemon status through systemctl.
Querying the SSH daemon status
[offsec@linux02 ~]$ systemctl status sshd
● sshd.service - OpenSSH server daemon
Loaded: loaded (/usr/lib/systemd/system/sshd.service; enabled; vendor preset: enabled)
Active: inactive (dead) since Tue 2021-06-15 09:52:57 CEST; 2s ago
Docs: man:sshd(8)
man:sshd_config(5)
...
Starting the SSH daemon
[offsec@linux02 ~]$ sudo systemctl start sshd
[sudo] password for offsec:
Verifying the SSH daemon status
[offsec@linux02 ~]$ systemctl status sshd
● sshd.service - OpenSSH server daemon
Loaded: loaded (/usr/lib/systemd/system/sshd.service; enabled; vendor preset: enabled)
Active:active (running) since Tue 2021-06-15 09:53:55 CEST; 4s ago
Docs: man:sshd(8)
man:sshd_config(5)
Main PID: 78962 (sshd)
Tasks: 1 (limit: 4627)
Memory: 1.3M
CGroup: /system.slice/sshd.service
└─78962 /usr/sbin/sshd -D -oCiphers=aes256-gcm@openssh.com,chacha20-poly1305@openssh.com,aes256-ctr,aes256-cbc,aes128-gcm@openssh.com,aes128->
Logging on Linux and the Syslog Framework
Log files are saved w ithin the /var/log folder and named after their category/role.
Linux log files locations
Purpose
Source Process
CentOS Location
Ubuntu Location
Authentication
sudo, sshd, etc.
secure
auth.log
Web Server
apache
httpd/
apache2/
System Logs
systemd,kernel, rsyslogd
messages
syslog
Package management Logs
dpkg
yum.log
dpkg.log
Raw log example of ssh attempt
[offsec@linux02 ~]$ sudo grep sshd /var/log/secure
...
Jun 28 11:22:55 linux02 sshd[156299]: pam_unix(sshd:session): session opened for user offsec by (uid=0)
...
Rsyslog configuration supporting RFC3164 translation and multiple optional transport protocols
...
#### RULES ####
$template RFC3164fmt,"<%PRI%>%TIMESTAMP% %HOSTNAME% %syslogtag%%msg%\n"
...
# The authpriv file has restricted access.
authpriv.* /var/log/secure;RFC3164fmt
...
# Forwarding to remote syslog collectors
# ----------------------------
#*.* @linux01 # udp transport
#*.* @@linux01 # tcp transport
Inspecting the SSH log event for failed login
[offsec@linux02 ~]$ sudo cat /var/log/secure | grep "Failed password"
<86>Jun 28 12:05:21 linux02 sshd[157165]:Failed password for offsec from 192.168.51.50 port 54209 ssh2
The event above is structed as so: Priority, Timestamp, Hostname, App Name, Process ID, Message.
Syslog Facilities Codes
Facility code
Keyword
Description
0
kern
Kernel messages
1
user
User-level messages
2
mail
Mail system
3
daemon
System daemons
4
auth
Security/authentication messages
5
syslog
Messages generated internally by syslogd
6
lpr
Line printer subsystem
7
news
Network news subsystem
8
uucp
UUCP subsystem
9
cron
Cron subsystem
10
authpriv
Security/authentication messages
11
ftp
FTP daemon
12
ntp
NTP subsystem
13
security
Log audit
14
console
Log alert
15
solaris-cron
Scheduling daemon
16–23
local0 – local7
Locally used facilities
Syslog Severity Levels
Value
Severity
Keyword
Description
0
Emergency
emerg
System is unusable - A panic condition
1
Alert
alert
Action must be taken immediately
2
Critical
crit
Critical conditions
3
Error
err
Error conditions
4
Warning
warning
Warning conditions
5
Notice
notice
Normal but significant conditions
6
Informational
info
Informational messages
7
Debug
debug
Debug-level messages
Rsyslog Meets Journal
By default, systemd_journald, or journal is responsible for processing log events first.
Inspecting Journal Logs
[offsec@linux02 ~]$ journalctl -u sshd.service --since "1 hour ago"
-- Logs begin at Tue 2021-06-01 16:05:01 CEST, end at Tue 2021-06-22 15:00:31 CEST. --
Jun 22 15:00:31 linux02 sshd[131733]: Accepted password for offsec for offsec from 192.168.51.50 port 58379 ssh2
# Loop through each file in our array
for log_file in ssh_log_files:
# If the file exists, open it in read mode
if os.path.isfile(log_file):
with open(log_file, "r") as file:
# For each line in the file, search for the regex pattern
for line in file:
for match in re.finditer(regex_pattern, line, re.S):
# If pattern is found, print the line without adding a newline character
print(line, end='')
DevOps Tools
DevOps is an effort to combine traditional development practices and operational strategies into a joint mechanism that focuses on orchestration, automation, and consistency.
There are a few options available, such as Puppet, Chef, Ansible, etc.
kali@attacker01:~/SOC-200/Linux_Endpoint_Introduction$ ansible-playbook ./log_parser.yml -u offsec --key-file='/home/kali/.ssh/ansible_rsa' -K
BECOME password:
PLAY [logparser] *************************************************************************************************************************************************************************************
TASK [Gathering Facts] *******************************************************************************************************************************************************************************
ok: [192.168.51.12]
ok: [192.168.51.13]
TASK [list files in folder] **************************************************************************************************************************************************************************
changed: [192.168.51.12]
changed: [192.168.51.13]
TASK [debug] *****************************************************************************************************************************************************************************************
ok: [192.168.51.12] => {
"output.stdout_lines": [
"",
"Jun 15 13:13:36 linux02 sshd[81613]: Failed password for offsec from 192.168.51.50 port 60040 ssh2",
"Jun 16 09:11:28 linux02 sshd[84486]: Accepted password for offsec from 192.168.51.50 port 51741 ssh2"
]
}
ok: [192.168.51.13] => {
"output.stdout_lines": [
"",
"Jun 16 09:16:11 linux01 sshd[47847]: Accepted password for offsec from 192.168.51.50 port 55660 ssh2",
]
}
PLAY RECAP *******************************************************************************************************************************************************************************************
192.168.51.12 : ok=3 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
192.168.51.13 : ok=3 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
Ideally, we would parse distributed log files with a full-fledged SIEM solution. However, what we've practiced here can be useful as an initial proof-of-concept or a small-scaled log parsing alternative.