[BSidesSF2020]decrypt-2

Posted p201721410013

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[BSidesSF2020]decrypt-2相关的知识,希望对你有一定的参考价值。

#附件
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的主要内容,如果未能解决你的问题,请参考以下文章

BUU-MISC-[BSidesSF2019]zippy

BUUCTF MISC刷题笔记

BUUCTF MISC刷题笔记

BUUCTF MISC刷题笔记

BUUCTF MISC刷题笔记

漏洞通告更新WebSphere远程代码执行漏洞(CVE-2020-4450CVE-2020-4449)