web安全——HTTPS中间人攻击

一、中间人攻击过程
正常HTTPS通道建立过程
  1. 客户端向服务器发送HTTPS请求;
  2. 服务器返回公钥
  3. 客户端生成私钥,并使用公钥加密,发送给服务器;
  4. 服务器使用与公钥配对的私钥解密,获取客户端生成的私钥
  5. 客户端、服务器使用协商好的私钥进行加密数据传输。
中间人攻击
  1. 客户端向服务器发送HTTPS请求;
  2. 服务器返回公钥
    2.1. 中间人劫持服务器数据,生成假公钥,返回给客户端;(同时自己备份公钥,后边有用)
  3. 客户端生成私钥,并使用假公钥加密,发送给服务器;
    3.1. 中间人劫持客户端数据,使用与假公钥配对的假私钥解密获取客户端生成的私钥(已暴露);同时使用劫持的公钥加密私钥,发送给服务器;
  4. 服务器使用与公钥配对的私钥解密,获取客户端生成的私钥
  5. 客户端、服务器使用协商好的私钥进行加密数据传输。

补充

  1. 公钥私钥假公钥假私钥:对应非对称加密算法
  2. 私钥私钥:对应对称加密算法
二、防御方法
  1. 服务端在发送浏览器的公钥中加入 CA 证书,浏览器可以验证 CA 证书的有效性(大概就是验证公钥与CA证书是否匹配,保证公钥是来自于服务器而不是中间人
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Web Service 安全是指保护 Web Service 不受恶意击和非法访问的一系列措施。其中,Basic 验证是最简单的一种身份验证方式,也是最常用的一种。 Basic 验证的原理是在 HTTP 头部中添加一个 Authorization 字段,该字段包含一个经过编码的用户名和密码。在服务端接收到请求后,会验证该用户名和密码是否正确,如果正确,则允许访问 Web Service,否则拒绝访问。 下面是一个使用 Basic 验证的 Web Service 的示例: ```xml <definitions name="HelloWorld" targetNamespace="http://www.example.org/HelloWorld" xmlns="http://schemas.xmlsoap.org/wsdl/" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="http://www.example.org/HelloWorld" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <types> <xsd:schema targetNamespace="http://www.example.org/HelloWorld"> <xsd:element name="sayHello" type="xsd:string"/> <xsd:element name="sayHelloResponse" type="xsd:string"/> </xsd:schema> </types> <message name="sayHelloRequest"> <part name="name" type="xsd:string"/> </message> <message name="sayHelloResponse"> <part name="greeting" type="xsd:string"/> </message> <portType name="HelloWorld"> <operation name="sayHello"> <input message="tns:sayHelloRequest"/> <output message="tns:sayHelloResponse"/> </operation> </portType> <binding name="HelloWorldSoapBinding" type="tns:HelloWorld"> <soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/> <operation name="sayHello"> <soap:operation soapAction=""/> <input> <soap:header message="tns:sayHelloRequest" part="Authorization" use="required"/> <soap:body use="literal"/> </input> <output> <soap:body use="literal"/> </output> </operation> </binding> <service name="HelloWorld"> <port name="HelloWorldPort" binding="tns:HelloWorldSoapBinding"> <soap:address location="http://localhost:8080/HelloWorld"/> </port> </service> </definitions> ``` 在上面的示例中,我们在 `<binding>` 标签中定义了一个 Authorization 头部,它的 use 属性设置为 required,表示该头部是必须的。这个头部包含了一个经过用户名和密码编码的字符串。 在服务端,我们需要在代码中对该 Authorization 头部进行解码,然后验证用户名和密码是否正确。如果正确,则允许访问 Web Service,否则拒绝访问。下面是一个使用 Java 代码实现的 Basic 验证的示例: ```java public class HelloWorldImpl implements HelloWorld { @Override public String sayHello(String name) { String auth = (String) wsContext.getMessageContext().get(MessageContext.HTTP_REQUEST_HEADERS).get("Authorization").get(0); String[] parts = auth.split(" "); byte[] decoded = Base64.getDecoder().decode(parts[1]); String credentials = new String(decoded); String[] credentialParts = credentials.split(":"); String username = credentialParts[0]; String password = credentialParts[1]; if (username.equals("admin") && password.equals("password")) { return "Hello, " + name + "!"; } else { throw new RuntimeException("Unauthorized access"); } } } ``` 在上面的代码中,我们首先从 MessageContext 中获取 Authorization 头部,然后对该头部进行解码。解码后得到的是一个用户名和密码的字符串,我们再对该字符串进行拆分,得到用户名和密码。最后,我们根据用户名和密码的正确性来决定是否允许访问 Web Service。 需要注意的是,Basic 验证并不是一种安全性很高的身份验证方式。因为它使用明文传输用户名和密码,所以容易被窃取。因此,在实际应用中,我们通常会使用更加安全的身份验证方式,比如 Digest 验证或 SSL/TLS 验证。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值