{"id":178,"date":"2021-04-01T10:57:00","date_gmt":"2021-04-01T02:57:00","guid":{"rendered":"http:\/\/feel.name\/?p=178"},"modified":"2021-04-01T10:57:00","modified_gmt":"2021-04-01T02:57:00","slug":"%e9%98%b2%e6%ad%a2%e6%81%b6%e6%84%8f%e6%89%ab%e6%8f%8fssh%e7%99%bb%e9%99%86","status":"publish","type":"post","link":"http:\/\/feel.name\/?p=178","title":{"rendered":"\u9632\u6b62\u6076\u610f\u626b\u63cfssh\u767b\u9646"},"content":{"rendered":"\n<p>\u811a\u672c1\uff1a<\/p>\n\n\n\n<p>#!\/bin\/bash #\u7edf\u8ba1\u5c1d\u8bd5\u767b\u9646\u6b21\u6570<br>cat \/var\/log\/secure | grep &#8220;Failed password&#8221; | awk &#8216;{ print $(NF-3)}&#8217; | sort -n | uniq -c | awk &#8216;{ print $2&#8243;=&#8221;$1}&#8217; &gt; abc.txt<br>#\u5f97\u5230\u626b\u63cf\u5931\u8d25\u7684ip\u5730\u5740<br>ipaddr=($(cat \/var\/log\/secure | grep &#8220;Failed password&#8221; | awk &#8216;{ print $(NF-3)}&#8217; | sort -n | uniq -c | awk &#8216;{ print $2&#8243;=&#8221;$1}&#8217;))<br>for i in ${ipaddr[@]} ; do<br>&nbsp; &nbsp; &nbsp; &nbsp; NUM=$(cat abc.txt | awk &#8216;{print $1}&#8217;)<br>&nbsp; &nbsp; &nbsp; &nbsp; IP=$(cat abc.txt | awk &#8216;{print $2}&#8217;)<br>&nbsp; &nbsp; &nbsp; &nbsp; if [[ $NUM &gt; 15 ]]; #\u5f97\u5230\u626b\u63cf\u8d85\u8fc715\u6b21\u7684ip\u5730\u5740<br>&nbsp; &nbsp; &nbsp; &nbsp; then<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; grep $IP \/etc\/hosts.deny &gt; \/dev\/null<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if [[ $? != 0 ]]; #\u5224\u65ad\u8be5ip\u662f\u5426\u5728\/etc\/hosts.deny\u4e2d<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; then<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo &#8220;sshd:$IP&#8221; &gt;&gt; \/etc\/hosts.deny<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fi<br>&nbsp; &nbsp; &nbsp; &nbsp; fi<\/p>\n\n\n\n<p>done<\/p>\n\n\n\n<p>\u53e6\u4e00\u79cd\u5199\u6cd5\uff1a<\/p>\n\n\n\n<p>#\/bin\/bash<br>##deny ssh from invalid remote client\u00a0<br>grep &#8220;Failed password&#8221; \u00a0\/var\/log\/secure | awk &#8216;{ print $(NF-3)}&#8217; | sort -n | uniq -c > \/tmp\/ssh_log<br><br><br>while read num ip<br>do<br>\u00a0 \u00a0 if (($num > 15))<br>\u00a0 \u00a0 then<br>\u00a0 \u00a0 \u00a0 \u00a0 grep $ip \/var\/log\/secure &amp;>\/dev\/null<br>\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 if [[ $? != 0 ]]<br>\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 then<br>\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 echo &#8220;sshd:$ip&#8221; >> \/etc\/host.deny<br>\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 fi<br>\u00a0 \u00a0 fi<br>done &lt; \/tmp\/ssh_log<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>\u67e5\u8be2\u54ea\u4e2aip\u626b\u63cf\u6bd4\u8f83\u591a\u7684<\/p>\n\n\n\n<p>cat \/var\/log\/secure* | grep &#8220;Failed password&#8221; | awk &#8216;{ print $(NF-3)}&#8217; | sort -n | uniq -c<\/p>\n\n\n\n<p>grep &#8220;Failed password&#8221; \/var\/log\/secure* | awk &#8216;{ print $(NF-3)}&#8217; | sort -n | uniq -c<\/p>\n\n\n\n<p>\u8f6c\u8f7d\u4e8e:https:\/\/my.oschina.net\/fengjihu\/blog\/192464<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u811a\u672c1\uff1a #!\/bin\/bash #\u7edf\u8ba1\u5c1d\u8bd5\u767b\u9646\u6b21\u6570cat \/var\/log\/secure | grep &#038;#&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[],"class_list":["post-178","post","type-post","status-publish","format-standard","hentry","category-linux"],"_links":{"self":[{"href":"http:\/\/feel.name\/index.php?rest_route=\/wp\/v2\/posts\/178","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/feel.name\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/feel.name\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/feel.name\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/feel.name\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=178"}],"version-history":[{"count":1,"href":"http:\/\/feel.name\/index.php?rest_route=\/wp\/v2\/posts\/178\/revisions"}],"predecessor-version":[{"id":179,"href":"http:\/\/feel.name\/index.php?rest_route=\/wp\/v2\/posts\/178\/revisions\/179"}],"wp:attachment":[{"href":"http:\/\/feel.name\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=178"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/feel.name\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=178"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/feel.name\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=178"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}