{"id":376,"date":"2009-09-13T08:18:41","date_gmt":"2009-09-13T08:18:41","guid":{"rendered":"http:\/\/g33kinfo.com\/info\/index.php\/?p=376"},"modified":"2009-09-13T08:18:41","modified_gmt":"2009-09-13T08:18:41","slug":"blocking-ssh-probing-ips-using-a-simple-script","status":"publish","type":"post","link":"https:\/\/g33kinfo.com\/info\/blocking-ssh-probing-ips-using-a-simple-script\/","title":{"rendered":"Blocking SSH probing IPs using a simple script"},"content":{"rendered":"<p>(from tuxopia.net)<\/p>\n<p>I was troubled by the fact that my server was constantly being bombarded with SSH probes for as long as 1 to 2 hours at a stretch. Although the SSH daemon configuration has been tuned for security like disabling root access, allowing only a certain list of users, etc., a continuous SSH probe is still disturbing. I could block all other IPs for SSH access except for the ones that I use, but who knows where I will be when I need to SSH to my machine? I could be on any where. So the solution, I found was to just block SSH access to IPs that were violating my server&#8217;s and my peace. Since, I know that many of you out there have seen such attacks in your logs, I will share the script with you and a short explanation of how it works.<br \/>\nThe Logic<\/p>\n<p>Whenever an invalid ssh login appears you usually get a log message like<\/p>\n<p>May  3 13:21:44 bell sshd[11909]: Failed password for invalid user root from 90.228.208.201 port 59948 ssh2<\/p>\n<p>So we need to grep the lines from the log that have &#8220;Failed Password&#8221; in them. This can be done by<\/p>\n<p>grep &#8220;Failed Password&#8221; \/var\/log\/messages <\/p>\n<p>Now that we have the lines we can grab the IP. Note that the IP is the 4th item from the end of line. To grab it we will pipe the output of previous command to awk.<\/p>\n<p>grep &#8220;Failed Password&#8221; \/var\/log\/messages |awk &#8216;{a=NF-3; print $a}&#8217;<\/p>\n<p>Unfortunately this gives us duplicate IPs as well. we need to count how many times each IP has failed, since even our own IP may be in the list. Chances are legitimate users could be bloked too. So we need a count. we can do this using uniq command. Also for sake of redability lets sort it numerically and remove the spaces at the beginning of the line we get with output of uniq.<\/p>\n<p>grep &#8216;Failed password&#8217; \/var\/log\/messages | awk &#8216;{ a=NF-3; print $a}&#8217; |uniq -c|  sed &#8216;s\/^ *\/\/&#8217; | sort -n<\/p>\n<p>Once we have got this it is only a matter of checking if each IP which is above the bad countlevel exists in \/etc\/hosts.deny and add the ones that are not.<br \/>\nThe Script<\/p>\n<p>Compiling all this together we have our bash script:<\/p>\n<p>#!\/bin\/bash<br \/>\nAWK=\/bin\/awk<br \/>\nSORT=\/bin\/sort<br \/>\nGREP=\/bin\/grep<br \/>\nUNIQ=\/bin\/uniq<br \/>\nSED=\/bin\/sed<br \/>\nLOGFILE=\/var\/log\/messages<br \/>\nBADCOUNT=&#8221;5&#8243;<\/p>\n<p>$GREP &#8216;Failed password&#8217; $LOGFILE | $AWK &#8216;{ a=NF-3; print $a}&#8217; | $UNIQ -c| $SED &#8216;s\/^ *\/\/&#8217;| $SORT -n  | while read i<br \/>\ndo<br \/>\n        COUNT=`echo $i | cut -d &#8221; &#8221; -f1`<br \/>\n        IP=`echo $i | cut -d &#8221; &#8221; -f2`<br \/>\n        INLIST=`$GREP $IP \/etc\/hosts.deny | $GREP sshd`<br \/>\n        if [ -z &#8220;$INLIST&#8221; ]<br \/>\n        then<br \/>\n                if [ &#8220;$COUNT&#8221; -ge &#8220;$BADCOUNT&#8221; ]<br \/>\n                then<br \/>\n                        echo &#8220;sshd: $IP&#8221; >> \/etc\/hosts.deny<br \/>\n                fi<br \/>\n        fi<br \/>\ndone<\/p>\n<p>Since, I will be away for a month, I decided to add this to cron as well running every minute every hour, every day. So the cron entry reads<\/p>\n<p>* * * * * \/root\/bin\/ssh-block.sh 1> \/dev\/null<\/p>\n<p>Note that although this will block the IP from making yet another SSH probe, the machine will be able to make probes for about a minute before it is blocked. So you need to be sure to strengthen your SSHD configuration.<\/p>\n<p>Hope this helps you guys.<\/p>\n<p>NOTE: The script is a modified version from a script I found on Novel&#8217;s site http:\/\/www.novell.com\/coolsolutions\/tools\/19769.html My editted version removes one of the vulnerabilty of the Novel script that would allow an attacker to log inject an IP. So the credit for the original script goes to the original author.<br \/>\nBookmark\/Search this post with:<\/p>\n","protected":false},"excerpt":{"rendered":"<p>(from tuxopia.net) I was troubled by the fact that my server was constantly being bombarded with SSH probes for as long as 1 to 2 hours at a stretch. Although the SSH daemon configuration has been tuned for security like disabling root access, allowing only a certain list of users, etc., a continuous SSH probe&#8230; <\/p>\n<div class=\"read-more navbutton\"><a href=\"https:\/\/g33kinfo.com\/info\/blocking-ssh-probing-ips-using-a-simple-script\/\">Read More<i class=\"fa fa-angle-double-right\"><\/i><\/a><\/div>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[9],"tags":[],"class_list":["post-376","post","type-post","status-publish","format-standard","hentry","category-info"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.8 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Blocking SSH probing IPs using a simple script - Linux Shtuff<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/g33kinfo.com\/info\/blocking-ssh-probing-ips-using-a-simple-script\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Blocking SSH probing IPs using a simple script - Linux Shtuff\" \/>\n<meta property=\"og:description\" content=\"(from tuxopia.net) I was troubled by the fact that my server was constantly being bombarded with SSH probes for as long as 1 to 2 hours at a stretch. Although the SSH daemon configuration has been tuned for security like disabling root access, allowing only a certain list of users, etc., a continuous SSH probe... Read More\" \/>\n<meta property=\"og:url\" content=\"https:\/\/g33kinfo.com\/info\/blocking-ssh-probing-ips-using-a-simple-script\/\" \/>\n<meta property=\"og:site_name\" content=\"Linux Shtuff\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/fb.me\/g33kinf0\" \/>\n<meta property=\"article:author\" content=\"https:\/\/fb.me\/g33kinf0\" \/>\n<meta property=\"article:published_time\" content=\"2009-09-13T08:18:41+00:00\" \/>\n<meta name=\"author\" content=\"g33kadmin\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@https:\/\/twitter.com\/drsinger1111\" \/>\n<meta name=\"twitter:site\" content=\"@drsinger1111\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/g33kinfo.com\\\/info\\\/blocking-ssh-probing-ips-using-a-simple-script\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/g33kinfo.com\\\/info\\\/blocking-ssh-probing-ips-using-a-simple-script\\\/\"},\"author\":{\"name\":\"g33kadmin\",\"@id\":\"https:\\\/\\\/g33kinfo.com\\\/info\\\/#\\\/schema\\\/person\\\/c022e4c40b13ea1b678e6f020756f547\"},\"headline\":\"Blocking SSH probing IPs using a simple script\",\"datePublished\":\"2009-09-13T08:18:41+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/g33kinfo.com\\\/info\\\/blocking-ssh-probing-ips-using-a-simple-script\\\/\"},\"wordCount\":614,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\\\/\\\/g33kinfo.com\\\/info\\\/#\\\/schema\\\/person\\\/c022e4c40b13ea1b678e6f020756f547\"},\"articleSection\":[\"General Info\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/g33kinfo.com\\\/info\\\/blocking-ssh-probing-ips-using-a-simple-script\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/g33kinfo.com\\\/info\\\/blocking-ssh-probing-ips-using-a-simple-script\\\/\",\"url\":\"https:\\\/\\\/g33kinfo.com\\\/info\\\/blocking-ssh-probing-ips-using-a-simple-script\\\/\",\"name\":\"Blocking SSH probing IPs using a simple script - Linux Shtuff\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/g33kinfo.com\\\/info\\\/#website\"},\"datePublished\":\"2009-09-13T08:18:41+00:00\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/g33kinfo.com\\\/info\\\/blocking-ssh-probing-ips-using-a-simple-script\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/g33kinfo.com\\\/info\\\/blocking-ssh-probing-ips-using-a-simple-script\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/g33kinfo.com\\\/info\\\/blocking-ssh-probing-ips-using-a-simple-script\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/g33kinfo.com\\\/info\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Blocking SSH probing IPs using a simple script\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/g33kinfo.com\\\/info\\\/#website\",\"url\":\"https:\\\/\\\/g33kinfo.com\\\/info\\\/\",\"name\":\"Linux Shtuff\",\"description\":\"Because I have CRS Syndrome...\",\"publisher\":{\"@id\":\"https:\\\/\\\/g33kinfo.com\\\/info\\\/#\\\/schema\\\/person\\\/c022e4c40b13ea1b678e6f020756f547\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/g33kinfo.com\\\/info\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":[\"Person\",\"Organization\"],\"@id\":\"https:\\\/\\\/g33kinfo.com\\\/info\\\/#\\\/schema\\\/person\\\/c022e4c40b13ea1b678e6f020756f547\",\"name\":\"g33kadmin\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/g33kinfo.com\\\/info\\\/wp-content\\\/uploads\\\/2022\\\/07\\\/minion-researchA.gif\",\"url\":\"https:\\\/\\\/g33kinfo.com\\\/info\\\/wp-content\\\/uploads\\\/2022\\\/07\\\/minion-researchA.gif\",\"contentUrl\":\"https:\\\/\\\/g33kinfo.com\\\/info\\\/wp-content\\\/uploads\\\/2022\\\/07\\\/minion-researchA.gif\",\"width\":512,\"height\":512,\"caption\":\"g33kadmin\"},\"logo\":{\"@id\":\"https:\\\/\\\/g33kinfo.com\\\/info\\\/wp-content\\\/uploads\\\/2022\\\/07\\\/minion-researchA.gif\"},\"description\":\"I am a g33k, Linux blogger, developer, student and Tech Writer for Liquidweb.com\\\/kb. My passion for all things tech drives my hunt for all the coolz. I often need a vacation after I get back from vacation....\",\"sameAs\":[\"https:\\\/\\\/thelinuxreport.com\",\"https:\\\/\\\/fb.me\\\/g33kinf0\",\"https:\\\/\\\/x.com\\\/https:\\\/\\\/twitter.com\\\/drsinger1111\"]}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Blocking SSH probing IPs using a simple script - Linux Shtuff","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/g33kinfo.com\/info\/blocking-ssh-probing-ips-using-a-simple-script\/","og_locale":"en_US","og_type":"article","og_title":"Blocking SSH probing IPs using a simple script - Linux Shtuff","og_description":"(from tuxopia.net) I was troubled by the fact that my server was constantly being bombarded with SSH probes for as long as 1 to 2 hours at a stretch. Although the SSH daemon configuration has been tuned for security like disabling root access, allowing only a certain list of users, etc., a continuous SSH probe... Read More","og_url":"https:\/\/g33kinfo.com\/info\/blocking-ssh-probing-ips-using-a-simple-script\/","og_site_name":"Linux Shtuff","article_publisher":"https:\/\/fb.me\/g33kinf0","article_author":"https:\/\/fb.me\/g33kinf0","article_published_time":"2009-09-13T08:18:41+00:00","author":"g33kadmin","twitter_card":"summary_large_image","twitter_creator":"@https:\/\/twitter.com\/drsinger1111","twitter_site":"@drsinger1111","schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/g33kinfo.com\/info\/blocking-ssh-probing-ips-using-a-simple-script\/#article","isPartOf":{"@id":"https:\/\/g33kinfo.com\/info\/blocking-ssh-probing-ips-using-a-simple-script\/"},"author":{"name":"g33kadmin","@id":"https:\/\/g33kinfo.com\/info\/#\/schema\/person\/c022e4c40b13ea1b678e6f020756f547"},"headline":"Blocking SSH probing IPs using a simple script","datePublished":"2009-09-13T08:18:41+00:00","mainEntityOfPage":{"@id":"https:\/\/g33kinfo.com\/info\/blocking-ssh-probing-ips-using-a-simple-script\/"},"wordCount":614,"commentCount":0,"publisher":{"@id":"https:\/\/g33kinfo.com\/info\/#\/schema\/person\/c022e4c40b13ea1b678e6f020756f547"},"articleSection":["General Info"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/g33kinfo.com\/info\/blocking-ssh-probing-ips-using-a-simple-script\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/g33kinfo.com\/info\/blocking-ssh-probing-ips-using-a-simple-script\/","url":"https:\/\/g33kinfo.com\/info\/blocking-ssh-probing-ips-using-a-simple-script\/","name":"Blocking SSH probing IPs using a simple script - Linux Shtuff","isPartOf":{"@id":"https:\/\/g33kinfo.com\/info\/#website"},"datePublished":"2009-09-13T08:18:41+00:00","breadcrumb":{"@id":"https:\/\/g33kinfo.com\/info\/blocking-ssh-probing-ips-using-a-simple-script\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/g33kinfo.com\/info\/blocking-ssh-probing-ips-using-a-simple-script\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/g33kinfo.com\/info\/blocking-ssh-probing-ips-using-a-simple-script\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/g33kinfo.com\/info\/"},{"@type":"ListItem","position":2,"name":"Blocking SSH probing IPs using a simple script"}]},{"@type":"WebSite","@id":"https:\/\/g33kinfo.com\/info\/#website","url":"https:\/\/g33kinfo.com\/info\/","name":"Linux Shtuff","description":"Because I have CRS Syndrome...","publisher":{"@id":"https:\/\/g33kinfo.com\/info\/#\/schema\/person\/c022e4c40b13ea1b678e6f020756f547"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/g33kinfo.com\/info\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":["Person","Organization"],"@id":"https:\/\/g33kinfo.com\/info\/#\/schema\/person\/c022e4c40b13ea1b678e6f020756f547","name":"g33kadmin","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/g33kinfo.com\/info\/wp-content\/uploads\/2022\/07\/minion-researchA.gif","url":"https:\/\/g33kinfo.com\/info\/wp-content\/uploads\/2022\/07\/minion-researchA.gif","contentUrl":"https:\/\/g33kinfo.com\/info\/wp-content\/uploads\/2022\/07\/minion-researchA.gif","width":512,"height":512,"caption":"g33kadmin"},"logo":{"@id":"https:\/\/g33kinfo.com\/info\/wp-content\/uploads\/2022\/07\/minion-researchA.gif"},"description":"I am a g33k, Linux blogger, developer, student and Tech Writer for Liquidweb.com\/kb. My passion for all things tech drives my hunt for all the coolz. I often need a vacation after I get back from vacation....","sameAs":["https:\/\/thelinuxreport.com","https:\/\/fb.me\/g33kinf0","https:\/\/x.com\/https:\/\/twitter.com\/drsinger1111"]}]}},"_links":{"self":[{"href":"https:\/\/g33kinfo.com\/info\/wp-json\/wp\/v2\/posts\/376","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/g33kinfo.com\/info\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/g33kinfo.com\/info\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/g33kinfo.com\/info\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/g33kinfo.com\/info\/wp-json\/wp\/v2\/comments?post=376"}],"version-history":[{"count":0,"href":"https:\/\/g33kinfo.com\/info\/wp-json\/wp\/v2\/posts\/376\/revisions"}],"wp:attachment":[{"href":"https:\/\/g33kinfo.com\/info\/wp-json\/wp\/v2\/media?parent=376"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/g33kinfo.com\/info\/wp-json\/wp\/v2\/categories?post=376"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/g33kinfo.com\/info\/wp-json\/wp\/v2\/tags?post=376"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}