{"id":5595,"date":"2016-02-05T12:50:05","date_gmt":"2016-02-05T04:50:05","guid":{"rendered":"http:\/\/rmohan.com\/?p=5595"},"modified":"2016-02-05T12:50:05","modified_gmt":"2016-02-05T04:50:05","slug":"harden-the-apache-web-server-on-centos-7","status":"publish","type":"post","link":"https:\/\/mohan.sg\/?p=5595","title":{"rendered":"Harden the Apache Web Server on CentOS 7"},"content":{"rendered":"<p>Harden the Apache Web Server on CentOS 7<\/p>\n<p>[root@clusterserver1 conf]# yum install httpd<br \/>\nLoaded plugins: fastestmirror<br \/>\nLoading mirror speeds from cached hostfile<br \/>\n* base: mirror.vodien.com<br \/>\n* epel: mirrors.hustunique.com<br \/>\n* extras: mirror.vodien.com<br \/>\n* updates: mirror.vodien.com<br \/>\nResolving Dependencies<br \/>\n&#8211;&gt; Running transaction check<br \/>\n&#8212;&gt; Package httpd.x86_64 0:2.4.6-40.el7.centos will be installed<br \/>\n&#8211;&gt; Finished Dependency Resolution<\/p>\n<p>Dependencies Resolved<\/p>\n<p>==========================================================================================================================<br \/>\nPackage\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 Arch\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 Version\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 Repository\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 Size<br \/>\n==========================================================================================================================<br \/>\nInstalling:<br \/>\nhttpd\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 x86_64\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 2.4.6-40.el7.centos\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 base\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 2.7 M<\/p>\n<p>Transaction Summary<br \/>\n==========================================================================================================================<br \/>\nInstall\u00a0 1 Package<\/p>\n<p>Total download size: 2.7 M<br \/>\nInstalled size: 9.4 M<br \/>\nIs this ok [y\/d\/N]: y<br \/>\nDownloading packages:<br \/>\nhttpd-2.4.6-40.el7.centos.x86_64.rpm\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 | 2.7 MB\u00a0 00:00:01<br \/>\nRunning transaction check<br \/>\nRunning transaction test<br \/>\nTransaction test succeeded<br \/>\nRunning transaction<br \/>\nInstalling : httpd-2.4.6-40.el7.centos.x86_64\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 1\/1<br \/>\nVerifying\u00a0 : httpd-2.4.6-40.el7.centos.x86_64\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 1\/1<\/p>\n<p>Installed:<br \/>\nhttpd.x86_64 0:2.4.6-40.el7.centos<\/p>\n<p>Complete!<\/p>\n<p>Hide the Apache version<\/p>\n<p>Visit your web server in Firefox. Activate Firebug by clicking the Firebug icon on the top right side.<\/p>\n<p>If you check the HTTP response headers in Firebug, it will show the Apache version along with your operating system name and version, as shown in this screenshot:<\/p>\n<p>[root@clusterserver1 test]# curl -I http:\/\/localhost\/tetete<br \/>\nHTTP\/1.1 404 Not Found<br \/>\nDate: Sun, 03 Jan 2016 17:20:18 GMT<br \/>\nServer: Apache\/2.4.6 (CentOS)<br \/>\nContent-Type: text\/html; charset=iso-8859-1<\/p>\n<p>echo &#8220;Change Apache Security&#8221;<\/p>\n<p>sed -i &#8220;s\/^ServerTokens OS$\/ServerTokens Prod\/&#8221; \/etc\/httpd\/conf\/httpd.conf<br \/>\nsed -i &#8220;s\/^ServerSignature On$\/ServerSignature Off\/&#8221; \/etc\/httpd\/conf\/httpd.conf<\/p>\n<p>echo &#8220;ServerTokens Prod&#8221;\u00a0 &gt;&gt; \/etc\/httpd\/conf\/httpd.conf<br \/>\necho &#8220;ServerSignature Off&#8221;\u00a0 &gt;&gt; \/etc\/httpd\/conf\/httpd.conf<br \/>\necho &#8220;UseCanonicalName On&#8221; &gt;&gt; \/etc\/httpd\/conf\/httpd.conf<br \/>\necho &#8220;TraceEnable Off&#8221; &gt;&gt; \/etc\/httpd\/conf\/httpd.conf<\/p>\n<p>systemctl restart httpd<\/p>\n<p>cat \/etc\/httpd\/conf\/httpd.conf | egrep &#8216;ServerTokens|ServerSignature&#8217;<\/p>\n<p>[root@clusterserver1 test]# curl -I http:\/\/localhost\/tetete<br \/>\nHTTP\/1.1 404 Not Found<br \/>\nDate: Sun, 03 Jan 2016 17:22:35 GMT<br \/>\nServer: Apache<br \/>\nContent-Type: text\/html; charset=iso-8859-1<\/p>\n<p>[root@clusterserver1 test]#<\/p>\n<p>Turn off directory listing<\/p>\n<p>Directory listing in the absence of an index file is enabled by default in Apache.<\/p>\n<p>Directory listing displays all the files from the Apache web root directory. If this is enabled,<br \/>\nthen a hacker can easily view any file, analyze it, and obtain sensitive information about an application of your Apache server.<\/p>\n<p>turn off this setting by using the Options directive in the Apache configuration file for a specific web directory.<br \/>\nvi \/etc\/httpd\/conf\/httpd.conf<\/p>\n<p>Find the section that begins with Directory \/var\/www\/html and add -Indexes in the Options directive:<br \/>\n&lt;Directory \/var\/www\/html\/&gt;<br \/>\nOptions -Indexes<br \/>\nAllowOverride None<br \/>\nRequire all granted<br \/>\n&lt;\/Directory&gt;<\/p>\n<p>Save the file and restart Apache service to reflect these changes.<br \/>\nsystemctl restart httpd<\/p>\n<p>Disable Apache directory indexes<\/p>\n<p>sed -i \\<br \/>\n-e &#8216;s~^IndexOptions \\(.*\\)$~#IndexOptions \\1~g&#8217; \\<br \/>\n-e &#8216;s~^IndexIgnore \\(.*\\)$~#IndexIgnore \\1~g&#8217; \\<br \/>\n-e &#8216;s~^AddIconByEncoding \\(.*\\)$~#AddIconByEncoding \\1~g&#8217; \\<br \/>\n-e &#8216;s~^AddIconByType \\(.*\\)$~#AddIconByType \\1~g&#8217; \\<br \/>\n-e &#8216;s~^AddIcon \\(.*\\)$~#AddIcon \\1~g&#8217; \\<br \/>\n-e &#8216;s~^DefaultIcon \\(.*\\)$~#DefaultIcon \\1~g&#8217; \\<br \/>\n-e &#8216;s~^ReadmeName \\(.*\\)$~#ReadmeName \\1~g&#8217; \\<br \/>\n-e &#8216;s~^HeaderName \\(.*\\)$~#HeaderName \\1~g&#8217; \\<br \/>\n\/etc\/httpd\/conf\/httpd.conf<\/p>\n<p>Disable unnecessary modules<\/p>\n<p>By default Apache comes with lots of unnecessary installed modules. It is a good policy to disable any unnecessary modules that are not in use.<\/p>\n<p>You can list all enabled modules on your server using the following command<\/p>\n<p>\/etc\/httpd\/conf.modules.d<\/p>\n<p>mv 00-dav.conf 00-dav.conf.bk<\/p>\n<p>mv 00-lua.conf 00-lua.conf.bk<\/p>\n<p>systemctl restart httpd<\/p>\n<p>sed -i \\<br \/>\n-e &#8216;s~^LanguagePriority \\(.*\\)$~#LanguagePriority \\1~g&#8217; \\<br \/>\n-e &#8216;s~^ForceLanguagePriority \\(.*\\)$~#ForceLanguagePriority \\1~g&#8217; \\<br \/>\n-e &#8216;s~^AddLanguage \\(.*\\)$~#AddLanguage \\1~g&#8217; \\<br \/>\n\/etc\/httpd\/conf\/httpd.conf<\/p>\n<p>sed -i \\<br \/>\n-e &#8216;s~^\\(LoadModule .*\\)$~#\\1~g&#8217; \\<br \/>\n-e &#8216;s~^#LoadModule mime_module ~LoadModule mime_module ~g&#8217; \\<br \/>\n-e &#8216;s~^#LoadModule log_config_module ~LoadModule log_config_module ~g&#8217; \\<br \/>\n-e &#8216;s~^#LoadModule setenvif_module ~LoadModule setenvif_module ~g&#8217; \\<br \/>\n-e &#8216;s~^#LoadModule status_module ~LoadModule status_module ~g&#8217; \\<br \/>\n-e &#8216;s~^#LoadModule authz_host_module ~LoadModule authz_host_module ~g&#8217; \\<br \/>\n-e &#8216;s~^#LoadModule dir_module ~LoadModule dir_module ~g&#8217; \\<br \/>\n-e &#8216;s~^#LoadModule alias_module ~LoadModule alias_module ~g&#8217; \\<br \/>\n-e &#8216;s~^#LoadModule expires_module ~LoadModule expires_module ~g&#8217; \\<br \/>\n-e &#8216;s~^#LoadModule deflate_module ~LoadModule deflate_module ~g&#8217; \\<br \/>\n-e &#8216;s~^#LoadModule headers_module ~LoadModule headers_module ~g&#8217; \\<br \/>\n-e &#8216;s~^#LoadModule alias_module ~LoadModule alias_module ~g&#8217; \\<br \/>\n\/etc\/httpd\/conf.modules.d\/00-base.conf<\/p>\n<p>Disable Apache language based content negotiation<\/p>\n<p># sed -i \\<br \/>\n-e &#8216;s~^LanguagePriority \\(.*\\)$~#LanguagePriority \\1~g&#8217; \\<br \/>\n-e &#8216;s~^ForceLanguagePriority \\(.*\\)$~#ForceLanguagePriority \\1~g&#8217; \\<br \/>\n-e &#8216;s~^AddLanguage \\(.*\\)$~#AddLanguage \\1~g&#8217; \\<br \/>\n\/etc\/httpd\/conf\/httpd.conf<\/p>\n<p>Turn off server-side includes (SSI) and CGI execution<\/p>\n<p>Server-side includes (SSI) are directives present on Web applications that are placed in HTML pages. An SSI attack allows a web application to be exploited by remotely executing arbitrary codes. The attacker can access sensitive information like password files, and execute shell commands. It is recommended that you disable server side includes and CGI execution if they are not needed.<\/p>\n<p>To do this, edit the main Apache config file:<\/p>\n<p>\/etc\/httpd\/conf\/httpd.conf&lt;\/code&gt;&lt;\/pre&gt;<\/p>\n<p>Find the section that begins with Directory \/var\/www\/html, Add -ExecCGI and -Includes in option directive:<br \/>\n&lt;Directory \/var\/www\/html\/&gt;<br \/>\nOptions -Indexes -FollowSymLinks -ExecCGI -Includes<br \/>\nAllowOverride None<br \/>\nRequire all granted<br \/>\n&lt;\/Directory&gt;<\/p>\n<p>nano \/etc\/httpd\/conf\/httpd.conf<\/p>\n<p>Add the following line:<br \/>\n&lt;Directory \/var\/www\/html\/www.vhost1.com\/&gt;<br \/>\nOptions -Includes -ExecCGI<br \/>\n&lt;\/Directory&gt;<\/p>\n<p>Save the file and restart Apache.<\/p>\n<p>Limit request size<\/p>\n<p>By default Apache has no limit on the size of the HTTP request. This can allow hackers to send large number of data.<\/p>\n<p>You can limit the requests size by using the Apache directive LimitRequestBody in combination with the Directory tag. This can help protect your web server from a denial of service (DOS) attack.<\/p>\n<p>Suppose you have a site (www.example.com), where you allow uploads, and you want to limit the upload size on this site.<\/p>\n<p>You can set value from 0 (unlimited) to 2147483647 (2GB) in the main Apache config file.<\/p>\n<p>For example, to limit the request size for the \/var\/www\/html\/www.example.com directory to 200K:<\/p>\n<p>\/etc\/httpd\/conf\/httpd.conf<\/p>\n<p>Add the following line:<br \/>\n&lt;Directory \/var\/www\/html\/www.example.com&gt;<br \/>\nLimitRequestBody 204800<br \/>\n&lt;\/Directory&gt;<\/p>\n<p>Disallow browsing outside the document root<\/p>\n<p>Unless you have a specific need, it is recommended to restrict Apache to being only able to access the document root.<\/p>\n<p>You can secure the root directory \/ with Allow and Deny options in the httpd.conf file.<\/p>\n<p>\/etc\/httpd\/conf\/httpd.conf<\/p>\n<p>Add\/edit the following line:<br \/>\n&lt;Directory \/&gt;<br \/>\nOptions None<br \/>\nOrder deny,allow<br \/>\nDeny from all<br \/>\n&lt;\/Directory&gt;<\/p>\n<p>Save the file and restart Apache:<br \/>\nsudo apachectl restart<\/p>\n<p>\u2022Options None : This will turn off all options<br \/>\n\u2022Order deny,allow : The order in which the allow and deny commands are applied<br \/>\n\u2022Deny from all : This will deny request from all to the root directory<\/p>\n<p>Secure Apache from clickjacking attacks<\/p>\n<p>Clickjacking, also known as &#8220;User Interface redress attack,&#8221; is a malicious technique to collect an infected user&#8217;s clicks. Clickjacking tricks the victim (visitor) into clicking on an infected site.<\/p>\n<p>To avoid this, you need to use X-FRAME-OPTIONS to prevent your website from being used by clickjackers.<\/p>\n<p>You can do this by editing the httpd.conf file:<br \/>\nsudo nano \/etc\/httpd\/conf\/httpd.conf<\/p>\n<p>Add the following line:<br \/>\nHeader append X-FRAME-OPTIONS &#8220;SAMEORIGIN&#8221;<\/p>\n<p>Disable ETag<\/p>\n<p>ETags (entity tags) are a well-known point of vulnerability in Apache web server.<br \/>\nETag is an HTTP response header that allows remote users to obtain sensitive information like inode number, child process ids, and multipart MIME boundary. ETag is enabled in Apache by default.<\/p>\n<p>To prevent this vulnerability, disabling ETag is recommended.<\/p>\n<p>You can do this by editing httpd.conf file:<\/p>\n<p>\/etc\/httpd\/conf\/httpd.conf<\/p>\n<p>Add the following line:<br \/>\nFileETag None<\/p>\n<p>HTTP request methods<\/p>\n<p>Apache support the OPTIONS, GET, HEAD, POST, CONNECT, PUT, DELETE, and TRACE method in HTTP 1.1 protocol.<br \/>\nSome of these may not be required, and may pose a potential security risk. It is a good idea to only enable HEAD, POST, and GET for web applications.<\/p>\n<p>You can do this by editing the httpd.conf file:<\/p>\n<p>\/etc\/httpd\/conf\/httpd.conf<\/p>\n<p>Find the section that begins with Directory \/var\/www\/html. Add the following lines under this section:<br \/>\n&lt;LimitExcept GET POST HEAD&gt;<br \/>\ndeny from all<br \/>\n&lt;\/LimitExcept&gt;<\/p>\n<p>Save the file and restart Apache:<\/p>\n<p>Secure Apache from XSS attacks<\/p>\n<p>Cross-site scripting (XSS) is one of the most common application-layer vulnerabilities in Apache server. XSS enables attackers to inject client-side script into web pages viewed by other users. Enabling XSS protection is recommended.<\/p>\n<p>You can do this by editing the httpd.conf file:<\/p>\n<p>\/etc\/httpd\/conf\/httpd.conf<\/p>\n<p>Add the following line:<br \/>\n&lt;IfModule mod_headers.c&gt;<br \/>\nHeader set X-XSS-Protection &#8220;1; mode=block&#8221;<br \/>\n&lt;\/IfModule&gt;<\/p>\n<p>HTTP request methods<\/p>\n<p>Apache support the OPTIONS, GET, HEAD, POST, CONNECT, PUT, DELETE, and TRACE method in HTTP 1.1 protocol. Some of these may not be required, and may pose a potential security risk. It is a good idea to only enable HEAD, POST, and GET for web applications.<\/p>\n<p>You can do this by editing the httpd.conf file:<br \/>\nsudo nano \/etc\/httpd\/conf\/httpd.conf<\/p>\n<p>Find the section that begins with Directory \/var\/www\/html. Add the following lines under this section:<br \/>\n&lt;LimitExcept GET POST HEAD&gt;<br \/>\ndeny from all<br \/>\n&lt;\/LimitExcept&gt;<\/p>\n<p>Save the file and restart Apache:<\/p>\n<p>sudo apachectl restart<\/p>\n<p>Secure Apache from XSS attacks<\/p>\n<p>Cross-site scripting (XSS) is one of the most common application-layer vulnerabilities in Apache server. XSS enables attackers to inject client-side script into web pages viewed by other users. Enabling XSS protection is recommended.<\/p>\n<p>You can do this by editing the httpd.conf file:<\/p>\n<p>\/etc\/httpd\/conf\/httpd.conf<\/p>\n<p>Add the following line:<br \/>\n&lt;IfModule mod_headers.c&gt;<br \/>\nHeader set X-XSS-Protection &#8220;1; mode=block&#8221;<br \/>\n&lt;\/IfModule&gt;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Harden the Apache Web Server on CentOS 7<\/p>\n<p>[root@clusterserver1 conf]# yum install httpd Loaded plugins: fastestmirror Loading mirror speeds from cached hostfile * base: mirror.vodien.com * epel: mirrors.hustunique.com * extras: mirror.vodien.com * updates: mirror.vodien.com Resolving Dependencies &#8211;&gt; Running transaction check &#8212;&gt; Package httpd.x86_64 0:2.4.6-40.el7.centos will be installed &#8211;&gt; Finished Dependency Resolution<\/p>\n<p>Dependencies Resolved<\/p>\n<p>========================================================================================================================== Package [&#8230;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[12],"tags":[],"_links":{"self":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts\/5595"}],"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=5595"}],"version-history":[{"count":1,"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts\/5595\/revisions"}],"predecessor-version":[{"id":5596,"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts\/5595\/revisions\/5596"}],"wp:attachment":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=5595"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=5595"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=5595"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}