セキュリティ
暗号・ハッシュ・パスワード
gpg と openssl での暗号化・署名・証明書の確認、base64、UUIDやパスワードの生成、安全な削除。
gpgファイルを暗号化する・署名する
ファイルの暗号化・復号や、署名の作成・確認を行います。
パスワードで暗号化する(鍵なし)
$ gpg -c secret.txt # secret.txt.gpg ができる(パスワードを聞かれる)
$ gpg -o secret.txt -d secret.txt.gpg # 復号
$ gpg -c --armor note.txt # テキスト形式(.asc)で出力鍵を使う
$ gpg --full-generate-key # 鍵を作る
$ gpg --list-keys # 公開鍵の一覧
$ gpg --list-secret-keys --keyid-format=long # 秘密鍵の一覧
$ gpg --armor --export [email protected] > pub.asc # 公開鍵を書き出す
$ gpg --import friend.asc # 相手の公開鍵を取り込む
$ gpg -e -r [email protected] file.pdf # 相手向けに暗号化
$ gpg -d file.pdf.gpg > file.pdf # 自分宛てを復号
$ gpg --detach-sign --armor release.tar.gz # 署名(release.tar.gz.asc)
$ gpg --verify release.tar.gz.asc release.tar.gz # 署名の確認openssl証明書・暗号・乱数
SSL/TLS 証明書を調べたり作ったり、暗号化や乱数の生成をしたりします。
サーバーの証明書を調べる
$ openssl s_client -connect example.com:443 -servername example.com </dev/null 2>/dev/null \
| openssl x509 -noout -subject -issuer -dates # 発行者と有効期限
$ echo | openssl s_client -connect example.com:443 2>/dev/null | openssl x509 -noout -enddate
$ openssl x509 -in cert.pem -noout -text # 手元の証明書の中身鍵と証明書を作る
$ openssl req -x509 -newkey rsa:4096 -nodes -keyout key.pem -out cert.pem -days 365 \
-subj "/CN=localhost" # 自己署名証明書(開発用)
$ openssl genpkey -algorithm ed25519 -out key.pem
$ openssl req -new -key key.pem -out req.csr -subj "/CN=example.com" # 署名リクエスト暗号化・ハッシュ・乱数
$ openssl enc -aes-256-cbc -pbkdf2 -salt -in file.txt -out file.enc # パスワードで暗号化
$ openssl enc -d -aes-256-cbc -pbkdf2 -in file.enc -out file.txt # 復号
$ openssl rand -hex 32 # ランダムな16進数(APIキーやシークレットに)
$ openssl rand -base64 24
$ openssl dgst -sha256 file.iso # ハッシュ
$ echo -n "message" | openssl dgst -sha256 -hmac "secret" # HMAC
$ openssl passwd -6 # /etc/shadow 形式のパスワードハッシュbase64Base64 に変換する
データを Base64 形式に変換したり、元に戻したりします。
$ echo -n "user:pass" | base64 # → dXNlcjpwYXNz
$ echo "dXNlcjpwYXNz" | base64 -d # 元に戻す
$ base64 -w0 image.png > image.b64 # 改行なしで1行にuuidgenUUID を作る
重ならない ID(UUID)を作ります。
$ uuidgen # ランダム(v4)
$ uuidgen --time # 時刻ベース(v1)
$ cat /proc/sys/kernel/random/uuid # uuidgen がなくても使えるapg覚えやすいパスワードを作る
Ubuntu には最初から入っていません。 で入れられます。
覚えやすいものも含めて、ランダムなパスワードを作ります。
$ apg -n 5 -m 16 # 16文字以上を5個
$ apg -a 1 -m 24 -n 3 # 記号も含むランダムな24文字pwgenランダムなパスワードを作る
Ubuntu には最初から入っていません。 で入れられます。
ランダムなパスワードを作ります。
$ pwgen -s 20 5 # 完全にランダムな20文字を5個パッケージを入れずに作るなら、openssl rand -base64 18 や tr -dc 'A-Za-z0-9' </dev/urandom | head -c 20; echo が使えます。
shred中身を上書きしてから消す
ファイルの中身を上書きしてから削除し、復元されにくくします。
$ shred -u -n 3 secret.txt # 3回上書きしてから削除
$ sudo shred -v -n 1 /dev/sdb # ディスク全体を上書き(注意)chattr / lsattr変更・削除できないようにする
ファイルを変更・削除できないように保護します。
$ sudo chattr +i important.conf # root でも変更・削除できなくする
$ lsattr important.conf
----i---------e------- important.conf
$ sudo chattr -i important.conf # 元に戻す
$ sudo chattr +a app.log # 追記だけ許可するgetfacl / setfacl細かいアクセス権(ACL)
chmod の「所有者・グループ・その他」では足りないとき、ユーザーごとに権限を付けられます。
$ setfacl -m u:alice:rw shared.txt # alice に読み書きを許可
$ setfacl -R -m g:dev:rwX project/ # dev グループに再帰的に
$ setfacl -d -m g:dev:rwX project/ # 今後作るファイルにも適用(既定ACL)
$ getfacl shared.txt
$ setfacl -b shared.txt # ACL をすべて外すssh-keyscanサーバーの公開鍵を取得する
サーバーの公開鍵を取得して、接続先として登録します。
$ ssh-keyscan github.com >> ~/.ssh/known_hosts
$ ssh-keyscan -t ed25519 example.com | ssh-keygen -lf - # 指紋を確認ufwかんたんファイアウォール
かんたんな操作でファイアウォールを設定します。
$ sudo ufw allow ssh
$ sudo ufw allow 443/tcp
$ sudo ufw enable
$ sudo ufw status numbered
$ sudo ufw delete 3