Security coding

Post on 27-Jun-2015

743 views 0 download

Tags:

description

The knowledge programmers need to know!

transcript

開發人員該知道?

·•資訊安全觀念 CIA

·•資訊安全策略 3A

·•環境設定與工具

·•資安程式開發

Confidentiality !

Integrity !

Availability

Authentication !

Authorization !

Accounting

Secure Coding Guidelines

• Avoid duplication

• Restrict privileges

• Establish trust boundaries

• Minimize the number of permission checks

• Encapsulate

Denial of Service

• Release resources in all cases

• "Zip bombs"

• "Billion laughs attack"

• "Log bombs"

• Infinite loops: "Messages poison”

• Resource limit checks should not suffer from integer overflow

Confidential Information

• Purge sensitive information from exceptions

• Do not log highly sensitive information

• Consider purging highly sensitive from memory after use

Injection & Inclusion

• Avoid dynamic SQL

• SQL Injection

• XML and HTML generation requires care

• Cross Site Scripting(XSS)

• Restrict XML inclusion

• XML External Entity (XXE)

Accessibility & Extensibility

• Limit the accessibility of classes, interfaces, methods, and fields.

• Limit the accessibility of namespace.

• Isolate unrelated code.

• Limit the extensibility of classes and methods.

• Understand how a superclass can affect subclass behavior.

Accessibility & Extensibility

Reducing the "attack surface" of the code

Input Validation

• Validate inputs

• 有效值 • 特殊字元:URL / HTML / XML

Mutability

• Prefer immutability for value types

• Create copies of mutable output values

• Make public static fields as constants

• Do not expose mutable statics

Object Construction

• Avoid exposing constructors of sensitive classes

• Prevent the unauthorized construction of sensitive classes

• Prevent constructors from calling methods that can be overridden

Serialization & Deserialization

• Avoid serialization for security-sensitive classes

• Guard sensitive data during serialization

• View deserialization the same as object construction

Access Control

• Understand how permissions are checked

• Beware of callback (Lifecycle) methods

• Callback methods are generally invoked from the system with FULL permissions

• Be careful caching results of potentially privileged operations

Security Coding 涵蓋了?·•Authentication & Authorization

·•Session & State management

·•Input & Output

·•File Handling

·•Error Handling

·•Auditing & Logging

·•Cryptography

·•Configuration

Application Logging Tips

使⽤用適當⼯工具

• ERROR

• WARN

• INFO

• DEBUG

• TRACE

資訊分級

Debug.WriteLine("Returning orders: {0}", orders);

到底在Log啥?

if(MyLog.isDebugEnabled()){ MyLog.Debug("It's a secret!”); }

避免副作⽤用!

try { MyLog.Trace("Id=" + request.getUser().getId() ); MyLog.Trace("Accesses " + manager.getPage().getUrl().toString()); } catch(Exception e) { }

簡潔描述!

if(message is TextMessage) //... else MyLog.Warn("Unknown message type");

精確調整輸出格式!

時戳 等級 程式名稱 訊息 {0:HH:mm:ss} {1} {2} {3}

public String printDocument(Document doc, Mode mode) { MyLog.Debug("Entering printDocument(doc={0}, mode={1})", doc, mode); String id = //... MyLog.Debug("Leaving printDocument(): {0}", id); return id; }

記錄⽅方法參數與回傳值!

記錄例外狀況資訊!

MyLog.Error("IO exception", e); throw new MyCustomException(e);

易於解讀!MyLog.Debug("Request TTL set to: {0} ({1})", new DateTime(ttl), ttl); // Request TTL set to: Wed Apr 28 20:14:12 CEST 2010 (1272478452437) !String duration = DurationFormatUtils.formatDurationWords(durationMillis, true, true); MyLog.Info("Importing took: {0}ms ({1})", durationMillis, duration); // Importing took: 123456789ms (1 day 10 hours 17 minutes 36 seconds)

Summary

·•Use for daily log or tracing

·•Audience

·•Easy to read

·•Easy to analysis

·•Will it cause error?

·•System performance issue

Fixing&bugs&by&releasing&a&patch&costs&60&6mes&more&than&fixing&the&same&bug&during&the&design&stage.&

Cost%of%fixing%security%flaws%during%different%development%phases%

Source:&h"p://www.auto.tuwien.ac.at0

Phases%

Cost%

Why Secure Coding Training is Important?