1. 程式人生 > >一些安全防護辦法

一些安全防護辦法

Created 星期三 08 三月 2017

Apache JServ protocol service

描述:

AJP(Apache Jserv Protocol)的connector:AJP為二進位制協議,專用於tomcat與apache建立通訊的, 在httpd反向代理使用者請求至tomcat時使用。

風險等級:

中。

解決辦法:

  • 防火牆上關閉tomcat使用的ajp埠8009。
  • 或修改tomcat的 server.xml,註釋掉ajp connector:

cookie缺少 secure flag

描述:

當設定為true時,表示建立的 Cookie 會被以安全的形式向伺服器傳輸,也就是隻能在 HTTPS 連線中被瀏覽器傳遞到伺服器端進行會話驗證,如果是 HTTP 連線則不會傳遞該資訊,所以不會被竊取到Cookie 的具體內容。

解決辦法:

(需要配套https使用)。
不同情況不同處理。
對於servlet3.0的web應用,在web.xml中新增:

<session-config>
       <cookie-config>
           <secure>true<secure>
        </cookie-config>
</session-config>

或新增過濾器,過濾器中設定cookie.setSecure(true);

參考文件

http://blog.csdn.net/a19881029/article/details/27536917

描述:

Web 應用程式設定了不含“HttpOnly”屬性的會話 cookie。由於此會話 cookie 不包含“HttpOnly”屬性,因此注入站點的惡意指令碼可能訪問此 cookie,並竊取它的值。任何儲存在會話令牌中的資訊都可能被竊取,並在稍後用於身份盜竊或使用者偽裝
如果在Cookie中設定了"HttpOnly"屬性,那麼通過程式(JS指令碼、Applet等)將無法讀取到Cookie資訊,這樣能有效的防止XSS攻擊

解決辦法:

PHP中的設定

PHP5.2以上版本已支援HttpOnly引數的設定,同樣也支援全域性的HttpOnly的設定,在php.ini中
session.cookie_httponly = 1
設定其值為1或者TRUE,來開啟全域性的Cookie的HttpOnly屬性
當然也支援在程式碼中來開啟:
<?php ini_set("session.cookie_httponly", 1);
// or session_set_cookie_params(0, NULL, NULL, NULL, TRUE);
?>

java web應用

對於servlet3.0的web應用,在web.xml中新增:

<cookie-config>
    <http-only>true</http-only>
</cookie-config>

參考文件:

  • http://blog.csdn.net/ssergsw/article/details/9137785?utm_source=tuicool
  • http://blog.sina.com.cn/s/blog_4dd475390102eu7p.html

http明文傳輸

描述:

使用http傳輸,可以被攻擊者可通過內網嗅探的方式獲取系統資訊,包括使用者名稱、cookie等。

風險等級:

中。

解決辦法:

使用https協議,步驟略。

登陸頁面缺少動態驗證碼

描述:

使用者登入只有賬戶+密碼的認證方式,缺少動態驗證碼機制,容易被暴力破解。

解決辦法:

新增驗證碼登陸機制,步驟略。

伺服器啟用了不安全的http方法

描述:

伺服器允許: GET, HEAD, POST, PUT, DELETE, TRACE, OPTIONS

風險等級:

中。

解決辦法:

建議關閉無用HTTP方法,僅開放允許GET/POST方法。
方法一:
修改所有web應用的web.xml,新增:
<security-constraint> <web-resource-collection> <web-resource-name>Forbidden</web-resource-name> <url-pattern>/*</url-pattern> <http-method>PUT</http-method> <http-method>DELETE</http-method> <http-method>HEAD</http-method> <http-method>OPTIONS</http-method> <http-method>TRACE</http-method> </web-resource-collection> <auth-constraint/> </security-constraint>
方法2:
在nginx裡新增限制:

limit_except GET POST {  
    deny  all;  
    }

http方法說明:
PUT 向指定的目錄上載檔案
DELETE 刪除指定的資源
COPY 將指定的資源複製到Destination訊息頭指定的位置
MOVE 將指定的資源移動到Destination訊息頭指定的位置
SEARCH 在一個目錄路徑中搜索資源
PROPFIND 獲取與指定資源有關的資訊,如作者、大小與內容型別
TRACE 在響應中返回伺服器收到的原始請求
------------------------------------------------------

登陸頁面設定失敗次數限制

描述:

多次登陸失敗後,限制禁止使用者登陸。

解決辦法:

略。

Clickjacking: X-Frame-Options header missing

描述:

點選劫持(ClickJacking)是一種視覺上的欺騙手段。攻擊者使用一個透明的、不可見的iframe,覆蓋在一個網頁上,然後誘使使用者在該網頁上進行操作,此時使用者將在不知情的情況下點選透明的iframe頁面。通過調整iframe頁面的位置,可以誘使使用者恰好點選在iframe頁面的一些功能性按鈕上。HTTP 響應頭資訊中的X-Frame-Options,可以指示瀏覽器是否應該載入一個 iframe 中的頁面。如果伺服器響應頭資訊中沒有X-Frame-Options,則該網站存在ClickJacking攻擊風險。網站可以通過設定 X-Frame-Options 阻止站點內的頁面被其他頁面嵌入從而防止點選劫持。

解決辦法:

修改web伺服器配置,新增X-Frame-Options響應頭。
賦值有如下三種:1、DENY:不能被嵌入到任何iframe或者frame中。
2、SAMEORIGIN:頁面只能被本站頁面嵌入到iframe或者frame中。
3、ALLOW-FROM uri:只能被嵌入到指定域名的框架中。

具體操作:

tomcat配置:

修改conf/web.xml新增下面的過濾器:

<filter>
    <filter-name>HTTP Header Security Filter</filter-name>
    <filter-class> org.apache.catalina.filters.HttpHeaderSecurityFilter</filter-class>
    <init-param>
        <param-name>antiClickJackingOption</param-name>
        <param-value> DENY或SAMEORIGIN或ALLOW-FROM</param-value>
    </init-param>
</filter>
<filter-mapping>
    <filter-name>HTTP Header Security Filter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

程式碼實現:

res.addHeader("X-FRAME-OPTIONS",mode );

nginx配置:

配置 nginx 傳送 X-Frame-Options 響應頭,把下面這行新增到 'http', 'server' 或者 'location' 的配置中:
add_header X-Frame-Options SAMEORIGIN;

Apache配置:

配置 Apache 在所有頁面上傳送 X-Frame-Options 響應頭,需要把下面這行新增到 'site' 的配置中:
Header always append X-Frame-Options SAMEORIGIN

測試方法

參考文件

X-Frame-Options 響應頭
https://tomcat.apache.org/tomcat-8.0-doc/config/filter.html

X-Content-Type-Options

風險等級

解決辦法

修改tomcat 的conf/web.xml新增下面的過濾器:

<filter>
    <filter-name>HTTP Header Security Filter</filter-name>
    <filter-class> org.apache.catalina.filters.HttpHeaderSecurityFilter</filter-class>
  </filter>
  <filter-mapping>
    <filter-name>HTTP Header Security Filter</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>

對於nginx
add_header X-Content-Type-Options nosniff; # 禁止嗅探檔案型別

參考文件

https://tomcat.apache.org/tomcat-8.0-doc/config/filter.html
https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/X-Content-Type-Options

X-XSS-Protection

風險等級

描述

HTTP X-XSS-Protection 響應頭是Internet Explorer,Chrome和Safari的一個功能,當檢測到反映的跨站點指令碼 (XSS)時,停止頁面載入。

解決辦法

修改conf/web.xml新增下面的過濾器:

<filter>
    <filter-name>HTTP Header Security Filter</filter-name>
    <filter-class> org.apache.catalina.filters.HttpHeaderSecurityFilter</filter-class>
  </filter>
  <filter-mapping>
    <filter-name>HTTP Header Security Filter</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping> 

對於nginx
add_header X-XSS-Protection "1; mode=block"; # XSS 保護

參考文件

https://tomcat.apache.org/tomcat-8.0-doc/config/filter.html
https://developer.mozilla.org/zh-CN/docs/Web/HTTP/Headers/X-XSS-Protection

httpSlow HTTP DoS漏洞

描述:

針對任意HTTP Server,建立一個連線,以很低的速度發包,並保持住這個連線不斷開。如果客戶端持續建立這樣的連線,那麼伺服器上可用的連線池將很快被佔滿,從而導致拒絕服務攻擊。

風險等級:

高。

解決辦法:

https://community.qualys.com/blogs/securitylabs/2011/11/02/how-to-protect-against-slow-http-attacks

BREACH attack

描述

風險等級:

解決辦法:

  1. Disabling HTTP compression(禁止http壓縮)
  2. Separating secrets from user input
  3. Randomizing secrets per request
  4. Masking secrets (effectively randomizing by XORing with a random secret per request)
  5. Protecting vulnerable pages with CSRF
  6. Length hiding (by adding random number of bytes to the responses)
  7. Rate-limiting the requests(限制請求速率)

例如:nginx 關閉gzip壓縮。

參考文件:

http://breachattack.com/

Password type input with auto-complete enabled

風險等級:

解決辦法:

<form>
<input type="text" name="username" autocomplete="off">
<input type="password" autocomplete="new-password" onfocus="if (this.hasAttribute('readonly')) {this.removeAttribute('readonly');}" readonly >
</form>

參考文件:

https://stackoverflow.com/questions/32369/disable-browser-save-password-functionality

Content Security Policy

風險等級:

解決辦法
(1)
兩種方法可以啟用 CSP。一種是通過 HTTP 頭資訊的Content-Security-Policy的欄位。

Content-Security-Policy: script-src 'self'; object-src 'none';
style-src cdn.example.org third-party.org; child-src https:
自定義filter
public class MyFilter implements Filter {
@Override
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {

    HttpServletResponse httpResponse = (HttpServletResponse) response;
    httpResponse.setHeader("Content-Security-Policy", "frame-ancestors 'self'");

    chain.doFilter(request, response);
}

}

另一種是通過網頁的標籤。

上面程式碼中,CSP 做了如下配置。

  • 指令碼:只信任當前域名
  • 樣式表:只信任cdn.example.org和third-party.org
  • 框架(frame):必須使用HTTPS協議載入
  • 其他資源:沒有限制

參考文件
http://www.ruanyifeng.com/blog/2016/09/csp.html
http://www.jackieathome.net/archives/317.html