def find_prefix_len(bs): # Create a "magic" block that decrypts to 16 null bytes. Submit queries # until the magic block appears, then compute the prefix length. r = oracle.response(bytes(bs * 3)) magic_block = statistics.mode(util.blocks(r, bs)) for qlen in range(bs, bs * 2): blocks = util.blocks(oracle.response(bytes(qlen)), bs) try: return (blocks.index(magic_block) + 1) * 16 - qlen except ValueError: continue raise RuntimeError("prefix length not found")
def parse(self, file): """ 核心方法,解析文本,打印符合要求的标签,写入新的文件中 """ # 调用 handler 实例的 start 方法,参数为 handler 实例的某个方法名的一部分 # 结果就是调用 handler 的 start_document 方法,打印文档的 head 标签 self.handler.start('document') # blocks 是从 urti.py 文件引入的生成器函数 # blocks(file) 就是一个生成器 # 使用 for 循环生成器 for block in blocks(file): # 调用过滤器,对每个文本块进行处理 for filter in self.filters: block = filter(block, self.handler) # 循环规则类的实例 for rule in self.rules: # 如果符合规则,调用实例的 action 方法打印标签 if rule.condition(block): last = rule.action(block, self.handler) # 如果 action 方法的返回值为 True # 表示该文本块处理完毕,结束循环 if last: break # 同 self.handler.start # 调用 handler 的 end_document 方法打印 '</body></html>' self.handler.end('document')
def parse(self, inputfile): for block in util.blocks(inputfile): for f in self.filters: block = f(block, handler) for r in self.rules: if r.condition(block): if r.action(block, self.handler): break
def parse(self, file): self.handler.start('document') for block in util.blocks(file): for filter in self.filters: block = filter(block, self.handler) for rule in self.rules: if rule.condition(block): last = rule.action(block, self.handler) if last: break self.handler.end('document')
def parse(self,file): self.handler.start('document') for block in util.blocks(file): for filter in self.filters: block=filter(block,self.handler) for rule in self.rules: if rule.condition(block): last=rule.action(block,self.handler) if last: break self.handler.end('document')
def parse(self, file): self.handler.start("document") for block in blocks(file): for filter in self.filters: block = filter(block, self.handler) for rule in self.rules: if rule.condition(blokc): last = rule.action(block, self.handler) if last break self.handler.end("document")
def testBlocks(self): from util import blocks f = open('test_input.txt') try: for line, item in enumerate(blocks(f)): print('block %d\t %s' % (line, item)) finally: f.close() print("test") self.failUnless(1 == 1, "Failed")
def parse(self, file): self.handler.start('document') for block in blocks(file): for filter in self.filters: block = filter(block, self.handler) for rule in self.rules: if rule.match(block): is_match = rule.mark(block, self.handler) if is_match: break self.handler.end('document')
def parse(self, file): # 对文件进行解析 self.handler.start('document') for block in util.blocks(file): # 迭代文本文件中多有文本块 for f in self.filters: # 应用过滤器,调用函数filter block = f(block, self.handler) # 以文本块和处理程序作为参数赋值给变量block for rule in self.rules: # 应用规则 if rule.condition(block): # 检查规则是否适用 last = rule.action(block, self.handler) if last: break self.handler.end('document')
def parse(self, file): self.handler.start('document') for block in blocks(file): #Update block with filter. eg: *this* to <em>this</em> for filter in self.filters: block = filter.filter(block, self.handler) for rule in self.rules: if rule.condition(block): last = rule.action(block, self.handler) if last: break self.handler.end('document')
def detect_cipher(encrypted): blocks = util.blocks(encrypted, 16) block_count = Counter() for block in blocks: block_count[block] += 1 repeated = 0 for k, v in block_count.items(): if v > 1: repeated += v return "ECB" if repeated > 0 else "CBC"
def cbc_encrypt(text, key, iv): ''' CBC encrypt text with initialization vector iv and key ''' block_length = len(iv) text = pkcs_padding(text, block_length) blocks = util.blocks(text, block_length) blocks[0] = util.string_xor(blocks[0], iv) blocks[0] = util.ecb_encrypt(blocks[0], key) for i in xrange(1, len(blocks)): blocks[i] = util.string_xor(blocks[i], blocks[i - 1]) blocks[i] = util.ecb_encrypt(blocks[i], key) return ''.join(blocks)
def cbc_decrypt(text, key, iv): ''' CBC decrypt text with initialization vector iv and key ''' block_length = len(iv) blocks = util.blocks(text, block_length) decoded_blocks = [0] * len(blocks) decoded_blocks[0] = util.ecb_decrypt(blocks[0], key) decoded_blocks[0] = util.string_xor(decoded_blocks[0], iv) for i in xrange(1, len(blocks)): decoded_blocks[i] = util.ecb_decrypt(blocks[i], key) decoded_blocks[i] = util.string_xor(decoded_blocks[i], blocks[i - 1]) return ''.join(decoded_blocks)
def cbc_encrypt(text, key, iv): ''' CBC encrypt text with initialization vector iv and key ''' block_length = len(iv) text = pkcs_padding(text, block_length) blocks = util.blocks(text, block_length) blocks[0] = util.string_xor(blocks[0], iv) blocks[0] = util.ecb_encrypt(blocks[0], key) for i in xrange(1, len(blocks)): blocks[i] = util.string_xor(blocks[i], blocks[i-1]) blocks[i] = util.ecb_encrypt(blocks[i], key) return ''.join(blocks)
def cbc_decrypt(text, key, iv): ''' CBC decrypt text with initialization vector iv and key ''' block_length = len(iv) blocks = util.blocks(text, block_length) decoded_blocks = [0] * len(blocks) decoded_blocks[0] = util.ecb_decrypt(blocks[0], key) decoded_blocks[0] = util.string_xor(decoded_blocks[0], iv) for i in xrange(1, len(blocks)): decoded_blocks[i] = util.ecb_decrypt(blocks[i], key) decoded_blocks[i] = util.string_xor(decoded_blocks[i], blocks[i-1]) return ''.join(decoded_blocks)
def find_prefix_length(block_size): ''' Finds the prefix length Starts off with a string of length block_size * 2, and increases it till there are two repeated blocks in the encrypted text ''' s = 'a' * block_size * 2 while True: encrypted_text = profile.encrypt(s) encrypted_blocks = blocks(encrypted_text, block_size) first_repetition = first_repeated(encrypted_blocks) if first_repetition is not False: return (first_repetition + 1) * block_size - len(s) s += 'a'
def parse(self, file): self.handler.start('html') for block in blocks(file): # 调用过滤器,对每个文本块进行处理 for filter in self.filters: block = filter(self.handler, block) # 循环规则类的实例 for rule in self.rules: # 如果符合规则,调用实例的 action 方法打印标签 if rule.condition(block): last = rule.action(block, self.handler) # 如果 action 方法的返回值为 True # 表示该文本块处理完毕,结束循环 if last: break self.handler.end('html')
def recover_token(oracle): token = oracle.create_token() ans = bytearray() for c1, c2 in util.pairs(util.blocks(token, 16)): p2 = bytearray(16) for i in reversed(range(16)): padding = pad(bytes(i), 16) for guess in range(256): p2[i] = guess iv = xor(c1, p2, padding) if i == 15: # Ugly hack; prevent creating new valid padding. iv[:14] = bytearray(14) if oracle.is_padding_ok(bytes(iv + c2)): break ans += p2 return bytes(unpad(ans, 16))
class Parser: '''parent''' def __init__(self,handler): self.handler=handler self.rules=[] self.filters=[] def addRule(self,rule: self.rules.append(rule) def addFilter(self,pattern,name): def filter(block,handler): return re.sub(pattern,handler.sub(name),block) self.filters.append(filter) def parse(self,file): self.handler.start('document') for block in blocks(file): for filter in self.filters: block=filter(block,self.handler) for rule in self.rules: if rule.condition(block): last=rule.action(block,self.handler) if last: break self.handler.end('document') class BasicTextParser(Parser): def __init__(self,handler): super().__init__(handler) for rule in rule_list: self.addRule(rule) self.addFilter(r'\*(.+?)\*', 'emphasis') self.addFilter(r'(http://[\.a-zA-Z/]+)', 'url') self.addFilter(r'([\.a-zA-Z]+@[\.a-zA-Z]+[a-zA-Z]+)', 'mail') def main(): handler=HTMLRenderer() parser=BasicTextParser(handler) parser.parse(sys.stdin) if __name__='__main__': main()
def generate_new_wrong_list(context, fp2): prebody = '' graylist = '' for block in blocks(context): if len(block): head = (block[0] + '\r\n') graylist += (block[0] + '\r\n') # prebody += (block[0]+'\r\n') for i in range(1, block.__len__()): precontent = '' string = str(block[i]).strip() + '\n' newstr = ignore_old_wrong_node(string, fp2) if newstr == '': graylist += string precontent += newstr if precontent == '': prebody += precontent else: prebody += head prebody += precontent prebody += '\r\n\r\n' graylist += '\r\n\r\n' return prebody, graylist
''' Created on 2013-11-27 @author: zzy ''' import sys, re from util import blocks print '<html><head><title>...</title><body>' title = True for block in blocks(sys.stdin): block = re.sub(r'\*(.+?)\*', r'<em>\1</em>', block) if title: print '<h1>' print block print '</h1>' title = False else: print '<p>' print block print '</p>' print '</body></html>'
if __name__ == '__main__': block_size = 16 prefix_length = find_prefix_length(block_size) suffix_length = 17 # Not computing it here, but can be found similarly to prefix length computation profile.decrypt(profile.encrypt('*****@*****.**')) # First create an encrypted block so that admin is at the start of the block # It should be like this # |________________| |_____________| |admin______| |____________| padding_length = block_size - prefix_length % block_size email = 'a' * padding_length + 'admin' # Get the block which has 'admin' at the start and store it encrypted = profile.encrypt(email) block_to_copy = blocks(encrypted, 16)[prefix_length / block_size + 1] # Now we need to get so that role=user is divided on the = # 'role=' is at the right boundary of a block, and 'admin' # is at the left boundary of the next block, which we'll overwrite # It should be like this # |________________| |_________role=| |user______| cruft_length = prefix_length + (suffix_length - 4) # Number of bytes required to round cruft to nearest block_size padding_length = block_size - cruft_length % block_size encrypted = profile.encrypt('a' * padding_length) encrypted_blocks = blocks(encrypted, block_size) encrypted_blocks[
def is_ecb_oracle(bs): b = util.blocks(oracle.response(bytes(bs * 2)), bs) return len(b) > len(set(b)) # Any repeats?
def is_aes_ecb_ct(m): blocks = util.blocks(m, 16) return len(blocks) > len(set(blocks))
import util from handlers import Handler file = open('file.txt',encoding = 'utf-8') g = util.blocks(file) for i in g: print(i) Handler.
import sys import re from util import blocks print("<html><head><title>...</title><body>") title = True for block in blocks(sys.stdin): block = re.sub(r"\*(.+?)\*", r"<em>\1</em>", block) if title: print("<h1>") print(block) print("</h1>") title = False else: print("<p>") print(block) print("</p>") print("</body></html>")
#!/usr/bin/env python3 import collections import sys import util mapping = collections.defaultdict(set) mapping_straight = collections.defaultdict(set) with open('ext/candc/src/data/ccg/cats/markedup') as f: for block in util.blocks(l for l in f if not (l.startswith('=') or l.startswith('#'))): if not block: continue block = block.split('\n') cat = block[0].rstrip() markedup = block[1].split()[1] cat_straight = cat.replace('\\', '/') markedup_straight = markedup.replace('\\', '/') mapping[cat].add(markedup) mapping_straight[cat_straight].add(markedup_straight) print('# dependency-ambiguous original categories') print() for cat, markedups in mapping.items(): if len(markedups) > 1: print(cat) for markedup in markedups: print(' ' + markedup) print()
def heuristic(ks): pairs = util.pairs(util.blocks(ct, ks)) return sum(hamming(x, y) for x, y in pairs if len(x) == len(y))
# -*- coding: utf-8 -*- import os, sys, re from util import blocks, lines fileTobeturned = open('Project One/test_input.txt') fileOutput = open('Project One/text_output.html', 'w') fileOutput.write('<html><head><title>...</title><body>\n') title = True for block in blocks(fileTobeturned): block = re.sub(r'\*(.+?)\*', r'<em>\1</em>', block) if title: fileOutput.write('<h1>') fileOutput.write(block) fileOutput.write('</h1>\n') title = False else: fileOutput.write('<p>') fileOutput.write(block) fileOutput.write('</p>\n') fileOutput.write('</body></html>')
if __name__ == '__main__': block_size = 16 prefix_length = find_prefix_length(block_size) suffix_length = 17 # Not computing it here, but can be found similarly to prefix length computation profile.decrypt(profile.encrypt('*****@*****.**')) # First create an encrypted block so that admin is at the start of the block # It should be like this # |________________| |_____________| |admin______| |____________| padding_length = block_size - prefix_length % block_size email = 'a' * padding_length + 'admin' # Get the block which has 'admin' at the start and store it encrypted = profile.encrypt(email) block_to_copy = blocks(encrypted, 16)[prefix_length/block_size + 1] # Now we need to get so that role=user is divided on the = # 'role=' is at the right boundary of a block, and 'admin' # is at the left boundary of the next block, which we'll overwrite # It should be like this # |________________| |_________role=| |user______| cruft_length = prefix_length + (suffix_length - 4) # Number of bytes required to round cruft to nearest block_size padding_length = block_size - cruft_length % block_size encrypted = profile.encrypt('a' * padding_length) encrypted_blocks = blocks(encrypted, block_size)
#_*_ coding: utf-8 _*_ import re,sys from util import blocks print '<html><head><title>...</title><body>' title=True # for block in blocks(sys.stdin): f=open(r'D:\03F_DISK\test.log','r') for block in blocks(f): if title: print '<h1>' print block print '</h1>`' title = False else: print '<p>' print block print '</p>' print '</body></html>'
import re from util import blocks f = open('test_output.html', 'w') # print('<html><head><title>...</title><body>') f.write('<html><head><title>...</title><body>') title = True for block in blocks('test_input.txt'): block = re.sub(r'\*(.+?)\*', r'<em>\l</em>', block) if title: # print('<h1>') f.write('<h1>') # print(block) f.write(block) # print('</h1>') f.write('</h1>') title = False else: # print('<p>') f.write('<p>') # print(block) f.write(block) # print('</p>') f.write('</p>') # print('</body></html>') f.write('</body></html>')
import sys, re import util print '<html><head><title>...</title><body>' title = True for block in util.blocks(sys.stdin): block = re.sub(r'\*(.+?)\*', r'<em>\1</em>', block) if title: print '<h1>' print block print '</h1>' title = False else: print '<p>' print block print '</p>' print '</body></html>'
import sys, re import util #Usage #python simple_markup.py <test_input.txt> test_output.html print '<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en"><head><title>' gen = util.blocks(sys.stdin) title = gen.next() print title print '</title></head>' print '<body>' print '<h1>' print title print '</h1>' for block in gen: block = re.sub(r'\*(.+?)\*', r'<b>\1</b>', block) block = re.sub(r'((http|ftp|https):\/\/[\w\-_]+(\.[\w\-_]+)+([\w\-\.,@?^=%&:/~\+#]*[\w\-\@?^=%&/~\+#])?)', r'<a href="\1">\1</a>', block) if re.match(r'\* .+', block): print '<h2>' print re.sub(r'\* (.+)', r'\1', block) print '</h2>' else: print '<p>' print block print '</p>' print '</body></html>'