1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262
| HTTP/1.1 200 OK Date: Sun, 22 Nov 2020 02:33:49 GMT Server: WSGIServer/0.2 CPython/3.5.2 Vary: Cookie Content-Type: text/html; charset=utf-8 X-Frame-Options: SAMEORIGIN Content-Length: 7786
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>åªæææè½ç¨çAESå¾çå å¯ç¨åº</title> </head> <body> <h1>views.py</h1> <code> from django.shortcuts import render from django.shortcuts import redirect import hashlib import string import random from . import models from . import forms from . import file_handle
def hash_code(s, salt='hellohacker'): h = hashlib.sha256() s += salt h.update(s.encode()) return h.hexdigest()
# æªç»å½äººåï¼ä¸è®ºæ¯è®¿é®indexè¿æ¯loginålogoutï¼å
¨é¨è·³è½¬å°loginçé¢ # å·²ç»å½äººåï¼è®¿é®loginä¼èªå¨è·³è½¬å°indexé¡µé¢ # å·²ç»å½äººåï¼ä¸å
许ç´æ¥è®¿é®register页é¢ï¼éå
logout # ç»åºåï¼èªå¨è·³è½¬å°loginçé¢ # Create your views here. def index(request):
if not request.session.get('is_login', None): return redirect('/login/') user = request.session.get('user_name') if not request.session.get('is_admin', None): message_f = 'ä½ ä¸æ¯ç®¡çåï¼è¯·åæ管çå' else: message_s = '欢è¿ç»éï¼ç®¡çå' return render(request, 'login/index.html', locals())
def login(request): if request.session.get('is_login', None): return redirect('/index/') if request.method == "POST": login_form = forms.UserForm(request.POST) message = '请æ£æ¥å¡«åçå
容!' if login_form.is_valid(): username = login_form.cleaned_data.get('username') password = login_form.cleaned_data.get('password') try: user = models.User.objects.get(name=username) except: message = 'ç¨æ·ä¸åå¨' return render(request, 'login/login.html', locals())
if user.password == hash_code(password): request.session['is_login'] = True request.session['user_id'] = user.id request.session['user_name'] = user.name request.session['secret'] = ''.join(random.sample(string.ascii_letters+string.digits, 16)) request.session['is_admin'] = False
return redirect('/index/') else: message = 'å¯ç ä¸æ£ç¡®' return render(request, 'login/login.html', locals())
login_form = forms.UserForm() return render(request, 'login/login.html', locals())
def register(request): if request.session.get('is_login', None): return redirect('/index/')
if request.method == 'POST': register_form = forms.RegisterForm(request.POST) message = "请æ£æ¥å¡«åçå
容" if register_form.is_valid(): username = register_form.cleaned_data.get('username') password = register_form.cleaned_data.get('password')
same_name_user = models.User.objects.filter(name=username) if same_name_user: message = 'ç¨æ·åå·²ç»åå¨' return render(request, 'login/register.html', locals())
new_user = models.User() new_user.name = username new_user.password = hash_code(password) new_user.save() return redirect('/login/') else: return render(request, 'login/register.html', locals()) register_form = forms.RegisterForm() return render(request, 'login/register.html', locals())
def logout(request): if not request.session.get('is_login', None): return redirect('/login/') request.session.flush() return redirect('/login/')
def upload_file(request): if not request.session.get('is_login', None): return redirect('/login/') if not request.session.get('is_admin', None): return render(request, 'login/admin.html')
if request.method == 'POST': img = request.FILES.get('img', None) if img.content_type != 'image/png' or img.size > 300000 or img._name.split('.')[1] != 'png': message = 'åªå
许ä¸ä¼ png æ件ä¸æ件å°äº300kb' return render(request, 'login/result.html', locals())
user = request.session.get('user_name') key = request.session.get('secret') file_handle.img_handle(img, key, user, img._name.split('.')[0]) message = 'success'
return render(request, 'login/result.html', locals()) else: return render(request, 'login/upload.html', locals())
def show_source(request): if not request.session.get('is_login', None): return redirect('/login/') if not request.session.get('is_admin', None): return render(request, 'login/admin.html')
return render(request, 'login/code.html')
</code>
<h1>file_handle.py</h1> <code> from PIL import Image from . import pic_encrypto from challenge import settings import os import hashlib
def img_handle(f, key, name, i_name): try: img = Image.open(f) except: return False h = hashlib.md5() h.update((name+key).encode()) save_path = settings.MEDIA_ROOT + '/' + h.hexdigest() if not os.path.exists(save_path): os.makedirs(save_path.replace('\\', '/')) print(save_path) print(i_name) h.update((i_name + key).encode()) print(h.hexdigest()+'.png') pic_encrypto.encrypt(img, key.encode(), save_path, h.hexdigest()+'.png') </code>
<h1>pic_encrypto.py</h1> <code> from Crypto.Cipher import AES import binascii from PIL import Image
def replace_all(text, dic): for i, j in dic.items(): i = i.encode() j = j.encode() text = text.replace(i, j) return text
def construct_enc_image(ciphertext, relength, width, height, save_path, i_name): asciicipher = binascii.hexlify(ciphertext)
reps = {'a': '1', 'b': '2', 'c': '3', 'd': '4', 'e': '5', 'f': '6', 'g': '7', 'h': '8', 'i': '9', 'j': '10', 'k': '11', 'l': '12', 'm': '13', 'n': '14', 'o': '15', 'p': '16', 'q': '17', 'r': '18', 's': '19', 't': '20', 'u': '21', 'v': '22', 'w': '23', 'x': '24', 'y': '25', 'z': '26'} asciiciphertxt = replace_all(asciicipher, reps)
step = 3 encimageone = [asciiciphertxt[i:i + step] for i in range(0, len(asciiciphertxt), step)]
if int(encimageone[len(encimageone) - 1]) < 100: encimageone[len(encimageone) - 1] += b"1"
if len(encimageone) % 3 != 0: while (len(encimageone) % 3 != 0): encimageone.append(b"101")
encimagetwo = [(int(encimageone[int(i)]), int(encimageone[int(i + 1)]), int(encimageone[int(i + 2)])) for i in range(0, len(encimageone), step)]
while (int(relength) != len(encimagetwo)): encimagetwo.pop()
encim = Image.new("RGB", (int(width), int(height))) encim.putdata(encimagetwo) encim.save(save_path + '/' + i_name)
def encrypt(im, key, save_path, i_name): plaintext = list() plaintextstr = ""
pix = im.load()
width = im.size[0] height = im.size[1]
for y in range(0, height): for x in range(0, width): plaintext.append(pix[x, y])
for i in range(0, len(plaintext)): for j in range(0, 3): aa = int(plaintext[i][j]) + 100 plaintextstr = plaintextstr + str(aa)
relength = len(plaintext) plaintextstr = plaintextstr.encode() plaintextstr += b"h" + bytes(height) + b"h" + b"w" + bytes(width) + b"w"
while (len(plaintextstr) % 16 != 0): plaintextstr = plaintextstr + b"n"
key = key
try: obj = AES.new(key, AES.MODE_CBC, b'aaaaaaaaaaaaaaaa') except: return False
ciphertext = obj.encrypt(plaintextstr) # c = open("flag.crypto", 'wb') # c.write(flag) construct_enc_image(ciphertext, relength, width, height, save_path, i_name)
</code> </body> </html>
|