Showing posts with label apache. Show all posts
Showing posts with label apache. Show all posts

apache xss protection headers

Setting up Apache to emit NoSniff, X-Frame and X-XSS-Protection headers

Security Headers is a service which checks that your web server is not revealing too much information. It does this by checking the HTTP headers returned with every request. The service checks to see if the server information is being leaked or if it’s reporting what scripting language is being used in the X-Powered-By header.
The service also checks and reports on a list of useful security headers like NoSniff, which prevents browsers from MIME-sniffing a response away from the declared content-type. It also checks for headers which prevent clickjacking and cross site scripting by detecting if X-Frame-Options and X-XSS-Protection are defined.

Add the headers to the Apache config file

I decided it would useful to implement these headers on the web servers I support. In particular I wanted to prevent other sites from framing my content within an iframe. By setting the X-Frame-Options to deny I am explicitly disallowing framing of any type. After a little research, these are the headers that my servers now emit:
<IfModule mod_headers.c>
    Header unset ETag
    Header set X-Frame-Options: deny
    Header set X-XSS-Protection: "1; mode=block"
    Header set X-Content-Type-Options: nosniff
    Header set X-WebKit-CSP: "default-src 'self'"
    Header set X-Permitted-Cross-Domain-Policies: "master-only"
</IfModule>
If your using the Joomla CMS, then using deny on the X-XSS-Protection will prevent the media manager from working properly. In this case you will need to relax the setting to use the same origin policy:

Header set X-Frame-Options: sameorigin

Check which mpm (multi processing module) apache is running prefork worker or mpm

In Ubuntu 14.04 apache2.4

a2query -M
 
Tells event, prefork, worker
You can change it by adding symbolic links for mpm_<chosen> from mods-available to mods-enabled in /etc/apache2.
Only one is allowed in a time.


#source /etc/apache2/envvars
#apache2 -V | grep -i "server mpm"

#httpd -V | grep -i "server mpm"



ispconfig3 setup on debian jessie remove default page setting

https://www.howtoforge.com/tutorial/perfect-server-debian-8-jessie-apache-bind-dovecot-ispconfig-3

+ remove /etc/apache2/sites-enabled/000-default..conf
or echo >  /etc/apache2/sites-enabled/000-default..conf

part 5 apache MOD REWRITE

url rewrite with mod_rewrite.so  engine#
 
feature:
1)changes url or underlying request based on condition
2)based on perl regex
  important characters are
  . = matches a  single character
  * = matches zero or more of previous character
  + = matches one or more
  ? = optional match
  [] = character class  i,e '/prod[123]*'
  [^] = character class negation
  () = groups character for assignation to backreference , eg '/(prod|products)'
3) ability to parse:
  HTTP headers
  Server VARs - %{HTTP_HOST}
  enviromental VARs i,e IE
 
 http://hostname/URI
 HTTP= scheme
 host=hostname
 urlpath = /.. = basic rewrites operate here

  •        <Directory>
  •        .htaccess
      
      
RewirteRule - basic requirement - Takes 3 argument
usage: 'RewriteRule Pattern(URL-Path) Substitution [flag[s]]
Note: URL-Path matches after HOST and before Query String
eg http://site2.example.com/prod/index.php?pid=1&user=current

Note: substitution can be:
    a) File system path(mod_alias):
    RewriteRule ^/docs /usr/local/share/apache2/docs
    Note: The above uses 'mod_rewrite' as mod_alias
   
    b)web path:
    RewirteRule ^/docs$ /documents
    note: '/documents' exists beneath the DocumentRoot
   
    c)absolute URL: 'RewirteRule ^/docs$ http://www.testing.com/docs [R]'
    rewrites users browser URL to reflect new absolute URL
   
    Redirect
    No Change: '-'
    RewriteRule \.(jpg|png) - [E=image:1]  =>this enable VAR for later usage
Note Flags are optional can be multiple [f1,f2,f3...] and include common option
[NC] = case insensitive
[E=Var:Value] - setting of enviroment VAR -useful in logs,other rewrite rule, CGI
[F] = Returns 403 to client forbidding access to resource
[G] = Returns 410 to client resource is no longer available
[R] = returns redirect 300-399 to client

note: consult mod_rewrite docs for more flags

======================================================================

ensure module is loaded

ln -s . /etc/apache2/mods-available/rewrite.loaded

vim 000-default.conf
<virtualhost>
..
#request for example.com/index.html is sent to example.com/index2.html
RewriteEngine on   #enables processing
RewriteRule ^/index.html$ /index2.html #fetches new content but does not Redirect BROWSER (eg. example.com/index.html will serve content from index2.html but BROWSER url bar is not changed )
RewriteRule ^/index.html$ /index2.html [R] #fetches new content ALSO Redirect bROWSER #url is changed in browser
RewriteRule ^/docs$ http://testing.com/docs [R]


</virtualhost>


#mod rewrite - rewrite condition
 

features:

 stackability of logic
 rules are implicitly ANDed: use [OR] to enable ORing
 one or more tests may be performed on current request
 Supports backreferences using: %N - %1..%9
 Usage 'RewriteCond TestString CondPattern (RegEx) [OR, ornext, NC,NV
   teststring:
      RewriteRule
     
Task:
omit localhost (127.0.x.y) from apache logs
 a)  RewriteCond %{REMOTE_ADDR} ^127\.0\.
 b)  RewriteRule .* - [E=localnolog] => Defines VAR to indicate request from localhost
 c)  'CustomLog /var/log/apache/access.log combined env=!localnolog'



 Force SSL for url path '/login$'

 'RewriteCond %{REQUEST_URI} ^/login'
 'RewriteCond ${SERVER_PORT} !443'
 'RewriteRule .* https://%{HTTP_HOST}/login'
OR
 RewriteCond %{REQUEST_URI} ^(/login.*)
RewriteCond %{SERVER_PORT} !443
RewriteRule .* https://%{HTTP_HOST}/login.html

Force ssl for all
RewriteCond %{SERVER_PORT} !443
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI

RewriteEngine on
RewriteCond %{HTTP_HOST} ^lokeshworhandicraft.com.np [NC,OR]
RewriteCond %{HTTP_HOST} ^www.lokeshworhandicraft.com.np [NC]
RewriteCond %{REQUEST_URI} !^/public/
RewriteRule ^(.*)$ /public/$1 [L]

 

RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}





Back reference

##mod rewrite##
Backreferences

feature
 #tokenize portion of URL for reuse
 #token range form $1 .. $9
 #uses '()' to create token
 #per server and per directory (.htaccess | <Directory>)
 Note: normally placed at site-wide or virtualHost layer
 Note: normally used to match: URL-Path

 task:
 rewrite index.html -> /new/index.html
 ------------------------------------------
 RewriteRule ^/(index.html)$ /new/$1  #equivalent to redirect[Match]
 RewriteRule ^/(index.html)$ /new/$1 [R] #equivalent to Redirect[Match] - Force redirect


 Rewrite 'localhost/docs*' -> 'http://www.facebook.com/docs*
 a. RewriteRule ^(/docs.*)$ http://linuxorg.com$1 - Redirects: '/docs* to remote server