' OR '1'='1' --
' UNION SELECT null, null, user, password FROM users --
'; WAITFOR DELAY '00:00:10' --
Djangoでdjango-otpを使用して新たにQRコードを生成し、二要素認証を実装する方法について説明します。
1. 必要なパッケージのインストール
まず、以下のパッケージをインストールします。
bash
コピーする
編集する
pip install django-otp qrcode[pil]
django-otpはワンタイムパスワードのサポートを追加し、qrcodeはQRコードの生成に使用します。
2. Djangoプロジェクトの設定
settings.pyにdjango_otpとdjango_otp.plugins.otp_totpを追加します。
python
コピーする
編集する
INSTALLED_APPS = [
# 既存のアプリケーション
'django_otp',
'django_otp.plugins.otp_totp',
]
これにより、TOTP(Time-based One-Time Password)のサポートが有効になります。
3. モデルの作成
ユーザーごとにTOTPデバイスを管理するため、TOTPDeviceモデルを作成します。
python
コピーする
編集する
from django_otp.plugins.otp_totp.models import TOTPDevice
# ユーザーに関連付けられたTOTPデバイスを作成
device = TOTPDevice.objects.create(user=user)
TOTPDeviceは、ユーザーごとに一意のシークレットキーを生成し、QRコードの生成に使用されます。
4. QRコードの生成
qrcodeライブラリを使用して、生成されたシークレットキーからQRコードを作成します。
python
コピーする
編集する
import qrcode
from io import BytesIO
from base64 import b64encode
# デバイスの設定URLを取得
config_url = device.config_url
# QRコードを生成
qr = qrcode.QRCode()
qr.add_data(config_url)
qr.make(fit=True)
# 画像をメモリに保存
img = qr.make_image(fill='black', back_color='white')
buffer = BytesIO()
img.save(buffer, format='PNG')
buffer.seek(0)
# 画像をBase64でエンコード
img_str = b64encode(buffer.getvalue()).decode()
このコードにより、QRコードの画像データがBase64形式でエンコードされ、テンプレートで表示可能になります。
5. テンプレートでの表示
生成したQRコードをテンプレートで表示するには、以下のようにします。
html
コピーする
編集する
<img src="data:image/png;base64,{{ img_str }}" alt="QR Code">
これにより、ユーザーはQRコードをスキャンして認証アプリに追加できます。
6. 認証の検証
ユーザーが認証アプリで生成したトークンを入力した際に、それを検証する必要があります。
python
コピーする
編集する
# ユーザーのデバイスを取得
device = TOTPDevice.objects.get(user=user)
# トークンの検証
if device.verify_token(token):
# 認証成功
...
else:
# 認証失敗
...
verify_tokenメソッドは、ユーザーが入力したトークンが正しいかどうかを確認します。
参考資料
Django OTP TOTP - how to display QR code in template
django-otp 公式ドキュメント
Djangoで、二要素認証を、Google認証システムを使って実装。
pyotpでワンタイムパスワードを試してみる
Custom Two-factor with Qrcode in django
【Django】django-otpで多要素認証(二要素認証)を実現させる
$vi cmd __EOF__
#!/bin/bash
while IFS= read -r ip; do
if [[ -n "$ip" ]]; then
# whois コマンドで CustName を取得
cust_name=$(whois "$ip" | grep -i 'CustName' | awk -F: '{print $2}' | xargs)
# CustName が見つからない場合、OrgName を試す
if [[ -z "$cust_name" ]]; then
cust_name=$(whois "$ip" | grep -i 'OrgName' | awk -F: '{print $2}' | xargs)
fi
# CustName が見つからない場合、descr を試す
if [[ -z "$cust_name" ]]; then
cust_name=$(whois "$ip" | grep -i 'descr' | head -n 1 | awk -F: '{print $2}' | xargs)
fi
echo "$ip, $cust_name"
fi
done
__EOF__
chmod a+x cmd
cat /var/log/httpd/access_log* |grep "GET /office/cti"|cut -d" " -f 1|sort|uniq||.cmd
$ xinput --get-button-map 12
device has no buttons
[takahab@rocky92 ~]$ xinput list
⎡ Virtual core pointer id=2 [master pointer (3)]
⎜ ↳ Virtual core XTEST pointer id=4 [slave pointer (2)]
⎜ ↳ 2.4G Mouse Consumer Control id=9 [slave pointer (2)]
⎜ ↳ ELECOM IST TrackBall Mouse id=11 [slave pointer (2)]
⎜ ↳ 2.4G Mouse id=14 [slave pointer (2)]
⎜ ↳ ELECOM IST TrackBall Consumer Control id=15 [slave pointer (2)]
⎜ ↳ Getech HUGE TrackBall id=18 [slave pointer (2)]
⎣ Virtual core keyboard id=3 [master keyboard (2)]
↳ Virtual core XTEST keyboard id=5 [slave keyboard (3)]
↳ Power Button id=6 [slave keyboard (3)]
↳ Video Bus id=7 [slave keyboard (3)]
↳ Power Button id=8 [slave keyboard (3)]
↳ 2.4G Mouse Consumer Control id=10 [slave keyboard (3)]
↳ ELECOM IST TrackBall System Control id=12 [slave keyboard (3)]
↳ 2.4G Mouse id=13 [slave keyboard (3)]
↳ ELECOM IST TrackBall Consumer Control id=16 [slave keyboard (3)]
↳ 2.4G Mouse System Control id=17 [slave keyboard (3)]
↳ Getech HUGE TrackBall id=19 [slave keyboard (3)]
[takahab@rocky92 ~]$ xinput --get-button-map 11
1 2 3 4 5 6 7 8 9
[takahab@rocky92 ~]$ xinput query-state 11
2 classes :
ButtonClass
button[1]=up
button[2]=up
button[3]=up
button[4]=up
button[5]=up
button[6]=up
button[7]=up
button[8]=up
button[9]=up
ValuatorClass Mode=Relative Proximity=In
valuator[0]=4918
valuator[1]=2589
valuator[2]=0
valuator[3]=-15
[takahab@rocky92 ~]$
' OR '1'='1 ' OR '1'='1' -- ' UNION SELECT null, null, user, password FROM users -- '; WAITFOR D...