安全运维笔记


日志分析笔记

<h4><strong>日志分析</strong></h4> <h4>一、ssh登录日志</h4> <p><strong>1、定位有多少IP在爆破主机的root帐号: </strong><br /> grep &quot;Failed password for root&quot; /var/log/secure | awk '{print $11}' | sort | uniq -c | sort -nr | more <strong>2、定位有哪些IP在爆破:</strong> grep &quot;Failed password&quot; /var/log/secure|grep -E -o &quot;(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?). (25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?).(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?).(25[0-5]|2[0-4] [0-9]|[01]?[0-9][0-9]?)&quot;|uniq -c <strong>3、爆破用户名字典是什么?</strong> grep &quot;Failed password&quot; /var/log/secure|perl -e 'while($_=<>){ /for(.*?) from/; print &quot;$1\n&quot;;}'|uniq -c|sort -nr 2、登录成功的IP有哪些: grep &quot;Accepted &quot; /var/log/secure | awk '{print $11}' | sort | uniq -c | sort -nr | more <strong>4、登录成功的日期、用户名、IP: </strong> grep &quot;Accepted &quot; /var/log/secure | awk '{print $1,$2,$3,$9,$11}' </p> <h4>二、web日志分析</h4> <p>检索关键字:cat +文件名 | grep ‘关键字’</p> <h4>2.1apache日志分析</h4> <p>Apache日志分析技巧: 1、列出当天访问次数多的IP命令: cut -d- -f 1 log_file|uniq -c | sort -rn | head -20 </p> <p>2、查看当天有多少个IP访问: awk '{print $1}' log_file|sort|uniq|wc -l </p> <p>3、查看某一个页面被访问的次数: grep &quot;/index.php&quot; log_file | wc -l </p> <p>4、查看每一个IP访问了多少个页面: awk '{++S[$1]} END {for (a in S) print a,S[a]}' log_file </p> <p>5、将每个IP访问的页面数进行从小到大排序: awk '{++S[$1]} END {for (a in S) print S[a],a}' log_file | sort -n </p> <p>6、查看某一个IP访问了哪些页面: grep ^111.111.111.111 log_file| awk '{print $1,$7}' </p> <p>7、去掉搜索引擎统计当天的页面: awk '{print $12,$1}' log_file | grep ^\&quot;Mozilla | awk '{print $2}' |sort | uniq | wc -l </p> <p>8、查看2018年6月21日14时这一个小时内有多少IP访问: awk '{print $4,$1}' log_file | grep 21/Jun/2018:14 | awk '{print $2}'| sort | uniq | wc -l 9、统计爬虫: grep -E 'Googlebot|Baiduspider' /www/logs/access.2019-02-23.log | awk '{ print $1 }' | sort | uniq 10、统计浏览器: cat /www/logs/access.2019-02-23.log | grep -v -E 'MSIE|Firefox|Chrome|Opera|Safari|Gecko|Maxthon' | sort | uniq -c | sort -r -n | head -n 100<br /> 11、IP 统计: grep '23/May/2019' /www/logs/access.2019-02-23.log | awk '{print $1}' | awk -F'.' '{print $1&quot;.&quot;$2&quot;.&quot;$3&quot;.&quot;$4}' | sort | uniq -c | sort -r -n | head -n 10 2206 219.136.134.13 1497 182.34.15.248 1431 211.140.143.100 1431 119.145.149.106 1427 61.183.15.179 1427 218.6.8.189 1422 124.232.150.171 1421 106.187.47.224 1420 61.160.220.252 1418 114.80.201.18<br /> 12、统计网段: cat /www/logs/access.2019-02-23.log | awk '{print $1}' | awk -F'.' '{print $1&quot;.&quot;$2&quot;.&quot;$3&quot;.0&quot;}' | sort | uniq -c | sort -r -n | head -n 200<br /> 13、统计域名: cat /www/logs/access.2019-02-23.log |awk '{print $2}'|sort|uniq -c|sort -rn|more<br /> HTTP Status: cat /www/logs/access.2019-02-23.log |awk '{print $9}'|sort|uniq -c|sort -rn|more 5056585 304 1125579 200 7602 400 5 301<br /> 14、URL 统计: cat /www/logs/access.2019-02-23.log |awk '{print $7}'|sort|uniq -c|sort -rn|more<br /> 15、文件流量统计: cat /www/logs/access.2019-02-23.log |awk '{sum[$7]+=$10}END{for(i in sum){print sum[i],i}}'|sort -rn|more </p> <p>grep ' 200 ' /www/logs/access.2019-02-23.log |awk '{sum[$7]+=$10}END{for(i in sum){print sum[i],i}}'|sort -rn|more<br /> 16、URL访问量统计: cat /www/logs/access.2019-02-23.log | awk '{print $7}' | egrep '\?|&amp;' | sort | uniq -c | sort rn | more<br /> 17、脚本运行速度: 查出运行速度慢的脚本 grep -v 0$ /www/logs/access.2019-02-23.log | awk -F '\&quot; ' '{print $4&quot; &quot; $1}' web.log | awk '{print $1&quot; &quot;$8}' | sort -n -k 1 -r | uniq &gt; /tmp/slow_url.txt<br /> IP, URL 抽取: ****# tail -f /www/logs/access.2019-02-23.log | grep '/test.html' | awk '{print $1&quot; &quot;$7}'</p> <h4>2.2nignx日志分析</h4>

页面列表

ITEM_HTML