Skip to content

Commit

Permalink
Update(auto commit)
Browse files Browse the repository at this point in the history
  • Loading branch information
xta0 committed Aug 6, 2018
1 parent 82e2881 commit aa26ef0
Show file tree
Hide file tree
Showing 9 changed files with 363 additions and 50 deletions.
33 changes: 0 additions & 33 deletions _drafts/2009-06-10-Network-Protocols.md
@@ -1,34 +1 @@
## HTTPs

### HTTPs

- HTTPs:Hypertext Transfer Protocol over Secure Socket Layer.位于应用层的基于SSL/TLS的HTTP协议

- SSL/TLS
- 本身是带有加密信息的传输层协议
- SSL:为网络通信提供安全及数据完整性的安全协议
- TLS:基于SSL之上的通用协议,为SSL的继任者

- SSL加密方式
- HTTPs建立链接的步骤:

- Computers agree on how to encrypt
- C => S : 客户端向server发送一个hello包,提供几种加密方式:

- Key:RSA,Diffle-Hellman,DSA

- Cipher:AES, RC4, Triple DES

- Hash:HMAC-MD5

- S => C : 通知客户端,server选取了哪几种机密方式

- Server sends a certificate to client

- Client says 'start encrypting'

- Client Key Exchange : 双方计算master secret code, 这个secret code用来做后续的加密工作

- change cipher spec:让server选择上一个步骤中约定的加密方式

- The server says 'start encrypting'
20 changes: 15 additions & 5 deletions _drafts/nginx.md
@@ -1,7 +1,13 @@
---
layout: post
list_title: 使用NGINX (一) | NGINX Fundamentals Part 1
title: 使用NGINX
categoris: [UNIX, NGINX]
---

## Nginx Fundamentals

###About Nginx

- created in 2004
- High performance, High Concurrency, Low Memory
- webserver, reverse proxy
Expand All @@ -11,7 +17,6 @@
- Basic Architecture
- Apache多个进程,每个进程起一个处理一个请求,
- Nginx多个进程,每个进程可以实现并发处理多个请求,反向代理

- Resource Usage
- Apache每个进程都及时处理静态资源的请求也需要加载php等语言环境,有一定overhead的损耗
- Nginx对静态资源不需要加载语言环境
Expand All @@ -20,19 +25,24 @@
- Nginx使用URI定位资源
- Apache使用文件路径定位资源

### Install nginx from source code
### Install Nginx

- 使用Package Manger
- Check Nginx Status
- `ps au | grep nginx`

### 配置Nginx

- 两个名词
- `context`:`nginx.conf`中的section,类似scope:
- `context`
- `nginx.conf`中的section
```
events {
worker_connections 768;
# multi_accept on;
}
```
- `directive`:`ngix.conf`中的键值对,例如:`sendfile on;`
- `directive`
- `ngix.conf`中的键值对,例如:`sendfile on;`

- 配置Virtual Host
103 changes: 99 additions & 4 deletions _posts/2006-07-18-Backend-Auth.md
Expand Up @@ -6,7 +6,7 @@ list_title: 用户认证 | Backend Authentication
categories: [Network,Backend]
---

### Cookies
## Cookies

a small piece of data stored in the browser for a website, key-value paires, 20 cookies per website

Expand Down Expand Up @@ -54,18 +54,113 @@ Accept-Ranges: none
Vary: Accept-Encoding
```

- Cookie Domain
### Hash Cookies

上面日志中可以看出Cookie的格式为
常用的Hash函数有,CRC,MD5,Sha1,Sha256,其安全性由低到高,Hash的速度由快到慢。Python提供了一些常用的Hash API

```python
import hashlib
key = "udacity"
key = key.encode('utf-8')
x = hashlib.sha256(key)
x.hexdigest() #
```
set-cookie: name=steve; Domain = www.reddit.com; Path = /foo
可以使用Hash来校验Cookie,假设cookie的格式为`{visits | hashcode}`其中visits表示访问server的次数

```python
#server
#-----------
#set-cookie: {5|e4da3b7fbbce2345d7772b0674a318d5}

#client
#-----------
#cookie: {5|e4da3b7fbbce2345d7772b0674a318d5}

# check cookie
def hash_str(s):
return hashlib.md5(s.encode('utf-8')).hexdigest()

def check_secure_val(h):
val, hashstr = h.split('|')
if hashstr == hash_str(val):
return True
else:
return False
```

上述Hash Cookie的方法任然有一个缺陷,就是可以被伪造,例如可以将`{5|e4da3b7fbbce2345d7772b0674a318d5}`替换为`{123|202cb962ac59075b964b07152d234b70}`,校验仍然有效。

可以对上述方法做个修改,在计算hash时,加入一个secret key

```
Hash(secret_key,cookie) = hash_code
```

Python的hash库中提供HMAC(Hash-based Meesage Authentication Code)的API来应对上述场景

```python
secret_key = bytes([0x13, 0x00, 0x00, 0x00, 0x08, 0x00])
cookie = "some_cookie".encode('utf-8')
hash_code = hmac.new(secret_key,cookie).hexdigest()
#f2b280549c1c9edb18d5500d6c01ea51
```

### Password

Hash password的方法和cookie类似,对明文密码 + 一个随机数(salt)进行hash

```python
import random
import string

def make_salt():
seed = "1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!@#$%^&*()_+=-"
sa = []
salt=''
for i in range(5):
sa.append(random.choice(seed))
salt = ''.join(sa)
return salt

>>> print(make_salt())
vZMV1
```
上述代码可以生成5个字符的随机字符串, 用该字符串对密码进行加密,密码加密以及用户登录校验密码的逻辑如下

```python
def make_pwd_hash(name,pwd,salt=None):
if not salt:
salt = make_salt()
key = (name+pwd+salt).encode('utf-8')
hash_code = hashlib.sha256(key).hexdigest()
return f"{hash_code},{salt}"

#check user password
#hash_code comes from database
def valid_pw(name,pw,hash_code):
salt = h.split(',')[1]
return h == make_pwd_hash(name,pwd,salt)
```
在密码的加密算法上,sha256比较慢,可以选择使用bcrypt

## HTTPs

- HTTP over **Secure Socket Layer**, 位于应用层的基于SSL/TLS的HTTP协议
- SSL/TLS
- 本身是带有加密信息的传输层协议
- SSL:为网络通信提供安全及数据完整性的安全协议
- TLS:基于SSL之上的通用协议,为SSL的继任者
- SSL加密方式
- Computers agree on how to encrypt
- C => S : 客户端向server发送一个hello包,提供几种加密方式:
- Key:RSA,Diffle-Hellman,DSA
- Cipher:AES, RC4, Triple DES
- Hash:HMAC-MD5

- S => C : 通知客户端,server选取了哪几种机密方式

- Server sends a certificate to client
- Client says 'start encrypting'
- Client Key Exchange : 双方计算master secret code, 这个secret code用来做后续的加密工作
- change cipher spec:让server选择上一个步骤中约定的加密方式
- The server says 'start encrypting'

0 comments on commit aa26ef0

Please sign in to comment.