Linux安全篇-审计
tcpdump
1.了解数据包的构成,并且通过数据包的走向来判断一些常见的网络问题
2.通过抓报,分析数据包,并判断是否可疑
tcpdump -i eth0 指定接口
tcpdump -i eth0 -v -n
-v 显示包含有TTL,TOS值等等更详细的信息
-n不要做IP解析为主机名
-nn不做名字解析和端口解析
-s 指定大小
-X显示内容默认前96字节 1500以内就可以
-w 指定保存的位置
-c 指定抓机关包
-r读抓包的保存文件内容
更有针对性的抓包:
针对IP,网段,端口,协议
[root@ ftp]# tcpdump -i eth0 -vnn host 192.168.0.154
[root@ ftp]# tcpdump -i eth0 -vnn net 192.168.0.0/24
[root@ ftp]# tcpdump -i eth0 -vnn port 22
[root@ ftp]# tcpdump -i eth0 -vnn udp
[root@ ftp]# tcpdump -i eth0 -vnn icmp
[root@ ftp]# tcpdump -i eth0 -vnn arp
[root@ ftp]# tcpdump -i eth0 -vnn ip
[root@ ftp]# tcpdump -i eth0 -vnn src host 192.168.0.154
[root@ ftp]# tcpdump -i eth0 -vnn dst host 192.168.0.154
[root@ ftp]# tcpdump -i eth0 -vnn src port 22源IP
[root@ ftp]# tcpdump -i eth0 -vnn src host 192.168.0.253 and dst port 22
tcpdump -i eth0 -nn dst port 22 and dst host.192.168.0.253
[root@ ftp]# tcpdump -i eth0 -vnn src host 192.168.0.154 or port 22
[root@ ftp]# tcpdump -i eth0 -vnn src host 192.168.0.154 and not port 22
]# tcpdump -i eth0 -vnn \( src host 192.168.0.2 and dst port 22 \) or \( src host 192.168.0.65 and dst port 80 \)
]#tcpdump -i eth0 -vnn ‘src host 192.168.0.2 and dst port 22 ‘ or ‘ src host 192.168.0.65 and dst port 80 ‘
]#tcpdump -i eth0 -vnn not port 5900 and not icmp
[root@ ~]# tcpdump -ieth0 -vnn -r /tmp/fil1 tcp
[root@ ~]# tcpdump -ieth0 -vnn -r /tmp/fil1 host 192.168.0.58
[root@ ~]# tcpdump -ieth0 -vnn -r /tmp/fil1 tcp
[root@ ~]# tcpdump -ieth0 -vnn -w /tmp/fil1 -c 100
wireshark
[root@ ~]# rpm -qa |grep wireshark
wireshark-gnome-1.0.3-4.el5_2
wireshark-1.0.3-4.el5_2
sniffer windows里面的工具
入侵检测:
HIDS:基于主机入侵检测
1.日志
生成日志 /etc/syslog.conf 大多数在/var/log下 /var/log/secure 安全日志
分析日志 last登录本机的 lastlog 本机用户最近登录
保护日志 记录到远程主机上
vim /etc/syslog.conf
*..* @192.168.0.188
service syslog restart
0.188主机上
vim /etc/sysyconfig/syslog
6行 SYSLOGD_OPTIONS=" -m -r -x" 允许远程记录日志 ,不作名字解析
service syslog restart
本地登录上的日志同样在0.188上也记录一份
0.188记载的信息和0.253记载的信息目录是一致的
2.文件系统的完整性
NIDS:基于网络入侵检测
[root@tch ~]# logger -t apache -p local5.info "123"
[root@tch ~]# echo local5.info /var/log/testlog >> /etc/syslog.conf
[root@tch ~]# service syslog restart >> /dev/null
[root@tch ~]# logger -t apache -p local5.info "123"[root@tch ~]# tail /var/log/testlog
Apr 29 12:16:44 tch apache: 123
----------------------
[root@tch ~]# vim /etc/httpd/conf/httpd.conf
[root@tch ~]# grep logger !$
grep logger /etc/httpd/conf/httpd.conf
CustomLog "| logger -t apache -p local4.info" combined
[root@tch ~]# echo local4.info /var/log/apachelog >> /etc/syslog.conf
[root@tch ~]# service syslog restart >> /dev/null
[root@tch ~]# service httpd restart >> /dev/null
[root@tch ~]# tail /var/log/apachelog
Apr 29 12:24:09 tch apache: 192.168.0.254 - - [29/Apr/2010:12:24:09 +0800] "GET /favicon.ico HTTP/1.1" 404 288 "-" "Mozilla/5.0 (X11; U; Linux i686; zh-CN; rv:1.9.0.5)
Gecko/2008120908 Red Hat/3.0.5-1.el5_2 Firefox/3.0.5"
last 查看系统中的每个用户最近登录时间
lastlog 查看最近一个月的系统被登录日志。可以看远程
记录到远程主机
1、日志备份
2、避免日志被修改,或者说被可疑者清除掉
0.253需要把日志记录到远程主机0.2上配置如下:
192.168.0.253:
[root@ linux]# grep '0\.2' /etc/syslog.conf
*.info @192.168.0.2
[root@ linux]# /etc/init.d/syslog restart
192.168.0.2:
[root@mail ~]# grep -- '\-r' /etc/sysconfig/syslog --表示后面没有选项
# -r enables logging from remote machines
# -x disables DNS lookups on messages recieved with -r
SYSLOGD_OPTIONS="-m 0 -r -x"
[root@mail ~]# /etc/init.d/syslog restart
配置完成后,测试结果:
192。168。0。253上产生日志:
[root@ linux]# logger "test for syslog remote logging "
到192。168。0。2上看日志是否被记录过来了。
[root@mail ~]# grep "test for syslog " /var/log/messages
Jul 30 13:43:13 192.168.0.253 root: test for syslog remote logging
成功!!!
生成日志 /etc/syslog.conf 大多数在/var/log下,要看 /var/log/secure 安全日志
分析日志 last登录本机的 lastlog 本机用户最近登录
保护日志 记录到远程主机上
vim /etc/syslog.conf
*.* @192.168.0.188
service syslog restart
0.188主机上
vim /etc/sysyconfig/syslog
6行 SYSLOGD_OPTIONS=" -m -r -x" 允许远程记录日志 ,不作名字解析
service syslog restart
本地登录上的日志同样在0.188上也记录一份
0.188记载的信息和0.253记载的信息目录是一致的
有iptables
允许514 端口就好
iptables -A INPUT -s 192.168.0.253 -p udp --dport 514 -j ACCEPT
日志
1.内核日志 syslog记录或者应用程序是自己记录的
1)应用程序怎么向远程主机备份 远程主机设置ssh自动化登录 scp拷贝日志
2)rsync
#/bin/bash
while :
do
scp -r /var/log 192.168.0.188:/tmp/` date +%Y-%m-%d"-"%H:%M`
sleep 120
done
文件完整性检查
1 为了看重要文件 内容是否被非法用户篡改
2 作为可疑的操作的判断依据
1.手动通过脚本检查
1.查看md5值
通过生成md5值来检查:
]# md5sum /etc/passwd
cae048d9fe5cd181c91855d8932db610 /etc/passwd
通过生成sha1值来检查
]# sha1sum /etc/passwd
e16f58be68a697a44c849eb346be9b3d6150b040 /etc/passwd
自已写脚本
[root@mail ~]# find /etc -type f -exec md5sum {} \; //生成MD5值
[root@mail ~]# find /etc -type f -exec md5sum {} \; >/tmp/etc_listl //备份
[root@mail ~]# find /lib/modules/2.6.18-128.el5/kernel/ -type f -exec md5sum {} \;
#!/bin/bash
2
3 for i in `echo $PATH| sed 's/:/ /g'`
4 do
5 if [ -d $i ]
6 then
7 find $i -type f -exec md5sum {} \; >> /root/md5.txt
find $i -perm -4000 >>/root/suid.txt
8 fi
9 done
-4000
4(至少有4个权限0表示所有者组其他可有可无 - 是至少的意思)
xargs ls -l
2.大小
3.相关属性的变化
2.rpm 只针对rpm安装的包生成的文件
rpm -V packagename 检查这个包自安装以来,生成的所有文件,是否有相关的改变
rpm -Vf /etc/passwd 检查此命令对应的包安装完成后生成的所有文件的变化
rpm -aV 检查所有rpm生成的文件的变化
--verify test:
S file Size differs 大小
M Mode differs (includes permissions and file type) 权限
5 MD5 sum differs 内容
D Device major/minor number mismatch 主从设备号
L readLink(2) path mismatch 符号连接的原始文件
U User ownership differs 所属者
G Group ownership differs 所属组
T mTime differs 修改时间
[root@ linux]# ls -l /usr/src/linux -d
lrwxrwxrwx 1 root root 54 07-30 11:09 /usr/src/linux -> /usr/src/redhat/BUILD/kernel-2.6.18/linux-2.6.18.i686/
[root@ linux]# readlink /usr/src/linux
/usr/src/redhat/BUILD/kernel-2.6.18/linux-2.6.18.i686/
readlink /bin/sh
bash
3.自己装的软件通过第三方工具
tripwire 文件完整性检查文件
tripware ---- 本周作业 !!!!
用户权限
clamav 杀毒软件
文件完整性检查
1 为了看重要文件 内容是否被非法用户篡改
2 作为可疑的操作的判断依据
1.手动通过脚本检查
1.查看md5值
通过生成md5值来检查:
]# md5sum /etc/passwd
cae048d9fe5cd181c91855d8932db610 /etc/passwd
通过生成sha1值来检查
]# sha1sum /etc/passwd
e16f58be68a697a44c849eb346be9b3d6150b040 /etc/passwd
自已写脚本
[root@mail ~]# find /etc -type f -exec md5sum {} \; //生成MD5值
[root@mail ~]# find /etc -type f -exec md5sum {} \; >/tmp/etc_listl //备份
[root@mail ~]# find /lib/modules/2.6.18-128.el5/kernel/ -type f -exec md5sum {} \;
#!/bin/bash
2
3 for i in `echo $PATH| sed 's/:/ /g'`
4 do
5 if [ -d $i ]
6 then
7 find $i -type f -exec md5sum {} \; >> /root/md5.txt
find $i -perm -4000 >>/root/suid.txt
8 fi
9 done
-4000
4(至少有4个权限0表示所有者组其他可有可无 - 是至少的意思)
xargs ls -l
2.大小
3.相关属性的变化
2.rpm 只针对rpm安装的包生成的文件
rpm -V packagename 检查这个包自安装以来,生成的所有文件,是否有相关的改变
rpm -Vf /etc/passwd 检查此命令对应的包安装完成后生成的所有文件的变化
rpm -aV 检查所有rpm生成的文件的变化
--verify test:
S file Size differs 大小
M Mode differs (includes permissions and file type) 权限
5 MD5 sum differs 内容
D Device major/minor number mismatch 主从设备号
L readLink(2) path mismatch 符号连接的原始文件
U User ownership differs 所属者
G Group ownership differs 所属组
T mTime differs 修改时间
[root@ linux]# ls -l /usr/src/linux -d
lrwxrwxrwx 1 root root 54 07-30 11:09 /usr/src/linux -> /usr/src/redhat/BUILD/kernel-2.6.18/linux-2.6.18.i686/
[root@ linux]# readlink /usr/src/linux
/usr/src/redhat/BUILD/kernel-2.6.18/linux-2.6.18.i686/
readlink /bin/sh
bash
3.自己装的软件通过第三方工具
tripwire 文件完整性检查文件
tripware ---- 本周作业 !!!!
用户权限
clamav 杀毒软件
Audit
审计是做什么?
是在对文件的操作做审计,这些操作不管是用vim,cp,rm,cat,底层来讲都是一些定义好的系统调用。
将审计的日志记录到日志的/var/log/audit/audit.log 审计日志
man 2 syscalls 查看内核支持的所有系统调用 。
open
write
read
close
规则怎么写?
/etc/init.d/auditd start
自己根据需要去写审计规则:
man auditctl
auditctl 认为指定审计规则 添加a追加A
exit系统调用退出记录消息
entry 进入记
always记录日志
never 不记录
task 指定进程和线程
clone创建线程
fork创建进程
auditctl -a task ,always -w /mount -S all -F auid=500 -k "FORK"
删除
auditctl -d task ,always -w /mount -S all -F auid=500 -k "FORK"
-D清空
-d 删除
-F 指定字段 系统调用 auid原始登录ID有效udid gid euid egid
-p perm
-w path
-S 名字
w 监控
[root@ linux]# auditctl -l 查看规则
No rules
[root@ linux]# auditctl -s
AUDIT_STATUS: enabled=0 flag=1 pid=0 rate_limit=0 backlog_limit=64 lost=10861 backlog=0
[root@ linux]# auditctl -e 1
AUDIT_STATUS: enabled=1 flag=1 pid=0 rate_limit=0 backlog_limit=64 lost=10861 backlog=0
success=0
0失败
1成功
auditctl
enabled=1 必须要是1/var/log/audit/audit.log
守护进程 auditd
看消息
1.tail /var/log/audit/audit.log 不能显示时间戳
2.ausearch -f
auditctl -a exit,always -S(指定是什么系统调用) open -F path=/etc/shadow
auditctl -a exit,always -S open -F path=/etc/shadow -k "SHADOW"
[root@stu154 ~]#ausearch -k “SHADOW“ 指定过滤的关键字
[root@stu154 ~]#ausearch -f /etc/shadow 基于文件名字 显示时间和---分割符item name= ouid ogid mode 权限后3个数字 inode 文件编号
cwd 在那个目录环境操作的
syscall= 操作编号 5 设置密码的操作
pid cat
ppid exe /bin/cat
ouid 所偶这
有效的euid egid 没有设置suid位和sgid位 和他的组号一致
位置 pts/3
对于系统,有如下操作需要审计
1.对/etc/shadow -------------- /var/log/secure
auditctl -a exit,always -S open -F success=0 -Fpath=/etc/shadow -k "OPEN SHADOW FAIL"
对/etc/shadow 文件的open失败记录日志
auditctl -a exit,always -S open -F write -F path=/etc/shadow -k "WRITE SHADOW"
auditctl -a exit,always -S all -F write -F path=/etc/shadow auid!=0 -k "NO ROOT"
auditctl -a exit,always -S all -F write -F path=/etc/shadow auid '>'0 -k "NO ROOT"
auditctl -a exit,always -S open -F write -F path=/etc/shadow -k "OPEN SHADOW FAIL"
#!/bin/awk -f
BEGIN{
FS=" "
}
/sshd.*failure/{
if(NF==14){
print $1,$2,$3,$4,$5,$NF
}else if(NF==16){
print $1,$2,$3,$4,$5,$NF
}else{
print $1,$2,$3,$4,$5,$(NF-1),$NF
}
}
#uniq -f 4 file
rootkit 1.隐藏踪迹2. 留有后门下次能够再次登录 有内核级别 和应用程序级别的
审计规则
1.可执行文件
2.模块
3.生成MD5值
]#grep SHADOW /var/log/audit/audit.log
[root@stu154 ~]#auditctl -a entry,always -S all -F pid=3070 -k "SSHD" 想看看谁在什么时候SSHD登录过!
[root@stu154 ~]#auditctl -a exit,always -S all -F uid!=0 对于所有用户(除root外)
[root@stu154 ~]#auditctl -a exit,always -S open -S write -F uid!=0
只要是UID不为0的用户对/etc/shadow文件做了open,write的操作都会审计记录下来:
[root@stu154 ~]#auditctl -a exit,always -S open -S write -F uid!=0 -F path=/etc/shadow -k "NOROOTUSER"
[root@stu154 ~]#ausearch -k "NOROOTUSER"
[root@stu154 ~]#ausearch -ui 1008
[root@stu154 ~]#ausearch -sv no 按照程序执行的返回值来 有yes 和no两种
对于etc目录中的文件有写操作时,就记录下来!!!
[root@stu154 ~]#auditctl -w /etc -p w -k "PERM"
实例:
需要对一些常用的命令做审计:
ls bash ps insmod
对于系统的命令审计,已避免ls ps 这样的命令替换
-w相当于-F dir=(目录)
perm(p)
-p r|w|x|a( a属性变化)
-a 追加
]#auditctl -w /bin -p aw -k "SBIN"
]#auditctl -w /usr/bin -p aw -k " USER BIN"
]#auditctl -w /bin -p aw -k "SBIN"
等价于
]#auditctl -a exit,always -F dir=/bin -F perm=rwa -k "BIN"
3.对于/var/log要做审计
]#auditctl -w /lib/modul/2.x.x.xx /kernel
]#auditctl -aesxit,always -S all -F path=/bin/insmod -F perm=rwa
]#auditctl -a exit,always -S all -F path=/bin/insmod -F perm=rwa
]#auditctl -a exit,always -S all -F path=/bin/ps -F perm=rwa
]#auditctl -a exit,always -S all -F path=/bin/ls -F perm=rwa
]#auditctl -a exit,always -S all -F path=/bin/bash -F perm=rwa
-----------------------------------
对/lib/modules/$(uname -r)
[root@mail ~]# auditctl -a exit,always -S write -F dir=/lib/modules/2.6.18-128.el5/
规则要永久生效必须要些到下面的文件
/etc/audit/audit.rules
从前面加tab键 -a后面复制
-a always,exit -S all -F dir=/usr/bin -F perm=rwa -k USR
-w /usr/bin -p aw -k USRBIN
要查看man手册
重启服务
网络入侵检测
开源的软件snort
1.snort嗅探模式
1005 snort -v -i eth0 对TCP/IP 协议包的信息网络层的信息 传输层的信息
-d 更多的信息 应用层的信息
-e 数据链路层
-V 表示显示IP协议和TCP协议的信息
snort -vd -i eth0 接口
snort -vde -i eth0
snort -vde -i eth0 host 192.168.0.254
snort -vde -i eth0 dst host 192.168.0.254
snort -vde -i eth0 net 192.168.0.0/24
snort -vde -i eth0 dst net 192.168.0.0/24
snort -vde -i eth0 src host 192.168.0.254
snort -vde -i eth0 arp src host 192.168.0.254 arp协议的包
man snort
snort -vde -i eth0 tcp and net 192.168.0.0/24
snort -vde -i eth0 arp net 192.168.0.0/24
snort -vde -i eth0 icmp and net 192.168.0.0/24
history
[root@tch ~]# snort -vde -i eth0 src port 21 and host 192.168.0.254
[root@tch ~]# snort -vde -i eth0 src port 21 or host 192.168.0.254
[root@tch ~]# snort -vde -i eth0 not port 21
[root@tch ~]# snort -vde -i eth0 src port 21 and \( not host 192.168.0.254 \)
2.snort日志分组模式包记录器
输出插件可以输出到哪里?
]# snort -vde -i eth0 not port 21 -l /var/log/snort指定存在哪里
]# snort -vde not port 21 -r -l /var/log/snort.log
]# ls -l /var/log/snort.log.1273112320
-rw------- 1 root root 7208596 05-06 10:19 /var/log/snort.log.1273112320
[root@tch ~]# file !$
file /var/log/snort.log.1273112320
/var/log/snort.log.1273112320: tcpdump capture file (little-endian) - version 2.4 (Ethernet, capture length 1514)
[root@tch ~]#
1050 snort -vde -r /var/log/snort.log.1273112320
1051 snort -vde -r /var/log/snort.log.1273112320 host 192.168.0.254
[root@tch ~]# tcpdump src host 192.168.0.254 -r /var/log/snort.log.1273112320
3.网络入侵检测模式 分析将可疑数据记录到告警日志中
规则集
怎么定义规则集?
匹配后面的条件就报警
内容 content
alter all<>
var定义变量
端口变量Portvar
IP变量ipvar
any any 任意IP 任意端口
alter tcp any any ->192.168.0.253 21 (content:"USER pg";msg:"user pg attempt login")
alert tcp any any -> 192.168.1.0/24 111 (content:"|00 01 86 a5|"; msg:"mountd access";)
安装:rpm -ivh snort-2.8.6-1.RH5.i386.rpm
vim/etc/snort
rules规则集
snort.conf 配置文件
是自己写 还是用别人的:
snort团队开发文件可以下载
man snort
-c 配置文件 定义工作模式
kill %%
jobs
data -s
nohup