网上看到的一些关于网络安全的学习资料小结。
对称加密: 通信双方共享同一个密钥。发送方用它来加密,接收方用它来解密。
非对称加密: 有公钥和私钥。
现在的做法一般是用非对称加密生成?钥(公钥还是私钥?)用于传输?,然后再用对称加密进行通信。
加密 : 公钥加密,私钥解密 (公钥是lock,私钥是key)
数字签名证书: 私钥加密(生成签名),公钥解密(验证签名)
既然是加密,那肯定是不希望别人知道我的消息,所以只有我才能解密,所以可得出公钥负责加密,私钥负责解密;同理,既然是签名,那肯定是不希望有人冒充我发消息,只有我才能发布这个签名,所以可得出私钥负责签名,公钥负责验证。
对称加密分为两大类: 序列型 stream cipher 和 分组型 block cipher (更流行).
stream cipher: RC4
RC4: secret key 通过RC4 会生成一个无限长的序列,Plain Text跟RC4进行操作(比如说异或)会生成加密文本。不安全。
block cipher: DES, 3DES, AES, BLOWFISH, RC5, RC6.
非对称加密: RSA
Hash函数最快,其次是对称加密,再是非对称加密。
计算机中的Hash表主要用来存储和查找。
密码学中的Hash函数用途:
- 完整性检测(下载文件,解压文件)
- 注意:奇偶校验和CRC校验都没有抗数据篡改的能力。Hash函数也不可以,但加上key就可以(见数字签名)。
- 登录验证,校对密码(加上salt)
- 数字签名(加上key)
- 区块链
Hash算法有:
- MD5: 128 bits
- SHA-1: 160 bits
- SHA-2
- SHA-224 224 bits
- SHA-256 256 bits
- SHA-384 384 bits
- SHA-512 512 bits
- Whirlpool
- SHA-3
- SM3
MAC: Message Authentication Code 用来确保消息完整性
HMAC: Hash-based MAC
TLS 1.3之前是MAC, then encrypt
1. plaintext先通过Hash function运算,结果再加上key,生成MAC
2. plaintext再加上MAC进行Encryption,生成Ciphertext
TLS 1.3之后通信双方可以自己定义是MAC then encypt,还是encrypt then MAC
SHA256 + RSA加密,最常见。
数字签名和指纹区别:
指纹只是校验用的,数字签名才是防黑客篡改。
查看证书: 浏览器,命令行
openssl x509 -text -noout -in amazon.cer
CIA原则:
Confidentiality: 保密 (可以通过加密,权限管理和敏感信息不被暴露来实现)
Integrity: 数据内容完整,没有被篡改 (可以通过数字签名校验来实现)
Availability: 不让人家无限制调用你的服务
微软提出的STRIDE模型:
欺骗(Spoofing)
篡改(Tampering):也就是资料修改
否认(Repudiation)
资讯泄露(Information disclosure),可能是私隐泄露或是资料外泄
阻断服务攻击(Denial of service)
特权提升(Elevation of privilege):例如黑客把自己的权限提高
实战原则:
- 白名单和黑名单:白名单更严格
- 最小权限原则: 人家需要什么权限就给他什么权限,不要图省事给Admin原则
- 纵深防御:各个层面都需要考虑安全 (网络层,数据库层,操作系统层)
- 数据和代码分离原则: (注入攻击,缓存区溢出) 不要把用户输入作为命令的一部分。
- 不可预测性。在设计代码的时候,ID最好是随机变化。
DoS: 拒绝服务 (例如大量的攻击,持续发送SYN包)
用防护墙可以防止网络层攻击。
如何防止应用层DoS攻击?核心是对资源进行限制。不然黑客就会滥用服务。
1. 负载均衡(至少不会攻击同一台服务器。)
2. 限流
3. 缓存(在缓存区就把请求给处理了)
DDoS: Distributed DoS (攻击来自不同IP)
随机数: 用作蜜钥
不能用日期或时间做seed
不用用简单的rand()函数。JAVA里面有security包,用其它时间比如用户鼠标点击数作为seed。
Linux里面的/dev/random 和 /dev/urandom比较安全一些。
通过增大随机数空间或者组合随机数可以增加安全。
黑客攻防:
- 文件上传攻击: 黑客把一个包含木马程序的文件直接上传。可以通过限制文件后缀类型(比用MIME格式限制好)和文件大小来防止。
如果黑客把木马程序直接写到文件内容里面,可以通过压缩或resize文件来破坏可能包含的HTML代码。
另外一些好办法:
a)把接受上传的文件服务器单独分开。把文件服务器和应用服务器分开。
b)把存放文件的目录权限设为只读,不给它执行权限。
c)随机数改写文件名和文件路径,让黑客找不到上传的文件。
d)如果网站不需要文件上传功能,就关闭文件上传功能。 - 文件包含漏洞: 文件包含其他文件,
不推荐include *,不要用通配符。这样如果有文件上传漏洞,恶意代码就会被包含进去。
远程文件包含功能,不用的话,关闭。
#############################
SSL/TLS 学习小结
#############################
SSL/TLS 的目的有3个:
- Confidentiality - Data is only accessible by Client and Server. 通过加密encription来实现
- Integrity - Data is not modified between Client and Server. 通过Hashing来实现
- Authentication - Client/Server are indeed who they say they are. 通过PKI(public key infrastructure)来实现
// Replay: 在Client和Server之间的第三者把截取的消息发送多次。
Anti-Replay:
1) Provided with build-in sequence numbers.
2) Built in to Integrity + Authentication mechanism.
// Repudiation: dishonest sender.
Non-Repudiation:
1) Sender cannot later deny sending a message
2) Byproduct of Integrity + Authentication
If the message is protected by Integrity and Authentication, then we know no one is modifying this message.
SSL/TLS ecosystem involves three key players:
- Client:
- Entity initiating the TLS handshake
- Web Browser
- Phone, Apps, Smart Toaster, Internet of Things
- Optionally authenticated (rare)
- Server:
- Entity receiving the TLS handshake
- Web Server
- Apache, IIS, NginX, etc…
- Load Balancer or SSL Accelerator
- Always authenticatied
- Certification Authority (CA)
- Governing Entity that issues Certificates
- Trusted by Client and Server
- Provides Trust Anchor
- If we trust the CA, we trust what the CA trusts
- Five organizations secure 98% of the Internet (IdenTrust, DigiCert, Sectigo, GoDaddy, GlobalSign)
Hashing 算法四原则:
- Infeasible to produce a given digest
- Impossible to extract original message
- Slight changes produce drastic differences
- Resulting digest is fixed width (length)
Sender光Hashing Message还不能保证Message不会被中间者篡改,因为中间者可以篡改Message并加上自己的Hashing值。
如果Sender Hashing (Secret Key+Message),可以保证Message不会被中间者篡改,因为中间者得不到Secret Key。
如果Receiver 收到后验证Hash值正确,说明:
- 消息没有被篡改 //Integrity
- Sender有着同一个Secret Key //Authentication
MAC: Message Authentication Code
- Concept combining Message + Secret Key when calculating digest
- Provides Integrity and Authentication for Bulk data transfer
HMAC: Hash Based Message Authentication Code (MAC的工业标准实现) - RFC 2104
- 描述如何Combine Message 和 Key。Sender 和 Receiver都必须遵循同样的Combination,不然Hashing值还是不match。
Data Integrity:
- Hashing Algorithm
- INPUT: Message
- OUTPUT: Digest
- Example: MD5, SHA1, etc…
- MAC - Message Authentication Code
- INPUT: Message + Secret Key
- OUTPUT: Digest
- Example: HMAC (Hash Based Message Authentication Code)
Encryption:
- 简单的加密Message is not scalable。因为加密方法一样,如果接收到的加密结果一样,接收方1可以知道接收方2的Message。
- Key Based Encryption 针对每个接收方生成一个不同的Secret Key, allows encryption to scale to the whole Internet.
- Combines industry vetted algorithm with a Secret Key
- Algorithm is created by experts
- Secret Keys can be randomly generated
- Combines industry vetted algorithm with a Secret Key
- Two types of Key Based Encryption
-
Symmetric Encryption - Ideal for Bulk Data
- Encrypt and Decrypt using the same keys
- Strength: Faster - Lower CPU Cost
- Strength: Cipher text is the same size as Plain Text
- Weakness: Secret key must be shared - Less Secure
- Examples:
- DES 56 bit key
- RC4 128 bit key
- 3DES 168 bit key
- AES 128, 192, or 256 bit keys
- ChaCha20 128 or 256 bit keys
-
Asymmetric Encryption - Restricted to Limited Data
- Encrypt and Decrypt using different keys
- Two different keys are mathematically related
- What one key Encrypts, only the other can Decrypts
– One key will be made Public
– Other key will be kept Private - Weakness: Slower - Requires much larger key sizes
- Weakness: Cipher text expansion
- Strength: Private Key is never shared - More Secure
- Examples:
- DSA
- RSA - Recommended Key Size 2048 bits
- Diffie-Hellman
- ECDSA
- ECDH
-