{"id":5207,"date":"2015-10-02T13:52:44","date_gmt":"2015-10-02T05:52:44","guid":{"rendered":"http:\/\/rmohan.com\/?p=5207"},"modified":"2015-10-02T13:52:44","modified_gmt":"2015-10-02T05:52:44","slug":"linux-filesystem-security-scans","status":"publish","type":"post","link":"https:\/\/mohan.sg\/?p=5207","title":{"rendered":"Linux filesystem security scans"},"content":{"rendered":"<p>Either they are intentional or not, system misconfiguration can lead to very big problems. For example :<\/p>\n<ul>\n<li>Corruption or deletion of important system files<\/li>\n<li>Integrity failure (ex. eavesdropping on private data)<\/li>\n<li>Privilege escalation<\/li>\n<li>Backdoor\/rootkit installation<\/li>\n<\/ul>\n<p>To avoid these problems you should regularly scan your system for known &#8220;misconfiguration patterns&#8221;. This article will explain how to do so using<span class=\"Apple-converted-space\">\u00a0<\/span><i>bash<\/i><span class=\"Apple-converted-space\">\u00a0<\/span>on a GNU Linux system.<span class=\"Apple-converted-space\">\u00a0<\/span><\/p>\n<h2>I. General system scan<\/h2>\n<p>In this section I will describe commands that can be used to have a general overview of your system\u2019s security potential risks.<\/p>\n<p><b>I.1 Setuid et Setgid files<span class=\"Apple-converted-space\">\u00a0<\/span><\/b><br \/>\nFiles with the setuid bit set are not necessarily evil, and setgid folders are really useful and can improve your system\u2019s security. However an awfully huge lot of exploits takes advantage of vulnerabilities in setuid files because it is an easy way to escalate privileges (when exploiting a setuid bit file belonging to root). You should always have a look at those files and ensure that each one really needs to have the setuid bits on and are known to be secure and stable.<br \/>\nList all setuid files :<br \/>\n<code class=\"spip_code\" dir=\"ltr\">find \/ \u00a0 -perm -4000 2&gt;\/dev\/null<\/code><br \/>\nList all setuid files owned by root :<br \/>\n<code class=\"spip_code\" dir=\"ltr\">find \/ \u00a0 -perm -4000 -user root 2&gt;\/dev\/null<\/code><br \/>\nList all setgid files :<br \/>\n<code class=\"spip_code\" dir=\"ltr\">find \/ \u00a0 -perm -4000 2&gt;\/dev\/null<\/code><br \/>\nList all setgid files owned by root :<br \/>\n<code class=\"spip_code\" dir=\"ltr\">find \/ \u00a0 -perm -4000 -user root 2&gt;\/dev\/null<\/code><\/p>\n<p><b>I.2 World writable files<span class=\"Apple-converted-space\">\u00a0<\/span><\/b><br \/>\nThe other big danger on filesystems is bad rights management (it can be admin mistakes). A world writable file owned by root can lead to easy privileges escalation or system corruption.<br \/>\nList all world writable files:<br \/>\n<code class=\"spip_code\" dir=\"ltr\">find \u00a0 \/ \u00a0! -type l -perm -002 \u00a02&gt;\/dev\/null<\/code><\/p>\n<div class=\"note\"><dfn>Note:<span class=\"Apple-converted-space\">\u00a0<\/span><\/dfn>I use the &#8220;! -type l&#8221; options to avoid to list symlinks (that are described as world writable files).<\/div>\n<p><b>I.3 Opened socket connections<\/b><br \/>\nIn Linux system, everything is a file, even sockets. And every admin should have a regular look at the opened connections on a machine.<span class=\"Apple-converted-space\">\u00a0<\/span><br \/>\nThe classical way to list all transport layer connections would be to use netstat :<span class=\"Apple-converted-space\">\u00a0<\/span><br \/>\n<code class=\"spip_code\" dir=\"ltr\">netstat -tupl<\/code><br \/>\nHowever I prefer the<span class=\"Apple-converted-space\">\u00a0<\/span><i>lsof<\/i><span class=\"Apple-converted-space\">\u00a0<\/span>tool that offers way more possibilities and is cabable of describing precisely any opened file on the system (lsof is for &#8220;list opened files&#8221;. Sockets are files, so a nice way to monitor your opened connections would be to use the next command :<span class=\"Apple-converted-space\">\u00a0<\/span><br \/>\n<code class=\"spip_code\" dir=\"ltr\">lsof | grep -E \"IPv4|IPv6|COMMAND.*USER\" | \u00a0sed -r \u00a0\"s\/ +\/ \/g\" | cut -d \" \" -f 1,2,3,5,8,9 | column -t | sed \"s\/.*\/ &amp;\/\"<\/code><br \/>\nThat command will list all IPv4 and IPv6 opened connections and display the corresponding command, pid, user, the connexion type, the transport layer protocol used and the connection description (IP address and port). The<span class=\"Apple-converted-space\">\u00a0<\/span><i>column<\/i><span class=\"Apple-converted-space\">\u00a0<\/span>and<span class=\"Apple-converted-space\">\u00a0<\/span><i>sed<\/i><span class=\"Apple-converted-space\">\u00a0<\/span>part is just for nice formatting ;-).<span class=\"Apple-converted-space\">\u00a0<\/span><br \/>\n<b>I.4 Broken Symbolic links<span class=\"Apple-converted-space\">\u00a0<\/span><\/b><br \/>\nIt can be useful to be able to list the broken symlinks on the system (example, for a cleaning task).<br \/>\nExample, list all broken symlinks in \/usr directory :<br \/>\n<code class=\"spip_code\" dir=\"ltr\">find -L \u00a0\/usr -type l -maxdepth 8 2&gt;\/dev\/null<span class=\"Apple-converted-space\">\u00a0<\/span><\/code><\/p>\n<p><b>I.5 Sticky bit files<span class=\"Apple-converted-space\">\u00a0<\/span><\/b><br \/>\nThe sticky bit is important, it should always be set on world writable folders to prevent a user from removing a file he doesn\u2019t own.<br \/>\nList all sticky bit files on the system :<span class=\"Apple-converted-space\">\u00a0<\/span><br \/>\n<code class=\"spip_code\" dir=\"ltr\">find \/ \u00a0-perm -1000 2&gt;\/dev\/null<\/code><span class=\"Apple-converted-space\">\u00a0<\/span><\/p>\n<h2>II. Potential threats scan<\/h2>\n<p>After the general scan, you can do more detailed scan to find abnormal configurations or real dangers.<\/p>\n<p><b>II.1 File access rights risks<\/b><br \/>\nSome files should never be world writable or even world readable. For example the directories \/bin, \/sbin, \/boot, \/etc, \/lib, \/root, \/usr should never be world writable. A world writable file in these directories could lead to system trojaning\/corruption.<br \/>\nHere is a simple way to scan all these folders for world writable files.<\/p>\n<div class=\"coloration_code\">\n<div class=\"spip_bash cadre\">\n<div class=\"bash\">\n<ol>\n<li>\n<div>#\/bin\/bash<\/div>\n<\/li>\n<li>\n<div>ww_scan_dirs=&#8221;\/bin \/sbin \/boot \/etc \/lib \/root \/usr &#8220;<\/div>\n<\/li>\n<li>\n<div>for<span class=\"Apple-converted-space\">\u00a0<\/span>ww_scan_dir<span class=\"Apple-converted-space\">\u00a0<\/span>in<span class=\"Apple-converted-space\">\u00a0<\/span>$ww_scan_dirs<\/div>\n<\/li>\n<li>\n<div>do<\/div>\n<\/li>\n<li>\n<div>\u00a0 \u00a0 \u00a0 \u00a0<span class=\"Apple-converted-space\">\u00a0<\/span>for<span class=\"Apple-converted-space\">\u00a0<\/span>file<span class=\"Apple-converted-space\">\u00a0<\/span>in<span class=\"Apple-converted-space\">\u00a0<\/span>`<span class=\"Apple-converted-space\">\u00a0<\/span>find<span class=\"Apple-converted-space\">\u00a0<\/span>$ww_scan_dir<span class=\"Apple-converted-space\">\u00a0<\/span>\u00a0!<span class=\"Apple-converted-space\">\u00a0<\/span>-type<span class=\"Apple-converted-space\">\u00a0<\/span>l \u00a0-perm<span class=\"Apple-converted-space\">\u00a0<\/span>-002<span class=\"Apple-converted-space\">\u00a0<\/span>`<\/div>\n<\/li>\n<li>\n<div>\u00a0 \u00a0 \u00a0 \u00a0<span class=\"Apple-converted-space\">\u00a0<\/span>do<\/div>\n<\/li>\n<li>\n<div>\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0<span class=\"Apple-converted-space\">\u00a0<\/span>echo<span class=\"Apple-converted-space\">\u00a0<\/span>&#8220;DANGER :<span class=\"Apple-converted-space\">\u00a0<\/span>$file<span class=\"Apple-converted-space\">\u00a0<\/span>is world writable, files in<span class=\"Apple-converted-space\">\u00a0<\/span>$ww_scan_dir<span class=\"Apple-converted-space\">\u00a0<\/span>shouldn&#8217;t be.&#8221;<\/div>\n<\/li>\n<li>\n<div>\u00a0 \u00a0 \u00a0 \u00a0<span class=\"Apple-converted-space\">\u00a0<\/span>done<span class=\"Apple-converted-space\">\u00a0<\/span>|<span class=\"Apple-converted-space\">\u00a0<\/span>sort<\/div>\n<\/li>\n<li>\n<div>done<\/div>\n<\/li>\n<li>\n<div>unset<span class=\"Apple-converted-space\">\u00a0<\/span>ww_scan_dir<\/div>\n<\/li>\n<li>\n<div>unset<span class=\"Apple-converted-space\">\u00a0<\/span>ww_scan_dirs<\/div>\n<\/li>\n<\/ol>\n<\/div>\n<\/div>\n<\/div>\n<p>Another concern about world writable directories is that, when allowed (ex insind \/var or \/tmp) they must have the sticky bit on to prevent unauthorized file deletion.<\/p>\n<div class=\"coloration_code\">\n<div class=\"spip_bash cadre\">\n<div class=\"bash\">\n<ol>\n<li>\n<div>for<span class=\"Apple-converted-space\">\u00a0<\/span>file<span class=\"Apple-converted-space\">\u00a0<\/span>in<span class=\"Apple-converted-space\">\u00a0<\/span>`find<span class=\"Apple-converted-space\">\u00a0<\/span>\/<span class=\"Apple-converted-space\">\u00a0<\/span>\u00a0<span class=\"Apple-converted-space\">\u00a0<\/span>-type<span class=\"Apple-converted-space\">\u00a0<\/span>d<span class=\"Apple-converted-space\">\u00a0<\/span>-perm<span class=\"Apple-converted-space\">\u00a0<\/span>-002<span class=\"Apple-converted-space\">\u00a0<\/span>!<span class=\"Apple-converted-space\">\u00a0<\/span>-perm<span class=\"Apple-converted-space\">\u00a0<\/span>-1000<span class=\"Apple-converted-space\">\u00a0<\/span>\u00a0<span class=\"Apple-converted-space\">\u00a0<\/span>2&gt;\/dev\/null`<\/div>\n<\/li>\n<li>\n<div>do<\/div>\n<\/li>\n<li>\n<div>\u00a0 \u00a0 \u00a0 \u00a0<span class=\"Apple-converted-space\">\u00a0<\/span>echo&#8221;DANGER :<span class=\"Apple-converted-space\">\u00a0<\/span>$file<span class=\"Apple-converted-space\">\u00a0<\/span>is a world writable directory, it should have the sticky bit on.&#8221;<\/div>\n<\/li>\n<li>\n<div>done<span class=\"Apple-converted-space\">\u00a0<\/span>|<span class=\"Apple-converted-space\">\u00a0<\/span>sort<\/div>\n<\/li>\n<\/ol>\n<\/div>\n<\/div>\n<\/div>\n<p><b>II.2 file ownership risks<\/b><br \/>\nAre you sure that all system files are really owned by root? You can imagine the potential disaster for your system if not.<br \/>\nHere is a way to verify that.<\/p>\n<div class=\"coloration_code\">\n<div class=\"spip_bash cadre\">\n<div class=\"bash\">\n<ol>\n<li>\n<div>for<span class=\"Apple-converted-space\">\u00a0<\/span>file<span class=\"Apple-converted-space\">\u00a0<\/span>in<span class=\"Apple-converted-space\">\u00a0<\/span>`find<span class=\"Apple-converted-space\">\u00a0<\/span>\/root<span class=\"Apple-converted-space\">\u00a0<\/span>$find_options<span class=\"Apple-converted-space\">\u00a0<\/span>!<span class=\"Apple-converted-space\">\u00a0<\/span>-user<span class=\"Apple-converted-space\">\u00a0<\/span>root \u00a02&gt;\/dev\/null`<\/div>\n<\/li>\n<li>\n<div>do<\/div>\n<\/li>\n<li>\n<div>\u00a0 \u00a0 \u00a0 \u00a0<span class=\"Apple-converted-space\">\u00a0<\/span>echo&#8221;DANGER :<span class=\"Apple-converted-space\">\u00a0<\/span>$file<span class=\"Apple-converted-space\">\u00a0<\/span>doesn&#8217;t belong to root. It mustn&#8217;t be in the \/root folder.&#8221;<\/div>\n<\/li>\n<li>\n<div>done<span class=\"Apple-converted-space\">\u00a0<\/span>|<span class=\"Apple-converted-space\">\u00a0<\/span>sort<\/div>\n<\/li>\n<\/ol>\n<\/div>\n<\/div>\n<\/div>\n<p>It is also interesting to verify if all files belong to an existing user and group.<span class=\"Apple-converted-space\">\u00a0<\/span><\/p>\n<div class=\"coloration_code\">\n<div class=\"spip_bash cadre\">\n<div class=\"bash\">\n<ol>\n<li>\n<div>#!\/bin\/bash<\/div>\n<\/li>\n<li>\n<div>for<span class=\"Apple-converted-space\">\u00a0<\/span>file<span class=\"Apple-converted-space\">\u00a0<\/span>in<span class=\"Apple-converted-space\">\u00a0<\/span>`find<span class=\"Apple-converted-space\">\u00a0<\/span>\/<span class=\"Apple-converted-space\">\u00a0<\/span>$find_options<span class=\"Apple-converted-space\">\u00a0<\/span>-nouser<span class=\"Apple-converted-space\">\u00a0<\/span>\u00a02&gt;\/dev\/null`<\/div>\n<\/li>\n<li>\n<div>do<\/div>\n<\/li>\n<li>\n<div>\u00a0 \u00a0 \u00a0 \u00a0<span class=\"Apple-converted-space\">\u00a0<\/span>echo&#8221;DANGER : No user corresponds to<span class=\"Apple-converted-space\">\u00a0<\/span>$file<span class=\"Apple-converted-space\">\u00a0<\/span>\u00a0numeric user ID.&#8221;<\/div>\n<\/li>\n<li>\n<div>done<span class=\"Apple-converted-space\">\u00a0<\/span>|<span class=\"Apple-converted-space\">\u00a0<\/span>sort<\/div>\n<\/li>\n<li>\n<div>for<span class=\"Apple-converted-space\">\u00a0<\/span>file<span class=\"Apple-converted-space\">\u00a0<\/span>in<span class=\"Apple-converted-space\">\u00a0<\/span>`find<span class=\"Apple-converted-space\">\u00a0<\/span>\/<span class=\"Apple-converted-space\">\u00a0<\/span>$find_options<span class=\"Apple-converted-space\">\u00a0<\/span>\u00a0-nogroup<span class=\"Apple-converted-space\">\u00a0<\/span>\u00a02&gt;\/dev\/null`<\/div>\n<\/li>\n<li>\n<div>do<\/div>\n<\/li>\n<li>\n<div>\u00a0 \u00a0 \u00a0 \u00a0<span class=\"Apple-converted-space\">\u00a0<\/span>echo<span class=\"Apple-converted-space\">\u00a0<\/span>&#8220;DANGER : No group corresponds to<span class=\"Apple-converted-space\">\u00a0<\/span>$file<span class=\"Apple-converted-space\">\u00a0<\/span>\u00a0numeric group ID.&#8221;<\/div>\n<\/li>\n<li>\n<div>done<span class=\"Apple-converted-space\">\u00a0<\/span>|<span class=\"Apple-converted-space\">\u00a0<\/span>sort<\/div>\n<\/li>\n<\/ol>\n<\/div>\n<\/div>\n<\/div>\n<p><span class=\"Apple-converted-space\">\u00a0<\/span><br \/>\n<b>II.3 Special files risks<\/b><br \/>\n&#8220;Special&#8221; files like devices, socket and symlinks should be considered carefully. For example you should verify that devices are only stored in special directories like \/dev or &#8230;\/udev\/ which can be done using the next script :<\/p>\n<div class=\"coloration_code\">\n<div class=\"spip_bash cadre\">\n<div class=\"bash\">\n<ol>\n<li>\n<div>#!\/bin\/bash<\/div>\n<\/li>\n<li>\n<div>device_scan_dirs=&#8221;\/bin \/sbin \/lib \/boot \/etc \/home \/root \/sys \/usr \/var \/tmp \/mnt \/media \/proc&#8221;<\/div>\n<\/li>\n<li>\n<div>for<span class=\"Apple-converted-space\">\u00a0<\/span>device_scan_dir<span class=\"Apple-converted-space\">\u00a0<\/span>in<span class=\"Apple-converted-space\">\u00a0<\/span>$device_scan_dirs<\/div>\n<\/li>\n<li>\n<div>do<\/div>\n<\/li>\n<li>\n<div>\u00a0 \u00a0 \u00a0 \u00a0<span class=\"Apple-converted-space\">\u00a0<\/span>for<span class=\"Apple-converted-space\">\u00a0<\/span>file<span class=\"Apple-converted-space\">\u00a0<\/span>in<span class=\"Apple-converted-space\">\u00a0<\/span>`find<span class=\"Apple-converted-space\">\u00a0<\/span>$device_scan_dir<span class=\"Apple-converted-space\">\u00a0<\/span>$find_options<span class=\"Apple-converted-space\">\u00a0<\/span>-type<span class=\"Apple-converted-space\">\u00a0<\/span>b<span class=\"Apple-converted-space\">\u00a0<\/span>-o<span class=\"Apple-converted-space\">\u00a0<\/span>-type<span class=\"Apple-converted-space\">\u00a0<\/span>c \u00a02&gt;\/dev\/null`<\/div>\n<\/li>\n<li>\n<div>\u00a0 \u00a0 \u00a0 \u00a0<span class=\"Apple-converted-space\">\u00a0<\/span>do<\/div>\n<\/li>\n<li>\n<div>\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0<span class=\"Apple-converted-space\">\u00a0<\/span>[[<span class=\"Apple-converted-space\">\u00a0<\/span>&#8220;$file&#8221;<span class=\"Apple-converted-space\">\u00a0<\/span>=~ ^\/lib\/udev\/devices\/<span class=\"Apple-converted-space\">\u00a0<\/span>]]<span class=\"Apple-converted-space\">\u00a0<\/span>||<span class=\"Apple-converted-space\">\u00a0<\/span>echo<span class=\"Apple-converted-space\">\u00a0<\/span>&#8220;DANGER :<span class=\"Apple-converted-space\">\u00a0<\/span>$file<span class=\"Apple-converted-space\">\u00a0<\/span>is a device and should be in \/dev (or \/lib\/udev\/devices).&#8221;<\/div>\n<\/li>\n<li>\n<div>\u00a0 \u00a0 \u00a0 \u00a0<span class=\"Apple-converted-space\">\u00a0<\/span>done<span class=\"Apple-converted-space\">\u00a0<\/span>|<span class=\"Apple-converted-space\">\u00a0<\/span>sort<\/div>\n<\/li>\n<li>\n<div>done<\/div>\n<\/li>\n<li>\n<div>unset<span class=\"Apple-converted-space\">\u00a0<\/span>device_scan_dir<\/div>\n<\/li>\n<li>\n<div>unset<span class=\"Apple-converted-space\">\u00a0<\/span>device_scan_dirs<\/div>\n<\/li>\n<\/ol>\n<\/div>\n<\/div>\n<\/div>\n<p>Symlinks should also be checked carefully as they are often exploited to gain root privileges. For example, the presence of a symlink inside the \/tmp folder is not dangerous in itself but if that symlink was intentionally created by an attacker to imitate the name of tmp files generated by an insecure program, this symlink could lead to the corruption of a system file or to privilege escalation. If you want to check your \/tmp directory for symlinks :<\/p>\n<div class=\"coloration_code\">\n<div class=\"spip_bash cadre\">\n<div class=\"bash\">\n<ol>\n<li>\n<div>#!\/bin\/bash<\/div>\n<\/li>\n<li>\n<div>for<span class=\"Apple-converted-space\">\u00a0<\/span>file<span class=\"Apple-converted-space\">\u00a0<\/span>in<span class=\"Apple-converted-space\">\u00a0<\/span>`find<span class=\"Apple-converted-space\">\u00a0<\/span>\u00a0\/tmp<span class=\"Apple-converted-space\">\u00a0<\/span>$find_options<span class=\"Apple-converted-space\">\u00a0<\/span>\u00a0-type<span class=\"Apple-converted-space\">\u00a0<\/span>l \u00a0`<\/div>\n<\/li>\n<li>\n<div>\u00a0 \u00a0 \u00a0 \u00a0<span class=\"Apple-converted-space\">\u00a0<\/span>do<\/div>\n<\/li>\n<li>\n<div>\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0<span class=\"Apple-converted-space\">\u00a0<\/span>echo&#8221;RISK :<span class=\"Apple-converted-space\">\u00a0<\/span>$file<span class=\"Apple-converted-space\">\u00a0<\/span>is a symbolic link inside the \/tmp folder&#8221;<\/div>\n<\/li>\n<li>\n<div>\u00a0 \u00a0 \u00a0 \u00a0<span class=\"Apple-converted-space\">\u00a0<\/span>done<span class=\"Apple-converted-space\">\u00a0<\/span>|<span class=\"Apple-converted-space\">\u00a0<\/span>sort<\/div>\n<\/li>\n<\/ol>\n<\/div>\n<\/div>\n<\/div>\n<p><span class=\"Apple-converted-space\">\u00a0<\/span><br \/>\n<b>II.4 Extreme dangers detection<\/b><br \/>\nThere are a few configurations (intentionally or not) that are sure to lead to system &#8220;0wn3rship&#8221;. If you detect one of these patterns on you computer, you should repair it right away (however it might already be too late!).<br \/>\nCheck if there are any files with SetUID bit on in the \/tmp folder :<span class=\"Apple-converted-space\">\u00a0<\/span><\/p>\n<div class=\"coloration_code\">\n<div class=\"spip_bash cadre\">\n<div class=\"bash\">\n<ol>\n<li>\n<div>for<span class=\"Apple-converted-space\">\u00a0<\/span>file<span class=\"Apple-converted-space\">\u00a0<\/span>in<span class=\"Apple-converted-space\">\u00a0<\/span>`find<span class=\"Apple-converted-space\">\u00a0<\/span>\u00a0<span class=\"Apple-converted-space\">\u00a0<\/span>\/tmp<span class=\"Apple-converted-space\">\u00a0<\/span>$find_options<span class=\"Apple-converted-space\">\u00a0<\/span>\u00a0-perm<span class=\"Apple-converted-space\">\u00a0<\/span>-4000<span class=\"Apple-converted-space\">\u00a0<\/span>\u00a0`<\/div>\n<\/li>\n<li>\n<div>do<\/div>\n<\/li>\n<li>\n<div>\u00a0 \u00a0 \u00a0 \u00a0<span class=\"Apple-converted-space\">\u00a0<\/span>echo<span class=\"Apple-converted-space\">\u00a0<\/span>&#8220;EXTREME DANGER :<span class=\"Apple-converted-space\">\u00a0<\/span>$file<span class=\"Apple-converted-space\">\u00a0<\/span>is setuid and shouldn&#8217;t be in \/tmp folder.&#8221;<\/div>\n<\/li>\n<li>\n<div>done<span class=\"Apple-converted-space\">\u00a0<\/span>|<span class=\"Apple-converted-space\">\u00a0<\/span>sort<\/div>\n<\/li>\n<\/ol>\n<\/div>\n<\/div>\n<\/div>\n<p>Check if there are any files that are world writable and have setuid bit on (That should never happen. However if it does, the admin may live a nightmare&#8230;)<\/p>\n<div class=\"coloration_code\">\n<div class=\"spip_bash cadre\">\n<div class=\"bash\">\n<ol>\n<li>\n<div>for<span class=\"Apple-converted-space\">\u00a0<\/span>file<span class=\"Apple-converted-space\">\u00a0<\/span>in<span class=\"Apple-converted-space\">\u00a0<\/span>`find<span class=\"Apple-converted-space\">\u00a0<\/span>\u00a0<span class=\"Apple-converted-space\">\u00a0<\/span>\/<span class=\"Apple-converted-space\">\u00a0<\/span>$find_options<span class=\"Apple-converted-space\">\u00a0<\/span>-perm<span class=\"Apple-converted-space\">\u00a0<\/span>-4002<span class=\"Apple-converted-space\">\u00a0<\/span>\u00a02&gt;\/dev\/null`<\/div>\n<\/li>\n<li>\n<div>do<\/div>\n<\/li>\n<li>\n<div>\u00a0 \u00a0 \u00a0 \u00a0<span class=\"Apple-converted-space\">\u00a0<\/span>echo<span class=\"Apple-converted-space\">\u00a0<\/span>&#8220;EXTREME DANGER :<span class=\"Apple-converted-space\">\u00a0<\/span>$file<span class=\"Apple-converted-space\">\u00a0<\/span>is setuid and world writable.&#8221;<\/div>\n<\/li>\n<li>\n<div>done<span class=\"Apple-converted-space\">\u00a0<\/span>|<span class=\"Apple-converted-space\">\u00a0<\/span>sort<\/div>\n<\/li>\n<\/ol>\n<\/div>\n<\/div>\n<\/div>\n<p>Some files should never be readable by anyone but root :<span class=\"Apple-converted-space\">\u00a0<\/span><\/p>\n<div class=\"coloration_code\">\n<div class=\"spip_bash cadre\">\n<div class=\"bash\">\n<ol>\n<li>\n<div>#!\/bin\/bash<\/div>\n<\/li>\n<li>\n<div>nonReadableFiles=&#8221;\/etc\/master.passwd \/etc\/shadow \/etc\/shadow- \/etc\/gshadow \/etc\/sudoers \/var\/log\/messages &#8220;<\/div>\n<\/li>\n<li>\n<div>for<span class=\"Apple-converted-space\">\u00a0<\/span>nonReadableFile<span class=\"Apple-converted-space\">\u00a0<\/span>in<span class=\"Apple-converted-space\">\u00a0<\/span>$nonReadableFiles<\/div>\n<\/li>\n<li>\n<div>do<\/div>\n<\/li>\n<li>\n<div>\u00a0 \u00a0 \u00a0 \u00a0<span class=\"Apple-converted-space\">\u00a0<\/span>[<span class=\"Apple-converted-space\">\u00a0<\/span>-f<span class=\"Apple-converted-space\">\u00a0<\/span>&#8220;$nonReadableFile&#8221;<span class=\"Apple-converted-space\">\u00a0<\/span>]<span class=\"Apple-converted-space\">\u00a0<\/span>&amp;&amp;<span class=\"Apple-converted-space\">\u00a0<\/span>[[<span class=\"Apple-converted-space\">\u00a0<\/span>$(ls<span class=\"Apple-converted-space\">\u00a0<\/span>-gn<span class=\"Apple-converted-space\">\u00a0<\/span>&#8220;$nonReadableFile&#8221;)<span class=\"Apple-converted-space\">\u00a0<\/span>=~ ^&#8230;&#8230;.r..\\ .*$<span class=\"Apple-converted-space\">\u00a0<\/span>]]<span class=\"Apple-converted-space\">\u00a0<\/span>\u00a0&amp;&amp;<span class=\"Apple-converted-space\">\u00a0<\/span>\u00a0echo&#8221;EXTREME DANGER :<span class=\"Apple-converted-space\">\u00a0<\/span>$nonReadableFile<span class=\"Apple-converted-space\">\u00a0<\/span>should not be readeable by others.&#8221;<\/div>\n<\/li>\n<li>\n<div>done<\/div>\n<\/li>\n<li>\n<div>unset<span class=\"Apple-converted-space\">\u00a0<\/span>nonReadableFile<\/div>\n<\/li>\n<li>\n<div>unset<span class=\"Apple-converted-space\">\u00a0<\/span>nonReadableFiles<\/div>\n<\/li>\n<\/ol>\n<\/div>\n<\/div>\n<\/div>\n<p><span class=\"Apple-converted-space\">\u00a0<\/span><br \/>\n<b>II.5 POSIX file capabilities risks<\/b><br \/>\nPOSIX file capabilities are enabled by default on most modern Linux distribs, however they are not without dangers. You may want to read first<span class=\"Apple-converted-space\">\u00a0<\/span><a href=\"http:\/\/www.friedhoff.org\/posixfilecaps.html\">what are POSIX file capabilities<\/a><span class=\"Apple-converted-space\">\u00a0<\/span>and after that<span class=\"Apple-converted-space\">\u00a0<\/span><a href=\"http:\/\/www.sevagas.com\/?POSIX-file-capabilities-the-dark\">why they are dangerous<\/a>.<span class=\"Apple-converted-space\">\u00a0<\/span><br \/>\nIt is not easy to scan your system for dangerous capabilities because the tool used for that<span class=\"Apple-converted-space\">\u00a0<\/span><i>getcap<\/i><span class=\"Apple-converted-space\">\u00a0<\/span>for example, are quite buggy and poorly documented. Here is an example of a script looking for dangerous file capabilities on the entire system.<\/p>\n<div class=\"coloration_code\">\n<div class=\"spip_bash cadre\">\n<div class=\"bash\">\n<ol>\n<li>\n<div>\u00a0 \u00a0 \u00a0 \u00a0<span class=\"Apple-converted-space\">\u00a0<\/span>#!\/bin\/bash<\/div>\n<\/li>\n<li>\n<div>\u00a0 \u00a0 \u00a0 \u00a0<span class=\"Apple-converted-space\">\u00a0<\/span>totalCaps=$(find<span class=\"Apple-converted-space\">\u00a0<\/span>\/<span class=\"Apple-converted-space\">\u00a0<\/span>-type<span class=\"Apple-converted-space\">\u00a0<\/span>f<span class=\"Apple-converted-space\">\u00a0<\/span>-print0<span class=\"Apple-converted-space\">\u00a0<\/span>2&gt;\/dev\/null \u00a0|<span class=\"Apple-converted-space\">\u00a0<\/span>\u00a0xargs<span class=\"Apple-converted-space\">\u00a0<\/span>-0<span class=\"Apple-converted-space\">\u00a0<\/span>getcap \u00a0<span class=\"Apple-converted-space\">\u00a0<\/span>2&gt;\/dev\/null)<\/div>\n<\/li>\n<li>\n<div>\u00a0 \u00a0 \u00a0 \u00a0<span class=\"Apple-converted-space\">\u00a0<\/span>echo<span class=\"Apple-converted-space\">\u00a0<\/span>&#8220;Number of files with capabilities :<span class=\"Apple-converted-space\">\u00a0<\/span>$(echo &#8220;$totalCaps&#8221; | wc -l)&#8221;<\/div>\n<\/li>\n<li>\n<div>\u00a0 \u00a0 \u00a0 \u00a0<span class=\"Apple-converted-space\">\u00a0<\/span>dangerousCaps=&#8221;cap_chown cap_dac_override cap_fowner cap_module cap_sys_admin cap_setuid cap_setfcap&#8221;<\/div>\n<\/li>\n<li>\n<div>\u00a0 \u00a0 \u00a0 \u00a0<span class=\"Apple-converted-space\">\u00a0<\/span>for<span class=\"Apple-converted-space\">\u00a0<\/span>line<span class=\"Apple-converted-space\">\u00a0<\/span>in<span class=\"Apple-converted-space\">\u00a0<\/span>$totalCaps<\/div>\n<\/li>\n<li>\n<div>\u00a0 \u00a0 \u00a0 \u00a0<span class=\"Apple-converted-space\">\u00a0<\/span>do<\/div>\n<\/li>\n<li>\n<div>\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0<span class=\"Apple-converted-space\">\u00a0<\/span>for<span class=\"Apple-converted-space\">\u00a0<\/span>\u00a0dangerousCap<span class=\"Apple-converted-space\">\u00a0<\/span>in<span class=\"Apple-converted-space\">\u00a0<\/span>\u00a0$dangerousCaps<\/div>\n<\/li>\n<li>\n<div>\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0<span class=\"Apple-converted-space\">\u00a0<\/span>do<\/div>\n<\/li>\n<li>\n<div>\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0<span class=\"Apple-converted-space\">\u00a0<\/span>[[<span class=\"Apple-converted-space\">\u00a0<\/span>&#8220;$line&#8221;<span class=\"Apple-converted-space\">\u00a0<\/span>=~ ^.*=\\ .*$dangerousCap<span class=\"Apple-converted-space\">\u00a0<\/span>]]<span class=\"Apple-converted-space\">\u00a0<\/span>&amp;&amp;<span class=\"Apple-converted-space\">\u00a0<\/span>echo<span class=\"Apple-converted-space\">\u00a0<\/span>&#8220;RISK :<span class=\"Apple-converted-space\">\u00a0<\/span>$(echo &#8220;$line&#8221; | cut -d &#8220;=&#8221; -f 1)has or inherits the<span class=\"Apple-converted-space\">\u00a0<\/span>$dangerousCap<span class=\"Apple-converted-space\">\u00a0<\/span>capability&#8221;<\/div>\n<\/li>\n<li>\n<div>\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0<span class=\"Apple-converted-space\">\u00a0<\/span>done<\/div>\n<\/li>\n<li>\n<div>\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0<span class=\"Apple-converted-space\">\u00a0<\/span>{<span class=\"Apple-converted-space\">\u00a0<\/span>[[<span class=\"Apple-converted-space\">\u00a0<\/span>&#8220;$line&#8221;<span class=\"Apple-converted-space\">\u00a0<\/span>=~ ^.*=ep<span class=\"Apple-converted-space\">\u00a0<\/span>]]<span class=\"Apple-converted-space\">\u00a0<\/span>||<span class=\"Apple-converted-space\">\u00a0<\/span>[[<span class=\"Apple-converted-space\">\u00a0<\/span>&#8220;$line&#8221;<span class=\"Apple-converted-space\">\u00a0<\/span>=~ ^.*=eip<span class=\"Apple-converted-space\">\u00a0<\/span>]]<span class=\"Apple-converted-space\">\u00a0<\/span>||<span class=\"Apple-converted-space\">\u00a0<\/span>[[<span class=\"Apple-converted-space\">\u00a0<\/span>&#8220;$line&#8221;<span class=\"Apple-converted-space\">\u00a0<\/span>=~ ^.*=ei<span class=\"Apple-converted-space\">\u00a0<\/span>]];<span class=\"Apple-converted-space\">\u00a0<\/span>}<span class=\"Apple-converted-space\">\u00a0<\/span>&amp;&amp;<span class=\"Apple-converted-space\">\u00a0<\/span>echo<span class=\"Apple-converted-space\">\u00a0<\/span>&#8220;DANGER :<span class=\"Apple-converted-space\">\u00a0<\/span>$(echo &#8220;$line&#8221; | cut -d &#8220;=&#8221; -f 1)<span class=\"Apple-converted-space\">\u00a0<\/span>has or inherits all capabilities&#8221;<\/div>\n<\/li>\n<li>\n<div>\u00a0 \u00a0 \u00a0 \u00a0<span class=\"Apple-converted-space\">\u00a0<\/span>done<\/div>\n<\/li>\n<li>\n<div>\u00a0 \u00a0 \u00a0 \u00a0<span class=\"Apple-converted-space\">\u00a0<\/span>unset<span class=\"Apple-converted-space\">\u00a0<\/span>line<\/div>\n<\/li>\n<li>\n<div>\u00a0 \u00a0 \u00a0 \u00a0<span class=\"Apple-converted-space\">\u00a0<\/span>unset<span class=\"Apple-converted-space\">\u00a0<\/span>dangerousCaps<\/div>\n<\/li>\n<li>\n<div>\u00a0 \u00a0 \u00a0 \u00a0<span class=\"Apple-converted-space\">\u00a0<\/span>unset<span class=\"Apple-converted-space\">\u00a0<\/span>dangerousCap<\/div>\n<\/li>\n<\/ol>\n<\/div>\n<\/div>\n<\/div>\n<p><span class=\"Apple-converted-space\">\u00a0<\/span><\/p>\n<h2>III Glyptodon<\/h2>\n<p>In the present article, I mentioned a few misconfiguration scans that could or should be done regularly on any GNU Linux computer. I think you realized that there are many more scans that are possible and that it can be fastidious to manually launch them everyday. But do not worry, I went trough this before and, lucky you, I created a open source tool that can do all that for you, log this information and sends an email report. I called this tool<span class=\"Apple-converted-space\">\u00a0<\/span><a href=\"http:\/\/www.sevagas.com\/?-Glyptodon-\">Glyptodon<\/a><br \/>\nGlyptodon executes all the scans mentioned in this article and many more. Plus it is actually the only tool scanning for POSIX file capabilities linked risks. Glyptodon is also compatible with &#8220;abnormal&#8221; file names (like files containing spaces).<span class=\"Apple-converted-space\">\u00a0<\/span><br \/>\nYou can find more infos about Glyptodon<span class=\"Apple-converted-space\">\u00a0<\/span><a href=\"http:\/\/www.sevagas.com\/?-Glyptodon-\">here<\/a>, and download the latest version<span class=\"Apple-converted-space\">\u00a0<\/span><a href=\"http:\/\/www.sevagas.com\/?-Download-\">here<\/a>.<span class=\"Apple-converted-space\">\u00a0<\/span><br \/>\nAnd remember my website is participative so do not hesitate to write or email any improvement or critics about any tool or article.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Either they are intentional or not, system misconfiguration can lead to very big problems. For example :<\/p>\n<p> Corruption or deletion of important system files Integrity failure (ex. eavesdropping on private data) Privilege escalation Backdoor\/rootkit installation <\/p>\n<p>To avoid these problems you should regularly scan your system for known &#8220;misconfiguration patterns&#8221;. This article will explain how to [&#8230;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4,17],"tags":[],"_links":{"self":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts\/5207"}],"collection":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=5207"}],"version-history":[{"count":1,"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts\/5207\/revisions"}],"predecessor-version":[{"id":5208,"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts\/5207\/revisions\/5208"}],"wp:attachment":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=5207"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=5207"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=5207"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}