码迷,mamicode.com
首页 > 其他好文 > 详细

[BSidesSF2020]decrypt-2

时间:2020-04-05 13:21:34      阅读:97      评论:0      收藏:0      [点我收藏+]

标签:Fix   type   while   for   odi   hash   des   crypto   stdout   

#附件
import hashlib
import struct
import sys


class Crypto:

    def __init__(self, key):
        if not isinstance(key, bytes):
            raise TypeError(‘key must be of type bytes!‘)
        self.key = key
        self._buf = bytes()
        self._out = open("/dev/stdout", "wb")

    def _extend_buf(self):
        self._buf += self.key

    def get_bytes(self, nbytes):
        while len(self._buf) < nbytes:
            self._extend_buf()
        ret, self._buf = self._buf[:nbytes], self._buf[nbytes:]
        return ret

    def encrypt(self, buf):
        if not isinstance(buf, bytes):
            raise TypeError(‘buf must be of type bytes!‘)
        stream = self.get_bytes(len(buf))
        return bytes(a ^ b for a, b in zip(buf, stream))

    def set_outfile(self, fname):
        self._out = open(fname, "wb")

    def encrypt_file(self, fname):
        buf = open(fname, "rb").read()
        self._out.write(self.encrypt(buf))


class HashCrypto(Crypto):

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self._blk = self.key
        self._blkid = 0

    def _extend_buf(self):
        self._blk = hashlib.sha256(
                self._blk + struct.pack(‘<I‘, self._blkid)).digest()
        self._blkid += 1
        self._buf += self._blk


def main(argv):
    if len(argv) not in (3, 4):
        print("%s <key> <infile> [outfile]" % sys.argv[0])
        return
    argv.pop(0)
    key = argv.pop(0)
    inf = argv.pop(0)
    crypter = HashCrypto(key.encode("utf-8"))
    if sys.argv:
        crypter.set_outfile(argv.pop(0))
    crypter.encrypt_file(inf)


if __name__ == ‘__main__‘:
    main(sys.argv)

  

exp.py
import hashlib
import struct
import sys


class Crypto:

    def __init__(self, key):
        if not isinstance(key, bytes):
            raise TypeError(‘key must be of type bytes!‘)
        self.key = key
        self._buf = bytes()
        self._out = open("/dev/stdout", "wb")

    def _extend_buf(self):
        self._buf += self.key

    def get_bytes(self, nbytes):
        while len(self._buf) < nbytes:
            self._extend_buf()
        ret, self._buf = self._buf[:nbytes], self._buf[nbytes:]
        return ret

    def encrypt(self, buf):
        if not isinstance(buf, bytes):
            raise TypeError(‘buf must be of type bytes!‘)
        stream = self.get_bytes(len(buf))
        return bytes(a ^ b for a, b in zip(buf, stream))

    def set_outfile(self, fname):
        self._out = open(fname, "wb")

    def encrypt_file(self, fname):
        buf = open(fname, "rb").read()
        self._out.write(self.encrypt(buf))


class HashCrypto(Crypto):

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self._blk = self.key
        self._blkid = 0

    def _extend_buf(self):
        self._blk = hashlib.sha256(
                self._blk + struct.pack(‘<I‘, self._blkid)).digest()
        self._blkid += 1
        self._buf += self._blk


def main(argv):
    data = open("flag.svg.enc", ‘rb‘).read()
    prefix = b‘<?xml version="1.0" encoding="UTF-8" standalone="no"?>‘
    def xor_string(a,b):
        return bytes((i^j) for i,j in zip(a,b))
    keystream = xor_string(data, prefix[:32])
    crypter = HashCrypto(keystream)
    # move keystream generation one block further
    crypter._blkid = 1
    crypter._blk = keystream
    crypter._buf = keystream
    str1 = (crypter.encrypt(data))
    print(str1)
    open("out.svg", ‘wb‘).write(str1)


if __name__ == ‘__main__‘:
    main(sys.argv)

  

[BSidesSF2020]decrypt-2

标签:Fix   type   while   for   odi   hash   des   crypto   stdout   

原文地址:https://www.cnblogs.com/p201721410013/p/12636804.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!