2019년 3월 3일 일요일

Node.js 보안 및 Https 서버 구성

Node.js Https 서버 구성


1. 암호화 해시 알고리즘


암호화 해시 알고리즘은 다음과 같다.

  • MD5는 보안용도로 권장하지 않음
  • SHA(secure hash algorithm)  사용


md5 - q 파일 이름 => md5 해쉬값을 비교가능 (파일 다운로드 변조 확인)

Hash값에 Salt (임의 문자를 추가)해서 해킹에 대한 방지

대칭 알고리즘의 종류는 다음과 같다.

  • DES (Data Encryption Standard)는 보안에 취약
  • AES(Advanced Encryption Standard)로 대체
대칭 암호화 : crypto. cipher/Decipher 사용


비대칭 알고리즘은 개인키와 공개키를 사용하여 비대칭으로 암호화 한다.


사설 인증서는 openssl이 있고 사용 시 경고가 발생한다.
openssl을 설치한다. 다음 명령을 이용하여 인증서를 발급한다.

  • openssl genrsa -out key.pem 2048 (Private Key 생성)
  • openssl req -new -key key.pem -out req.csr (인증서 발급 요청)
  • openssl x509 -req -in req.csr -signkey key.pem -out cert.pem -days 365 (인증서 발급 승인) 

두번째 명령어 실행 중 openssl.cnf파일을 찾을 수 없다는 오류가 발생하면 해당 파일의 위치를 명령어에 지정해야 한다.
C:\Users\jungwonkun7\내 프로그램\openssl-0.9.8k_WIN32\bin>openssl req -config ..
/openssl.cnf -new -key key.pem -out req.csr
Loading 'screen' into random state - done
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Country Name (2 letter code) [AU]:KR
State or Province Name (full name) [Some-State]:SEOUL
Locality Name (eg, city) []:Seoul
Organization Name (eg, company) [Internet Widgits Pty Ltd]:Stormboy
Organizational Unit Name (eg, section) []:
Common Name (eg, YOUR name) []:WK
Email Address []:stormboy@hanmail.net

Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []:
An optional company name []:



영리 목적으로 하는 개인 정보 수집 서비스의 경우는 보안 서버(https)가 의무화 되어 있다.
무료로 인증을 제공하는 사이트는 다음과 같다.

  • https://www.startssl.com
  • https://letsencrypt.org

2. https 서버 프로그램 작성

SSL 인증서와 함께 다음 코드를 작성한 후 node로 실행한다.

  • let http=require('http'),
  •     https = require('https'),
  •     express = require('express'),
  •     fs = require('fs');

  • let options = {
  •     key: fs.readFileSync('./key.pem'),
  •     cert: fs.readFileSync('./cert.pem')
  • };


  • let port1 = 80;
  • let port2 = 443;

  • let app = express();
  • app.use(express.urlencoded({extended: false}));

  • http.createServer(app).listen(port1, function(){
  •   console.log("Http server listening on port " + port1);
  • });


  • https.createServer(options, app).listen(port2, function(){
  •   console.log("Https server listening on port " + port2);
  • });

  • app.get('/', function (req, res) {
  •     res.writeHead(200, {'Content-Type' : 'text/html'});
  •     res.write('<h3>Welcome</h3>');
  •     res.write('<a href="/login">Please login</a>');
  •     res.end();
  • });

  • app.get('/login', function (req, res){
  •     res.writeHead(200, {'Content-Type': 'text/html'});
  •     res.write('<h3>Login</h3>');
  •     res.write('<form method="POST" action="/login">');
  •     res.write('<label name="userId">UserId : </label>')
  •     res.write('<input type="text" name="userId"><br/>');
  •     res.write('<label name="password">Password : </label>')
  •     res.write('<input type="password" name="password"><br/>');
  •     res.write('<input type="submit" name="login" value="Login">');
  •     res.write('</form>');
  •     res.end();
  • })

  • app.post('/login', function (req, res){
  •     let userId = req.params("userId");
  •     let password = req.params("password")

  •     res.writeHead(200, {'Content-Type': 'text/html'});
  •     res.write('Thank you, '+userId+', you are now logged in.');
  •     res.write('<p><a href="/"> back home</a>');
  •     res.end();
  • });
https://localhost/login 과 http://localhost/login을 접속하여 각각 실행되는 결과를 확인한다.

[참고 사이트]

https://www.youtube.com/watch?v=gks3CbpgtmI (Node 보안 강의)
http://blog.saltfactory.net/implements-nodejs-based-https-server/ (openssl과 node.js로 https서버 만들기)
https://code.google.com/archive/p/openssl-for-windows/downloads (window용 openssl설치파일)

댓글 없음:

댓글 쓰기