軟體安全性並非安全性軟體。我們關注驗證、Access Control、保密性、加密以及權限管理之類的主題。
範例 2:以下伺服器頁面指令停用了事件驗證。
...
<system.web>
...
<pages enableEventValidation="false">
...
</pages>
</system.web>
<%@ Page ... EnableEventValidation="false" %>
HttpRuntimeSection
物件的 EnableHeaderChecking
特性設為 false
,在程式上停用此行為。
Configuration config = WebConfigurationManager.OpenWebConfiguration("/MyApp");
HttpRuntimeSection hrs = (HttpRuntimeSection) config.GetSection("system.web/httpRuntime");
hrs.EnableHeaderChecking = false;
W3Clogger.loggingFields
可以設定為允許記錄敏感資料,例如私人資訊和系統資訊。
builder.Services.AddW3CLogging(logging =>
logging.LoggingFields = W3CLoggingFields.All;
... )
Example 1
顯示如何使用 All
旗標,將 W3Clogging 設定為記錄 Http 要求中的所有可能欄位,例如 ClientIpAddress
、ServerName
、ServerIpAddress
、ServerPort
、UserName
和 Cookie
。FormsAuthentication.RedirectFromLoginPage()
方法會發出 authentication 許可證,此許可證可讓使用者在特定的一段期間內維持驗證的身分。若將該方法的第二個參數設為 false
,它會發出一個臨時的 authentication 許可證,其有效期在 web.config
中配置。若將第二個參數設為 true
,此方法會發出 Persistent Authentication 許可證。在.NET 2.0 中,許可證的有效期為在 web.config
中配置的值,但在.NET 1.1 中,Persistent Authentication 許可證的有效期有個不可思議的預設值 - 50 年! useUnsafeHeaderParsing
屬性設定為 true
,導致存在不安全的 HTTP 標頭處理。由於有關 HTTP 標頭的驗證規則不足,此設定讓攻擊可以瞄準易受攻擊的應用程式。 true
時,不會執行下列驗證: useUnsafeHeaderParsing
設定為 true。
...
<configuration>
<system.net>
<settings>
<httpWebRequest useUnsafeHeaderParsing="true" />
</settings>
</system.net>
</configuration>
...
Page.EnableViewStateMac = false;
public ActionResult ActionName(Model model, string returnurl)
{
// ... controller logic
}
Example 2::The following Razor code creates a form in the resulting HTML without the built-in defense against cross-site request forgery. Note that parameter
@using (Html.BeginForm(new { ReturnUrl = ViewBag.ReturnUrl })) {
// ... form elements
}
antiforgery
is set to either false
or null
:
@using (Html.BeginForm("actionName", "controllerName", routeValues, FormMethod.Post, antiforgery: false, htmlAtts)) {
// ... form elements
}
NSURLConnectionDelegate.connection(_:willSendRequestFor:)
委派回呼方法將讓系統忽略 NSURLConnectionDelegate.connection(_:canAuthenticateAgainstProtectionSpace:)
和 NSURLConnectionDelegate.connection(_:didReceive:)
方法。NSURLConnectionDelegate.connection(_:willSendRequestFor:)
委派方法允許委派立即就連線驗證做出明智的決策。如果委派執行此方法,則無需執行 NSURLConnectionDelegate.connection(_:canAuthenticateAgainstProtectionSpace:)
或 NSURLConnectionDelegate.connection(_:didReceive:)
。事實上,不會叫用這些方法,因此將會忽略對其進行的任何安全檢查。NSURLConnectionDelegate.connection(_:willSendRequestFor:)
委派回呼方法將讓系統忽略 NSURLConnectionDelegate.connection(_:canAuthenticateAgainstProtectionSpace:)
和 NSURLConnectionDelegate.connection(_:didReceive:)
方法。NSURLConnectionDelegate.connection(_:willSendRequestFor:)
委派方法允許委派立即就連線驗證做出明智的決策。如果委派執行此方法,則無需執行 NSURLConnectionDelegate.connection(_:canAuthenticateAgainstProtectionSpace:)
或 NSURLConnectionDelegate.connection(_:didReceive:)
。事實上,不會叫用這些方法,因此將會忽略對其進行的任何安全檢查。NSURLConnection
物件、NSURLSession
物件或 NSURLDownload
物件的委派負責評估信任鏈。 NSURLCredential
:
-(void)URLSession:(NSURLSession *)session didReceiveChallenge:(NSURLAuthenticationChallenge *)challenge completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition disposition, NSURLCredential * credential)) completionHandler {
...
[challenge.sender useCredential:[NSURLCredential credentialForTrust: challenge.protectionSpace.serverTrust] forAuthenticationChallenge:challenge];
...
}
NSURLConnection
物件、NSURLSession
物件或 NSURLDownload
物件的委派負責評估信任鏈。 NSURLCredential
:
func urlSession(_ session: URLSession, didReceive challenge: URLAuthenticationChallenge, completionHandler: @escaping (URLSession.AuthChallengeDisposition, URLCredential?) -> Void) {
...
let cred = URLCredential(user: "foo", password: "bar", persistence: .none)
let trust = challenge.protectionSpace.serverTrust
let cred = URLCredential(trust:trust!)
...
}
NSURLConnection
物件、NSURLSession
物件或 NSURLDownload
物件的委派負責評估伺服器信任。若要評估伺服器信任,應使用從伺服器的 NSURLProtectionSpace
物件之 serverTrust
方法取得的信任呼叫 SecTrustEvaluate(_:_:)
方法。 SecTrustEvaluate(_:_:)
方法傳回兩個不同的值:OSStatus
值代表結果代碼。cancel(_:)
取消驗證挑戰。
SecTrustRef trust = [[challenge protectionSpace] serverTrust];
SecTrustResultType result = kSecTrustResultInvalid;
OSStatus status = SecTrustEvaluate(trust, &result);
completionHandler(NSURLSessionAuthChallengeUseCredential, [challenge proposedCredential]);
NSURLConnection
物件、NSURLSession
物件或 NSURLDownload
物件的委派負責評估伺服器信任。若要評估伺服器信任,應使用從伺服器的 NSURLProtectionSpace
物件之 serverTrust
方法取得的信任呼叫 SecTrustEvaluate(_:_:)
方法。 SecTrustEvaluate(_:_:)
方法傳回兩個不同的值:OSStatus
值代表結果代碼。cancel(_:)
取消驗證挑戰。
let trust = challenge.protectionSpace.serverTrust
var result = SecTrustResultType.invalid
let status = SecTrustEvaluate(trust!,&secresult)
completionHandler(Foundation.URLSession.AuthChallengeDisposition.useCredential, challenge.proposedCredential)
NSURLConnection
委派會直接處理驗證挑戰,而不會先驗證應用程式是否瞭解如何針對特定保護空間處理驗證挑戰。NSURLProtectionSpace
物件可代表需要驗證的伺服器或伺服器上的區域 (通常稱為領域)。NSURLConnectionDelegate.connection(_:didReceive:)
方法之前呼叫 NSURLConnectionDelegate.connection(_:canAuthenticateAgainstProtectionSpace:)
方法。這允許 NSURLConnectionDelegate
先檢查保護空間,然後再嘗試進行驗證。透過傳回 true
,委派表示其可處理驗證表單 (將在後續呼叫 connection(_:didReceive:)
時執行)。如果您的委派未執行此方法,而保護空間使用用戶端憑證驗證或伺服器信任驗證,則系統將嘗試使用使用者的金鑰鏈來驗證哪些可能不是想要的運作方式。NSURLConnection
委派會直接處理驗證挑戰,而不會先驗證應用程式是否瞭解如何針對特定保護空間處理驗證挑戰。NSURLProtectionSpace
物件可代表需要驗證的伺服器或伺服器上的區域 (通常稱為領域)。NSURLConnectionDelegate.connection(_:didReceive:)
方法之前呼叫 NSURLConnectionDelegate.connection(_:canAuthenticateAgainstProtectionSpace:)
方法。這允許 NSURLConnectionDelegate
先檢查保護空間,然後再嘗試進行驗證。透過傳回 true
,委派表示其可處理驗證表單 (將在後續呼叫 connection(_:didReceive:)
時執行)。如果您的委派未執行此方法,而保護空間使用用戶端憑證驗證或伺服器信任驗證,則系統將嘗試使用使用者的金鑰鏈來驗證哪些可能不是想要的運作方式。NSURLConnection
或 NSURLSession
的驗證處理回呼無法檢查其將認證傳送至的目標主機。NSURLConnection
或 NSURLSession
回呼來處理驗證要求。忘記確保驗證要求來自預期的主機,將導致認證傳送至應用程式載入的每個 URL。此外,未能檢查認證是否能安全傳送將導致認證遭竊取。
- (void)URLSession:(NSURLSession *)session didReceiveChallenge:(NS URLAuthenticationChallenge *)challenge completionHandler:(void (^)(NS URLSessionAuthChallengeDisposition, NSURLCredential *))completionHandler {
NSString *user = [self getUser];
NSString *pass = [self getPassword];
NSURLCredential *cred = [NSURLCredential credentialWithUser:user
password:pass persistence:NSURLCredentialPersistenceForSession];
completionHandler(NSURLSessionAuthChallengeUseCredential, credential);
}
NSURLConnection
或 NSURLSession
的驗證處理回呼無法檢查其將認證傳送至的目標主機。NSURLConnection
或 NSURLSession
回呼來處理驗證要求。忘記確保驗證要求來自預期的主機,將導致認證傳送至應用程式載入的每個 URL。此外,未能檢查認證是否能安全傳送將導致認證遭竊取。
func urlSession(_ session: URLSession, didReceive challenge: URLAuthenticationChallenge, completionHandler: @escaping (URLSession.AuthChallengeDisposition, URLCredential?) -> Void) {
let user = getUser()
let pass = getPassword()
let credential = URLCredential(user: user, password: pass, persistence: .none)
completionHandler(Foundation.URLSession.AuthChallengeDisposition.useCredential, credential)
}
0.0.0.0/0
) (即 TCP 連接埠 22
) 開放的 AWS 安全性群組,其中 SSH 伺服器通常回應傳入的連線要求。
- name: Task to open SSH port
amazon.aws.ec2_group:
name: demo_security_group
description: Open the ssh port to the world
state: present
vpc_id: 123456
region: us-west-1
rules:
- proto: tcp
ports: 22
cidr_ip: 0.0.0.0/0
- name: Create Lambda policy statement for S3 event notification
community.aws.lambda_policy:
action: lambda:InvokeFunction
function_name: functionName
principal: *
statement_id: lambda-s3-demobucket-access-log
source_arn: arn:aws:s3:us-west-2:123456789012:demobucket
source_account: 123456789012
state: present
publicly_accessible
參數設定為 yes
來明確啟用對 Amazon Redshift 叢集的公開存取。
- name: example1
community.aws.redshift:
identifier: mycluster
command: create
db_name: mydb
node_type: ds1.xlarge
username: "{{ username }}"
password: "{{ password }}"
publicly_accessible: yes
encrypted
參數設定為 false
。
- name: Basic AMI Creation
amazon.aws.ec2_ami:
state: present
instance_id: i-xxxxxx
name: test_ami
device_mapping:
device_name: /dev/sda
encrypted: false
encrypt
參數設定為 no
。
- name: EFS provisioning
community.aws.efs:
state: present
name: myTestEFS
encrypt: no
targets:
- subnet_id: subnet-12345678
security_groups: [ "sg-87654321" ]