[컴] Windows 용 Docker 설치

docker for windows


Windows 에서 docker 설치

Installation

위 경로에서 download 를 하면 된다.

prerequisite 전제조건

  • Microsoft Hyper-V 가 실행돼야 한다.: The Docker for Windows installer 가 enable 해준다.
  • 옛날 windows 의 docker toolbox는 VM (Virtual Box)을 사용했었는데, 이제는 VM 을 사용하지 않는다. : Docker for Mac vs. Docker Toolbox  를 읽어보자.

System Requirements

아래 조건을 만족해야 한다. 만족하지 않아도 docker toolbox 를 사용할 수는 있다.

  • Windows 10 64bit: Pro, Enterprise or Education (1607 Anniversary Update, Build 14393 or later).
  • BIOS 에서 Virtualization 을 켜야 한다.(기본적으로 켜져있다.)
  • CPU SLAT-capable feature.
  • At least 4GB of RAM.

설치마법사







설치 확인

service 를 실행하면 아래처럼 process 를 확인할 수 있다.


그리고 cmd 를 열어서 아래처럼 "docker run helloworld"를 쳐보면 된다.


Image Path

Settings > Advanced > Disk Image Location 에 가면 docker image 의 path 를 확인할 수 있다.

See Also

  1. 쿠...sal: [컴] Docker 설치 후 VMbox 가 안되는 경우

References

  1. Install Docker for Windows | Docker Documentation


[컴] MIT open courses

mooc / mit open courses / mit 오픈 코스들 / 컴퓨터 사이언스 코스 / 동영상 강의

MIT Open courses














See Also

  1. Stanford C programming
  2. 500 Free Computer Science Courses from the World’s Top CS Universities
  3. 150+ Stanford On-Campus Computer Science Courses Available Online : 스탠포드 대학 강의들을 주제별로 , 코스별로 묶어놨다.
  4. 쿠...sal: [컴][자료] Computer Science 관련 학습 자료

References

  1. MIT OCW : OCW Course Index | MIT OpenCourseWare | Free Online Course Materials

[컴] from php openssl to pycrypto (CTR mode, CBC mode)

php openssal to java abs cbc encrypt /

CTR mode from php openssl to pycrypto

php openssl ctr mode to python pycripto

To convert the php AES.CTR encryption to python AES.CTR encryption

php openssl code

$cipher = 'aes-256-ctr';

if (in_array($cipher, openssl_get_cipher_methods()))
{
    // Usually the below code is used for $iv
    // $ivlen = openssl_cipher_iv_length($cipher);
    // $iv = openssl_random_pseudo_bytes($ivlen);

    $key = 'AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA';

    // to start Counter from 1
    $iv0 = [00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,01];
    $iv = implode(array_map("chr", $iv0));

  
    $plaintext = 'test.test';
    $ciphertext = openssl_encrypt($plaintext, $cipher, $key, OPENSSL_RAW_DATA, $iv);
    $encodedCT = base64_encode($ciphertext); // $encodedCT => 'izQmJQ8nkPMP'
    
    //store $cipher, $iv, and $tag for decryption later    
    $original_plaintext = openssl_decrypt($encodedCT, $cipher, $key, 0, $iv);
    $original_plaintext2 = openssl_decrypt($ciphertext , $cipher, $key, OPENSSL_RAW_DATA, $iv);
}

python pycripto code


from Crypto.Cipher import AES
from Crypto.Util import Counter

import base64


def encrypt_token_ctr(data):
    key = b'AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA'
    iv = 1

    # to convert string 'iv' to int value to use for initial_value
    # iv = b'AAAAAAAAAAAAAAAA'
    # import binascii
    # hexval = binascii.hexlify(iv)
    # iv = int(hexval, 16)

    aes = AES.new(key=key, 
        mode=AES.MODE_CTR, 
        counter=Counter.new(128, initial_value=iv)
    )

    return aes.encrypt(data.encode('utf8'))

def decrypt_token_ctr(data):
    key = b'AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA'
    iv = 1

    aes = AES.new(key=key, 
        mode=AES.MODE_CTR, 
        counter=Counter.new(128, initial_value=iv)
    )

    return aes.decrypt(data)

print('Python encrypt: ' + str(base64.b64encode(encrypt_token_ctr('test.test'))) )
print('Python encrypt: ' + str(decrypt_token_ctr(base64.b64decode('izQmJQ8nkPMP'))) )


>> Python encrypt2: b'mzwsKw88h+c='
>> Python decrypt2: b'test.test'



AES CBC mode

php code


class AES
{
    var $key = 'AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA';
    var $iv = 'AAAAAAAAAAAAAAAA';
 
    function encryptToken($data)
    {
        // Mcrypt library has been DEPRECATED since PHP 7.1, use openssl:
        // return openssl_encrypt($data, 'aes-256-cbc', $this->key, OPENSSL_RAW_DATA, $this->iv);

        $message_padded = $data;
        if (strlen($message_padded) % 8) {
            $message_padded = str_pad($message_padded,
                strlen($message_padded) + 8 - strlen($message_padded) % 8, "\0");
        }

        // $padding = 16 - (strlen($data) % 16);
        // $data .= str_repeat(chr($padding), $padding);
        return openssl_encrypt($message_padded, 'aes-256-cbc', $this->key, OPENSSL_RAW_DATA, $this->iv);
    }
 
    function decryptToken($data)
    {
        // Mcrypt library has been DEPRECATED since PHP 7.1, use openssl:
        // return openssl_decrypt(base64_decode($data), 'aes-256-cbc', $this->key, OPENSSL_RAW_DATA, $this->iv);
        $data = openssl_decrypt(base64_decode($data), 'aes-256-cbc', $this->key, OPENSSL_RAW_DATA, $this->iv);
        $padding = ord($data[strlen($data) - 1]);
        return substr($data, 0, -$padding);
    }
}

python code


from Crypto.Cipher import AES

def _pad(s): return (s + (AES.block_size - len(s) % AES.block_size) * chr(AES.block_size - len(s) % AES.block_size)).encode('utf8')

def _cipher():
    key = b'AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA'
    iv = b'AAAAAAAAAAAAAAAA'
    return AES.new(key=key, mode=AES.MODE_CBC, IV=iv)


def encrypt_token(data):
    return _cipher().encrypt(_pad(data))

if __name__ == '__main__':
    print('Python encrypt: ' + str(base64.b64encode(encrypt_token('test.test'))) )
    print('Python decrypt: ' + str(decrypt_token(base64.b64decode('1pRLMUTqMCdBM4EyNyBTuQ=='))) )



See Also

  1. Encrypt and decrypt content with Nodejs · Christoph Hartmann
  2. AES/CBC/PKCS5Padding encrypt/decrypt PHP and JAVA example classes : java code <--> php code





[컴][알고리즘] AES 암호화 설명






  •  key expansion 은  key 를 4-byte 씩 쪼개어서 array 에 담는것
  • 16-byte 씩 작업을 하게 된다. 4 x 4 의 matrix 이다. 이 하나의 matrix을 state 라 부른다. 아래그림 참조
  • 각 정해진 bit 마다 round 가 정해져 있다.(128bit 은 10round, 256bit 는 14 round)
    • 각 bit 의 암호마다 "key-length/block-size/rounds" 수가 정해져 있다.
  • 이 (round-1)수 만큼 loop 을 돈다. 그리고 마지막 round 만 따로 한다.
  • 마지막 round 는 mix-columns 작업을 하지 않는다.

AES encryption process

1. SubBytes

sub 가 substitute 라고 보면 된다. State 에 있는 각 위치의 byte 에 해당하는 값을 "특정 table" 의 값과 교환하게 된다. 이 table 은 아래처럼 정해져있다.

여기 를 보면 어떻게 SubBytes 를 하는지 알 수 있다.

S = [ 
0x63, 0x7c, 0x77, 0x7b, 0xf2, 0x6b, 0x6f, 0xc5, 0x30, 0x01,
0x67, 0x2b, 0xfe, 0xd7, 0xab, 0x76, 0xca, 0x82, 0xc9, 0x7d,
0xfa, 0x59, 0x47, 0xf0, 0xad, 0xd4, 0xa2, 0xaf, 0x9c, 0xa4,
0x72, 0xc0, 0xb7, 0xfd, 0x93, 0x26, 0x36, 0x3f, 0xf7, 0xcc,
0x34, 0xa5, 0xe5, 0xf1, 0x71, 0xd8, 0x31, 0x15, 0x04, 0xc7,
0x23, 0xc3, 0x18, 0x96, 0x05, 0x9a, 0x07, 0x12, 0x80, 0xe2,
0xeb, 0x27, 0xb2, 0x75, 0x09, 0x83, 0x2c, 0x1a, 0x1b, 0x6e,
0x5a, 0xa0, 0x52, 0x3b, 0xd6, 0xb3, 0x29, 0xe3, 0x2f, 0x84,
0x53, 0xd1, 0x00, 0xed, 0x20, 0xfc, 0xb1, 0x5b, 0x6a, 0xcb,
0xbe, 0x39, 0x4a, 0x4c, 0x58, 0xcf, 0xd0, 0xef, 0xaa, 0xfb,
0x43, 0x4d, 0x33, 0x85, 0x45, 0xf9, 0x02, 0x7f, 0x50, 0x3c,
0x9f, 0xa8, 0x51, 0xa3, 0x40, 0x8f, 0x92, 0x9d, 0x38, 0xf5,
0xbc, 0xb6, 0xda, 0x21, 0x10, 0xff, 0xf3, 0xd2, 0xcd, 0x0c,
0x13, 0xec, 0x5f, 0x97, 0x44, 0x17, 0xc4, 0xa7, 0x7e, 0x3d,
0x64, 0x5d, 0x19, 0x73, 0x60, 0x81, 0x4f, 0xdc, 0x22, 0x2a,
0x90, 0x88, 0x46, 0xee, 0xb8, 0x14, 0xde, 0x5e, 0x0b, 0xdb,
0xe0, 0x32, 0x3a, 0x0a, 0x49, 0x06, 0x24, 0x5c, 0xc2, 0xd3,
0xac, 0x62, 0x91, 0x95, 0xe4, 0x79, 0xe7, 0xc8, 0x37, 0x6d,
0x8d, 0xd5, 0x4e, 0xa9, 0x6c, 0x56, 0xf4, 0xea, 0x65, 0x7a,
0xae, 0x08, 0xba, 0x78, 0x25, 0x2e, 0x1c, 0xa6, 0xb4, 0xc6,
0xe8, 0xdd, 0x74, 0x1f, 0x4b, 0xbd, 0x8b, 0x8a, 0x70, 0x3e,
0xb5, 0x66, 0x48, 0x03, 0xf6, 0x0e, 0x61, 0x35, 0x57, 0xb9,
0x86, 0xc1, 0x1d, 0x9e, 0xe1, 0xf8, 0x98, 0x11, 0x69, 0xd9,
0x8e, 0x94, 0x9b, 0x1e, 0x87, 0xe9, 0xce, 0x55, 0x28, 0xdf,
0x8c, 0xa1, 0x89, 0x0d, 0xbf, 0xe6, 0x42, 0x68, 0x41, 0x99,
0x2d, 0x0f, 0xb0, 0x54, 0xbb, 0x16 ]
from: https://www.youtube.com/watch?v=gP4PqVGudtg

2. Shift Rows

아래처럼 각 row 를 shift 한다. 0번째 열은 '0'번, 1번째 열은 '1'번 ...
from: https://www.youtube.com/watch?v=2y_tidbY-Lw

3. MixColumns

여기 를 보면 animation 을 볼 수 있다.

state 의 각 column 을 Galois field 와 곱하기(matrix 곱하기) 를 하게 된다. 만약 GF(255) 라면 0~255 사이의 숫자로만 구성된 field(matrix) 가 만들어진다.



4. Add Round Key

"State 의 각 byte" 와 "Round key 의 각 byte" 을 XOR 한다.
from: https://www.youtube.com/watch?v=2y_tidbY-Lw



CTR mode 사용시 유의할 점

  • PHP data encryption primer > Encryption algorithm / mode of operation / nonce (initializing vector)
    • single key 를 사용하는 경우는 nonce 가 unique 해야만 안전함을 보장한다.
    • ...



[컴][OS] Cent OS 7 / apache / php7.2 설치




Download Minimal CentOS

Network configuration

install 시에 설정을 못했다면 if 파일을 수정하면 된다. 

vi /etc/sysconfig/network-scripts/ifcfg-enp0s3

ONBOOT=yes


repository 설정


rpm -Uvh http://rpms.famillecollet.com/enterprise/remi-release-7.rpm

apache 설치

yum --enablerepo=remi,remi-php72 install httpd php php-common
php72 를 위해서는 remi 를 빼고 remi-php72 만 사용하자.

php 설치

yum --enablerepo=remi-php72 install php
위 link 를 참고해서 아래에서 필요한 부분만 골라서 설치하면 된다.
yum --enablerepo=remi,remi-php72 install php-pecl-apcu php-cli php-pear php-pdo php-pecl-memcache php-pecl-memcached php-gd php-mbstring php-mcrypt php-xml php-mysqlnd php-pgsql php-pecl-mongodb

php.ini

기본적으로 path 는 /etc/php.ini 로 되어 있다. php compile 시에 configure option 에 따라 기본 path 는 변경될 수 있다.(참고)
'--with-config-file-path=/etc/httpd/conf'

test.php

<?php
    phpinfo();
위 처럼 test.php 를 만들고 '/var/www/html/test.php' 에 놓은 후 접속하면 관련 설정 내역을 확인할 수 있다.

참고, apache 의 php 모듈 설정

  • apache config file path : /etc/httpd/conf/httpd.conf
httpd.conf 에 보면 LoadModule 부분이 아래처럼 되어 있다.(apache 2.4.6 기준)
Include conf.modules.d/*.conf 
그래서 php module 의 load 가 15-php.conf 안에 들어있다.(이것은 아마도 yum install 시에 자동으로 넣어주지 않을까 생각된다. 확인필요)
  • etc/httpd/conf/conf.modules.d/15-php.conf
  • /usr/lib64/httpd/modules/libphp7.so
여하튼, 그래서 추가설정 없이 바로 apache 에서 php 를 사용할 수 있다.



Dev. Enviroment

X Window System

아래처럼 하면 설치된다.
  1. yum groupinstall "X Window System" "Fonts"
  2. yum install gnome-classic-session gnome-terminal nautilus-open-terminal control-center liberation-mono-fonts
  3. unlink /etc/systemd/system/default.target
  4. ln -sf /lib/systemd/system/graphical.target /etc/systemd/system/default.target
  5. reboot

Samba client / mount

yum install samba-client
smbclient -L 10.0.2.2 -U user_name -m SMB2
자세한 건 아래 링크를 참조하자.

ifconfig

ip addr






[컴][웹] 서브도메인 설정 방법

subdomain / how to set sub domain


서브 도메인 설정 방법

A record 추가

자신이 사용하는 DNS 로 가서 A record 를 추가한다. 아래의 newtest.testdomain.com

A newtest.testdomain.com 124.138.223.234
A testdomain.com         124.138.223.234

cloudflare

cloudflare 의 DNS 서버를 사용하는 경우 주의할 점은 "Status" 부분이다. 기본적으로 grey cloud 나 not use 로 해주자. 그렇지 않으면 cloudflare 에 의해 엉뚱한 ip address 에 묶여 있다.

Web server 설정

자신이 사용하는 web server 에서 설정을 해준다.

만약 apache 를 사용한다면 vhost.conf 등에서 아래처럼 새로운 ServerName 을 이용해서 <Virtualhost> 를 추가 하면 된다.
Listen 8080
<Virtualhost *:8080>
    ServerAdmin webmaster@localhost
    DocumentRoot /home/public
    ServerName newtest.testdomain.com   
    ... 


방화벽 설정

당연한 이야기지만 방화벽이 걸려있으면 같이 열어줘야 한다.

[컴][웹] Apache, CentOS 7 에서 Let's Encrypt 인증서 설치


Apache, CentOS 7 에서 Certbot 설치

Certbot 이 EPEL (Extra Packages for Enterprise Linux). 형태로 제공된다.

설치

혹시 repository 설정이 안되어 있다면, the EPEL repository 를 설정해야 한다. 보통 기본으로 되어 있긴 한다.(참고)
  • sudo yum install python2-certbot-apache

실행

  • sudo certbot --apache
    • certificate 을 가져오게 된다.
    • cerbot 이 가져온 certificate 에 대해서 apache configuration 을 수정한다.
  • sudo certbot --apache certonly
    • certificate 만 가져온다.
    • 직접 apache configuration 을 수정해야 한다.
  • sudo certbot certonly
    • 서버의 지정없이 certificate 을 가져올 수 있다.
Let's Encrypt's new ACMEv2 server 에서 a wildcard certificate 를 가져오려고 한다면, Certbot's DNS plugins 를 사용해야 한다.
아래는 cloudflare 용 dns plugin (cerbot-dns-cloudflare ) 를 사용하는 command 이다.
  • sudo certbot -a certbot-dns-cloudflare -i apache -d "*.example.com" -d example.com --server https://acme-v02.api.letsencrypt.org/directory
  • 서버 지정없이(아래참고)
    sudo certbot certonly --manual -d "*.example.com" -d example.com  --preferred-challenges dns-01 --server https://acme-v02.api.letsencrypt.org/directory

server 지정없이 하는 방법(certbot certonly)

아래 링크에 자세한 방법이 나와 있다.

server 지정없이 한 경우 결과화면

IMPORTANT NOTES:

 - Congratulations! Your certificate and chain have been saved at:
   /etc/letsencrypt/live/certbot renew/fullchain.pem
   Your key file has been saved at:
   /etc/letsencrypt/live/certbot renew/privkey.pem
   Your cert will expire on 2010-02-01. To obtain a new or tweaked
   version of this certificate in the future, simply run certbot
   again. To non-interactively renew *all* of your certificates, run
   "certbot renew"
 - Your account credentials have been saved in your Certbot
   configuration directory at /etc/letsencrypt. You should make a
   secure backup of this folder now. This configuration directory will
   also contain certificates and private keys obtained by Certbot so
   making regular backups of this folder is ideal.
 - If you like Certbot, please consider supporting our work by:


   Donating to ISRG / Let's Encrypt:   https://letsencrypt.org/donate
   Donating to EFF:                    https://eff.org/donate-le


apache vhost 설정

SSLCipherSuite 는 Cipher Suites and Enforcing Strong Security 를 참고하자.
 
아래 설정에서 보면 알겠지만, 3개의 file 이 필요하다.
  1. 인증서
  2. 인증서를 만들때 쓰인 privatekey
  3. 인증서Chain
 
<VirtualHost *:443>
    DocumentRoot /home/myserverdomain/public
    ServerName dd.myserverdomain.com
    ServerAlias dd.myserverdomain.com

    SSLCipherSuite          ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-AES256-GCM-SHA384:DHE-RSA-AES128-GCM-SHA256:DHE-DSS-AES128-GCM-SHA256:kEDH+AESGCM:ECDHE-RSA-AES128-SHA256:ECDHE-ECDSA-AES128-SHA256:ECDHE-RSA-AES128-SHA:ECDHE-ECDSA-AES128-SHA:ECDHE-RSA-AES256-SHA384:ECDHE-ECDSA-AES256-SHA384:ECDHE-RSA-AES256-SHA:ECDHE-ECDSA-AES256-SHA:DHE-RSA-AES128-SHA256:DHE-RSA-AES128-SHA:DHE-DSS-AES128-SHA256:DHE-RSA-AES256-SHA256:DHE-DSS-AES256-SHA:DHE-RSA-AES256-SHA:AES128-GCM-SHA256:AES256-GCM-SHA384:AES128-SHA256:AES256-SHA256:AES128-SHA:AES256-SHA:AES:CAMELLIA:DES-CBC3-SHA:!aNULL:!eNULL:!EXPORT:!DES:!RC4:!MD5:!PSK:!aECDH:!EDH-DSS-DES-CBC3-SHA:!EDH-RSA-DES-CBC3-SHA:!KRB5-DES-CBC3-SHA
    SSLHonorCipherOrder     on
    SSLOptions +StrictRequire

    SSLEngine on
    SSLCertificateFile "/etc/letsencrypt/live/myserverdomain.com/cert.pem"
    SSLCertificateKeyFile "/etc/letsencrypt/live/myserverdomain.com/privkey.pem"
    SSLCertificateChainFile "/etc/letsencrypt/live/myserverdomain.com/fullchain.pem"
    ...

인증서 갱신

cerbot renew 이용

인증서를 갱신(renew) 하는 방법은 아래와 같다. 이것을 주기적으로 하고 싶다면 cron 이나 systemd timer 에 넣어놓으면 된다. 보통 하루에 2번 수행하는 것을 추천한다. renew 시점이 아니면 아무일도 하지 않아서 계속 돌려도 문제는 없다.[ref. 1]
  • sudo certbot renew --dry-run

cerbot renew 가 안되는 경우

위의 방법을 사용하면, 에러가 발생한다. 아래에서 답을 찾았다.
방법은, 일단 다시 아래처럼 신청하면 새롭게 만들어진다. 물론 DNS 확인도 해야 한다.
  • certbot certonly --manual -d '*.mydomain.com'
인증서를 변경하고 나서는 apache 를 restart 해야 한다. reload 하는 방법도 있는듯 한데 일단 그 부분은 확인이 필요하다.

[root@myserver bin]# certbot certonly --manual -d '*.mydomain.com'
Saving debug log to /var/log/letsencrypt/letsencrypt.log
Plugins selected: Authenticator manual, Installer None
Starting new HTTPS connection (1): acme-v02.api.letsencrypt.org
Obtaining a new certificate
Performing the following challenges:
dns-01 challenge for mydomain.com

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
NOTE: The IP of this machine will be publicly logged as having requested this
certificate. If you're running certbot in manual mode on a machine that is not
your server, please ensure you're okay with that.

Are you OK with your IP being logged?
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
(Y)es/(N)o: Y

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Please deploy a DNS TXT record under the name
_acme-challenge.mydomain.com with the following value:

Rfdklsfjkldsehtdfdsfdsgdgaga4

Before continuing, verify the record is deployed.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Press Enter to Continue
Waiting for verification...
Cleaning up challenges

IMPORTANT NOTES:
 - Congratulations! Your certificate and chain have been saved at:
   /etc/letsencrypt/live/mydomain.com-0001/fullchain.pem
   Your key file has been saved at:
   /etc/letsencrypt/live/mydomain.com-0001/privkey.pem
   Your cert will expire on 2019-04-21. To obtain a new or tweaked
   version of this certificate in the future, simply run certbot
   again. To non-interactively renew *all* of your certificates, run
   "certbot renew"
 - If you like Certbot, please consider supporting our work by:

   Donating to ISRG / Let's Encrypt:   https://letsencrypt.org/donate
   Donating to EFF:                    https://eff.org/donate-le

인증서 위치

기존에 아래 path 에 인증서가 있는 경우,
  • /etc/letsencrypt/live/mydomain.com
새롭게 만들어지는 것은 아래 path 에 추가로 만들어진다.
  • /etc/letsencrypt/live/mydomain.com-0001
참고로, /etc/letsencrypt/live/mydomain.com 에 있는 것은 link 이다. 실제 file 들은 아래경로에 있다.
  • /etc/letsencrypt/archive/mydomain.com

인증서 삭제

certbot delete --cert-name mydomain.com


문제

httpd: Syntax error on line 23 of /etc/httpd/conf/httpd.conf: Cannot load modules/mod_mpm_event.so into server
httpd 가 yum 에 의해 설치되지 않아서 일까 위의 문제가 발생했다. 그래서 Cerbot 없이 설치하는 법을 택했다.

Cerbot 없이

See Also

  1. DNS 의 TXT 확인 방법

References

  1. Certbot | Apache on CentOS/RHEL 7