第四届浙江省大学生网络与信息安全竞赛(决赛)Crypto方向WP

原文地址

https://4xwi11.github.io/posts/2e516684/

决赛的体验感好多了,但密码题实属不行,最后一个NTRU没出,归结于自己学习的问题,虽然之前是有遇到过,但被格劝退了,以后对于这类的题目逐一突破;但这次也没考到NTRU的攻击

但不管怎么说吧,CTF,还是会令人感到热血沸腾

还有网络。。。昨天连着好好的,然后晚上升级了下win11,结果第二天就识别不了水晶头,还好wifi也被限制过了,连wifi可以,浪费了不少时间

Crypto

decode_and_decode

前不久buu刷题刚做过,写了个脚本

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
from base64 import *

with open("decode_and_decode.txt", "r+") as fp:
    cipher = fp.read()
    while 1:
        print(cipher)
        try:
            cipher = b16decode(cipher)
        except:
            try:
                cipher = b32decode(cipher)
                assert str(cipher).isprintable() and str(cipher).isascii()
            except:
                try:
                    cipher = b64decode(cipher)
                except:
                    break
    print(cipher)

DASCTF{9f128fe14dfa30d90fd922393e6bbdfe}

dssssa1

这个类型的dsa在ctfshow一次比赛上遇到过

但这个明显太垃了,这种题有200分啊,大跌眼镜,就是简单推导。。。推导都不用吧

s = (h + x*r) * invert(k, q) % q

啥都知道了,求个x还不简单

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
from Crypto.Util.number import *
from gmpy2 import *

p, q, g, y, h, r, s, c, k = 94515040220263097875872541668071470619435707358211716562219917331797767488022053087267566586709944785329708571559126640339609375166385904147189950035630910404534642622114804635856314928438531544553236458244225698694846607333226704467932079712515971615643868209281460429629880920550469170449935295454629293399, 1001535514136994695529636128311212301250326767869, 89288700225171676599759774184146798321191748739703246395529001979988401303800066044674449834184095667747898974375431700503800142840899194492182057885675147681600217979466719692984863330298347742657472936559041930489702116255999412448996714923112824244267910808782794442895518685864174817501040060680962447941, 93887528695360292524813814240190328732283663255426806128197957720674496260060703595933676082882204724501085633424942582304707395449222043328895852812543576418567716781870179606049899540449729036771290550645770978667075821043797569255787271932556218014920373462882329802597672026806552417735660553144344650642, 775593521305134275967472254218401264703166138817, 75084117510316201869105133948164969652170742276, 599417004454208825884865529281453774324093134827, 94203926294365722030261882520165826558476099177297861176153811285238289485953276649563642144753132730431066372867407177248194182778827143183520415437355921352580608448713381897280433120409711633310458263502217605470824497215111936036532237050330222480782799188409969149722885261258984444311562364318406725475829089368796269160936194172040318140462371217663, 208672457767877303895327222020982963931779123819
n = p * q
phi = (p - 1) * (q - 1)

x = ((s * k - h) * invert(r, q)) % q
print(x.bit_length())
d = invert(x, phi)
print(long_to_bytes(pow(c, d, n)))

320了,看分值两题应该抢到血

easyNTRU(recuring)

用la佬博客上的脚本跑,果然不行,报错多项式没有逆元

似乎私钥并不难求


emmmmm,我艹尼玛,300分?

#!/usr/bin/env sage
# -*- coding: utf-8 -*-
from Crypto.Hash import SHA3_256
from Crypto.Cipher import AES
import sys
from Crypto.Util.Padding import unpad

N = 10
p = 3
q = 512
d = 3

R.<x> = ZZ[]

c = b'\xb9W\x8c\x8b\x0cG\xde\x7fl\xf7\x03\xbb9m\x0c\xc4L\xfe\xe9Q\xad\xfd\xda!\x1a\xea@}U\x9ay4\x8a\xe3y\xdf\xd5BV\xa7\x06\xf9\x08\x96="f\xc1\x1b\xd7\xdb\xc1j\x82F\x0b\x16\x06\xbcJMB\xc8\x80'

table = [-1, 0, 1]
for i1 in table:
    for i2 in table:
        for i3 in table:
            for i4 in table:
                for i5 in table:
                    for i6 in table:
                        for i7 in table:
                            for i8 in table:
                                for i9 in table:
                                    for i10 in table:
                                        result = [i1, i2, i3, i4, i5, i6, i7, i8, i9, i10]
                                        m = R(result)
                                        sha3 = SHA3_256.new()
                                        key = sha3.update(bytes(str(m).encode('utf-8'))).digest()
                                        dypher = AES.new(key, AES.MODE_ECB)
                                        try:
                                            flag = unpad(dypher.decrypt(c), 32)
                                            if flag.startswith(b'flag') or flag.startswith(b'DASCTF'):
                                                print(flag)
                                                sys.exit(0)
                                        except:
                                            pass

煞笔,决赛出这种题,md还没出,艹

Re

RE人三项

和misc&逆向手合作出的,SM4做的不多,sbox不认识;另外两个比较简单。也是比赛快要结束的时候出的,掉到第8又冲到第2,和第1差一题

看主逻辑

__int64 __fastcall main(int a1, char **a2, char **a3)
{
     
  v11 = __readfsqword(0x28u);
  memset(plaintext, 0, sizeof(plaintext));
  memset(cipher1, 0, sizeof(cipher1));
  memset(cipher2, 0, sizeof(cipher2));
  memset(cipher3, 0, 64uLL);
  printf("Input >");
  scanf("%64s", plaintext);
  if ( divide(plaintext, cipher1, cipher2, cipher3) < 0 )// 按_分隔成三段cipher
    quit();
  length_cipher = strlen(cipher1);
  if ( encode1(cipher1, length_cipher) < 0 )
    quit();
  v4 = strlen(cipher2);
  if ( encode2(cipher2, v4) < 0 )
    quit();
  v5 = strlen(cipher3);
  if ( encode3(cipher3, v5) < 0 )
    quit();
  puts("Check completed:)\nYou got it!");
  return 0LL;
}

按照下划线把flag大括号里的分成三份分别加密

第一个直接看出来,出来是R0tl3,哦ROT13

第三个很神奇的东西,不知道是什么加密,按照他的做的写逆就好,不会存在不确定的路径

第二个,分析了半天没看出什么名堂,猜应该是一个对称加密,然后和misc兼逆向手讨论,还好他之前记过常见加密的sbox

SM4的沙盒

sbox = [0xD6, 0x90, 0xE9, 0xFE, 0xCC, 0xE1, 0x3D, 0xB7, 0x16, 0xB6,
        0x14, 0xC2, 0x28, 0xFB, 0x2C, 0x05, 0x2B, 0x67, 0x9A, 0x76,
        0x2A, 0xBE, 0x04, 0xC3, 0xAA, 0x44, 0x13, 0x26, 0x49, 0x86,
        0x06, 0x99, 0x9C, 0x42, 0x50, 0xF4, 0x91, 0xEF, 0x98, 0x7A,
        0x33, 0x54, 0x0B, 0x43, 0xED, 0xCF, 0xAC, 0x62, 0xE4, 0xB3,
        0x1C, 0xA9, 0xC9, 0x08, 0xE8, 0x95, 0x80, 0xDF, 0x94, 0xFA,
        0x75, 0x8F, 0x3F, 0xA6, 0x47, 0x07, 0xA7, 0xFC, 0xF3, 0x73,
        0x17, 0xBA, 0x83, 0x59, 0x3C, 0x19, 0xE6, 0x85, 0x4F, 0xA8,
        0x68, 0x6B, 0x81, 0xB2, 0x71, 0x64, 0xDA, 0x8B, 0xF8, 0xEB,
        0x0F, 0x4B, 0x70, 0x56, 0x9D, 0x35, 0x1E, 0x24, 0x0E, 0x5E,
        0x63, 0x58, 0xD1, 0xA2, 0x25, 0x22, 0x7C, 0x3B, 0x01, 0x21,
        0x78, 0x87, 0xD4, 0x00, 0x46, 0x57, 0x9F, 0xD3, 0x27, 0x52,
        0x4C, 0x36, 0x02, 0xE7, 0xA0, 0xC4, 0xC8, 0x9E, 0xEA, 0xBF,
        0x8A, 0xD2, 0x40, 0xC7, 0x38, 0xB5, 0xA3, 0xF7, 0xF2, 0xCE,
        0xF9, 0x61, 0x15, 0xA1, 0xE0, 0xAE, 0x5D, 0xA4, 0x9B, 0x34,
        0x1A, 0x55, 0xAD, 0x93, 0x32, 0x30, 0xF5, 0x8C, 0xB1, 0xE3,
        0x1D, 0xF6, 0xE2, 0x2E, 0x82, 0x66, 0xCA, 0x60, 0xC0, 0x29,
        0x23, 0xAB, 0x0D, 0x53, 0x4E, 0x6F, 0xD5, 0xDB, 0x37, 0x45,
        0xDE, 0xFD, 0x8E, 0x2F, 0x03, 0xFF, 0x6A, 0x72, 0x6D, 0x6C,
        0x5B, 0x51, 0x8D, 0x1B, 0xAF, 0x92, 0xBB, 0xDD, 0xBC, 0x7F,
        0x11, 0xD9, 0x5C, 0x41, 0x1F, 0x10, 0x5A, 0xD8, 0x0A, 0xC1,
        0x31, 0x88, 0xA5, 0xCD, 0x7B, 0xBD, 0x2D, 0x74, 0xD0, 0x12,
        0xB8, 0xE5, 0xB4, 0xB0, 0x89, 0x69, 0x97, 0x4A, 0x0C, 0x96,
        0x77, 0x7E, 0x65, 0xB9, 0xF1, 0x09, 0xC5, 0x6E, 0xC6, 0x84,
        0x18, 0xF0, 0x7D, 0xEC, 0x3A, 0xDC, 0x4D, 0x20, 0x79, 0xEE,
        0x5F, 0x3E, 0xD7, 0xCB, 0x39, 0x48]

然后预赛就把la佬的博客给保存在本地了,拿脚本直接跑

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
from SM4Cipher import SM4Cipher

cipher1 = 'E0gy3'
space1 = 'NOPQRSTUVWXYZABCDEFGHIJKLMnopqrstuvwxyzabcdefghijklm'
plain1 = ''
for i in cipher1:
    if 'A' <= i < 'Z':
        plain1 += space1[ord(i) - 65]
    elif 'a' <= i <= 'z':
        plain1 += space1[ord(i) - 71]
    else:
        plain1 += i

cipher2 = [0xF2, 0x73, 0x52, 0xFB, 0x8D, 0xF4, 0x1D, 0x6D, 0xC2, 0x33,
           0xB5, 0xA5, 0xEE, 0xC1, 0x60, 0xDA]
key = [b'' for _ in range(16)]
key[0] = b'\x01'
key[1] = b'#'
key[2] = b'E'
key[3] = b'g'
key[4] = b'\x89'
key[5] = b'\xAB'
key[6] = b'\xCD'
key[7] = b'\xEF'
key[8] = b'\xFE'
key[9] = b'\xDC'
key[10] = b'\xBA'
key[11] = b'\x98'
key[12] = b'v'
key[13] = b'T'
key[14] = b'2'
key[15] = b'\x10'

key = b''.join(key)
plaintext = bytes(cipher2)
sm4 = SM4Cipher(key)
plain2 = sm4.decrypt(plaintext).decode()

check_box = [7856030,
             7856049,
             7856004,
             7856004,
             7856854,
             7856004,
             7856045,
             7856004,
             7856050,
             7856004,
             7856004,
             7856004,
             7856024,
             7856004,
             7856004,
             7856060,
             7856004,
             7856004,
             7856234,
             7856004,
             7856004,
             7856004,
             7856100,
             7856151,
             7856230,
             7856233,
             7856004,
             7856004,
             7856235,
             7856004,
             7856004,
             7856004,
             7856345,
             7856004,
             7856004,
             7856004,
             7856004,
             7856104,
             7856004,
             7856565,
             7856666,
             7856785,
             7856004,
             7856004,
             7856004,
             7856004,
             7856004,
             7856004,
             7857004,
             0,
             0,
             0]
trans = [1, 7, -1, -7]
start = 7856030  # 1
target = 7857004
assert 48 == check_box.index(target)
c1 = 'a'
c2 = 'b'
mid = check_box[1]
index = 1
plain3 = c1
while 1:
    if check_box[index + 1] > mid >= check_box[index + 7]:
        index = index + 1
        mid = check_box[index]
        plain3 += c1
    elif check_box[index + 7] > mid >= check_box[index + 1]:
        index = index + 7
        mid = check_box[index]
        plain3 += c2
    else:
        print('this seems not to happen')
    if mid == target:
        break

flag = 'ZJCTF{' + plain1 + '_' + plain2 + '_' + plain3 + '}'
print(flag)
# ZJCTF{R0tl3_Sm34@and_abbbaaabbaab}

把奇怪的东西去掉

你可能感兴趣的:(树哥让我天天写之Crypto,网络,python,开发语言)