软件安全不是安全软件。此处我们关注的主题包括身份验证、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 中,persistent authentication 票据的有效期就是在 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
对象代表需要身份验证的服务器或服务器上的某个区域(通常称为 Realm)。NSURLConnectionDelegate.connection(_:canAuthenticateAgainstProtectionSpace:)
方法之后立即调用应执行身份验证的 NSURLConnectionDelegate.connection(_:didReceive:)
方法。这样,NSURLConnectionDelegate
便可在尝试对保护空间进行身份验证之前检查它。通过返回 true
,代理指示它可以处理身份验证,在对 connection(_:didReceive:)
的后续调用中它也确实这样做了。如果您的代理未实施此方法且保护空间使用客户端证书身份验证或服务器信任身份验证,系统将会尝试使用用户的密钥链来验证哪些可能不是所需的行为。NSURLConnection
代理处理身份验证质询,而无需先验证应用程序是否知道如何处理特定保护空间的身份验证质询。NSURLProtectionSpace
对象代表需要身份验证的服务器或服务器上的某个区域(通常称为 Realm)。NSURLConnectionDelegate.connection(_:canAuthenticateAgainstProtectionSpace:)
方法之后立即调用应执行身份验证的 NSURLConnectionDelegate.connection(_:didReceive:)
方法。这样,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)
}
22
的 AWS 安全组 (0.0.0.0/0
),其中 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" ]