lunedì 31 ottobre 2011

Abraxas 2011 - writeup level2

Questo livello 2 si è concluso molto più velocemente rispetto al livello 1, che mi ha richiesto di scrivere 30 righe di codice, ed io ovviamente non lo faccio di mestiere...

Come l'altra volta si deve prima leggere il blog dell'agente:
So naturally, since he couldn't shut up about it anyway, I heard a lot about his work experience at DFS. Bryant wrote the power module of the space station. His motto seems to be "security through obscurity" and he was very vague about his software. All I learned is that it is written in C and authenticates the user with his user ID.

He also told me that it's corporate policy to have backdoors in the system. In case an employee leaves or dies, another employee can have access to the abandoned module. For that reason, each employee has access to all other employees' homedirectories and passwords are stored centrally in /etc/pass.
Quindi sappiamo che il programma è scritto in C... E' un'importante notizia, perchè troviamo che C tramite la chiamata getuid restituisce l'uid dell'utente... Da questo livello ho imparato una cosa nuova... La possibilità di passare variabili esterne arbitrarie ai programmi scritti in C... E' un'importante notizia da tenere in mente...

Il programma recover se avviato fa subito un controllo sul nostro uid... E ci dice che per avviare il programma dobbiamo avere l'uid=1002... Questa è un'altra importante notizia...

Allora troviamo una cartella che in cui abbiamo permessi in scrittura(/tmp da me non funzionava, ho così usato su suggerimento di Steven della chat di overthewire /dev/shm/) e creiamo una libreria condivisa per C... Ecco i comandi che ho usato io:


cd /dev/shm
mkdir livello2
cd livello2
nano fakelib.c
gcc -shared fakelib.c libfake.so
LD_PRELOAD=./libfake.so /home/level2/bin/recover


// libfake.c

int getuid() {
  return 1002;
}


Così otteniamo la password per il levello2... Ora possiamo loggarci tramite ssh come utente level2...

ps: grazie ancora a Steven per avermi indirizzato verso LD_PRELOAD...

giovedì 27 ottobre 2011

Abraxas 2011 - writeup level1

Finalmente posso dire, che con diversi aiuti sul python e qualche dritta su come arrivare alla soluzione(mai troppo dettagliate come dritte), sono finalmente arrivato a finire il livello 1 dell'Abraxas 2011 presentato all'HES2011 a Parigi. Direi che i CTF creati dal gruppo overthewire sono impossibili, oppure richiedono già una buona dose di conoscenza in fatto di crittografia... Perchè io fino a qualche giorno fa(quando ho pubblicato pure il writeup della sezione crypto di non ricordo quale CTF) non sapevo nemmeno cosa fossero XOR, cifrari di Cesare e via dicendo...

Per risolvere il primo livello di questo CTF non occorrono poi alla fine ottime conoscenze in linguaggi di programmazione, ma solo un po' di pazienza... Infatti su google si trovano sempre esempi molto utili, soprattutto per i linguaggi di programmazione nuovi, che consentono di capirne il significato anche ai meno preparati...

Io ho scelto di usare python, che non conosco, ma che si rivela sempre molto semplice... Infatti conoscendo un po' d'inglese si comprendono i linguaggi vari, ed avendolo pure usato per altre comunicazioni di rete, e per un BOF, mi sembrava il caso di usare lo stesso linguaggio...

Prima di procedere ad elencare la soluzione(che ovviamente poteva essere risolta in C,C#,C++,perl,ruby ecc) bisogna scaricare l'immagine dal sito della overthewire e seguire le istruzioni riportate nel box... Quindi leggere il blog del finto agente che ci fornisce delle informazioni necessarie per proseguire... Il sito dell'agente per il livello 1 ci propone le seguenti informazioni:

It seems the communications software Mazur wrote for the DFS space station uses very weak encryption. And be very weak I mean it really sucks. From his design documents, I've been able to gather that he uses XOR for performance reasons and a rolling key of only 4 ASCII characters!

Lucky for me, the space station is connected to the internet through a satellite communications provider. The hostname they use for the space station, which is called abraxas by the way, is abraxas.dildosfromspace.com. The communications module can be acivated through "secure" connection to port 4373.

The communications module displays a banner with lots of spaces and '#' signs in it, which should make the decryption easier.

Ci comunica che il cifrario utilizzato è XOR e la chiave d'accesso è composta di soli 4 caratteri... In più ci si deve collegare alla porta 4373 della macchina virtuale per avviare la connessione sicura... La prima parte, cioè la ricerca della chiave si risolve semplicemente con netcat e uno strumento chiamato xortool... netcat deve contenere l'opzione -o per seguire i comandi qui sotto, perchè ho notato che la versione BSD non presenta questa opzione... Io uso una lubuntu modificata per queste cose, quindi tramite synaptic si può scegliere di mantenere la versione netcat-tradizional... Comunque queste sono le istruzioni per avere i tool come li ho io...

#!/bin/bash

#Installa xortool
mkdir /pentest/crypto/
cd /pentest/crypto/
wget https://github.com/hellman/xortool/zipball/master -O xortool.zip
unzip xortool.zip
rm xortool.zip
cd ~

#Installa netcat
apt-get install -y netcat-traditional

E' un semplice codice bash, che avviato installerà xortool e netcat...

Allora per il primo passo digitiamo sulla shell:


nc -o log 192.168.66.66 4373
cat log | cut -d" " -f3-18 > key
cd /pentest/crypto/hellman-xortool-80cedef/
./xortool.py -x /home/user/key -c20

Questo ci consente di trovare la chiave usata nel cifrario... Infatti XOR è un metodo di cifratura molto debole... Le opzioni usate non sto a spiegarle... Sono molto semplici... Ho usato -c20 per indicare che il carattere più comune in hex fosse lo spazio...

Con questa chiave dfs! l'ho usata per decifrare le comunicazioni con la porta 4373... Questo il codice python da me utilizzato:


#!/usr/bin/env python

from socket import *
from Crypto.Cipher import XOR  

key='dfs!'
encryptor=XOR.new(key)
decryptor=XOR.new(key)

s = socket(AF_INET, SOCK_STREAM)
s.connect(('192.168.66.66', 4373))

testo = ''.join(s.recv(1024) for _ in range(4))
decifrato = encryptor.encrypt(testo)
print decifrato

data='1\r'
datac=decryptor.decrypt(data)
s.send(datac)

testo2 = ''.join(s.recv(1024) for _ in range(2))
decidata2=encryptor.encrypt(testo2)
print decidata2

s.close()

Adesso forse non sarà il codice meglio impostato o più veloce, ma io non studio informatica, e non conosco affatto python... Ci arrivo con l'intuito, ma tutto qui... Come dicevo, python poi è molto semplice, basta poco per leggerlo un po'...

Adesso proverò a risolvere il livello 2... Non si preannuncia facile...

ps:un grazie a Steven per avermi dato dei suggerimenti... Ed un grazie a Bakuriu del forum python-it per avermi indicato la soluzione per memorizzare stringhe dentro un unico file...

sabato 1 ottobre 2011

CSAW - 11 sezione crypto

Sto cercando di risolvere il mio primo wargames, composto da una serie di livelli in cui si deve accedere. Questo wargames è presentato da overthewire, è disponibile da poco e si chiama Abraxas. Dire che è impossibile per me è dire poco... Sono fermo al livello 0, e non riesco a risolverlo... Non ho mai avuto a che fare con la crittografia, e quella presente in Abraxas è molto difficile... Così ho deciso di imparare tramite qualche esempio... Il CTF CSAW11 ha avuto una sezione dedicata... Iniziamo con il primo.


Primo esempio
 
Cipher text: 87 101 108 99 111 109 101 32 116 111 32 116 104 101 32 50 48 49 49 32 78 89 85 32 80 111 108 121 32 67 83 65 87 32 67 84 70 32 101 118 101 110 116 46 32 87 101 32 104 97 118 101 32 112 108 97 110 110 101 100 32 109 97 110 121 32 99 104 97 108 108 101 110 103 101 115 32 102 111 114 32 121 111 117 32 97 110 100 32 119 101 32 104 111 112 101 32 121 111 117 32 104 97 118 101 32 102 117 110 32 115 111 108 118 105 110 103 32 116 104 101 109 32 97 108 108 46 32 84 104 101 32 107 101 121 32 102 111 114 32 116 104 105 115 32 99 104 97 108 108 101 110 103 101 32 105 115 32 99 114 121 112 116 111 103 114 97 112 104 121 46

E' semplice vedere che si tratta di un codice decimale. Quindi basta andare su xlate e convertire:

Welcome to the 2011 NYU Poly CSAW CTF event. We have planned many challenges for you and we hope you have fun solving them all. The key for this challenge is cryptography.

Questo primo esempio è stato molto veloce.

Secondo esempio
 
54:68:69:73:20:69:73:20:74:68:65:20:66:69:72:73:74:20:6d:65:73:73:61:67:65:20:62:65:69:6e:67:20:73:65:6e:74:20:74:6f:20:79:6f:75:20:62:79:20:74:68:65:20:6c:65:61:64:65:72:73:68:69:70:20:6f:66:20:74:68:65:20:55:6e:64:65:72:67:72:6f:75:6e:64:20:55:70:72:69:73:69:6e:67:2e:20:49:66:20:79:6f:75:20:68:61:76:65:20:64:65:63:6f:64:65:64:20:74:68:69:73:20:6d:65:73:73:61:67:65:20:63:6f:72:72:65:63:74:6c:79:20:79:6f:75:20:77:69:6c:6c:20:6e:6f:77:20:6b:6e:6f:77:20:6f:75:72:20:6e:65:78:74:20:6d:65:65:74:69:6e:67:20:77:69:6c:6c:20:62:65:20:68:65:6c:64:20:6f:6e:20:57:65:64:6e:65:73:64:61:79:20:40:20:37:70:6d:2e:20:57:65:20:77:69:6c:6c:20:61:6c:73:6f:20:72:65:71:75:69:72:65:20:61:20:6b:65:79:20:74:6f:20:62:65:20:6c:65:74:20:69:6e:74:6f:20:74:68:65:20:6d:65:65:74:69:6e:67:73:3b:20:74:68:69:73:20:77:65:65:6b:1f:73:20:6b:65:79:20:77:69:6c:6c:20:62:65:20:6f:76:65:72:74:68:72:6f:77:2e

Questo è un codice hex.Quindi basta andare su xlate e convertire:


This is the first message being sent to you by the leadership of the Underground Uprising. If you have decoded this message correctly you will now know our next meeting will be held on Wednesday @ 7pm. We will also require a key to be let into the meetings; this weeks key will be overthrow.

Terzo esempio
 
0100110001100001011100110111010000100000011101110110010101100101011010110111001100100000011011010110010101100101011101000110100101101110011001110010000001110111011000010111001100100000011000010010000001100111011100100110010101100001011101000010000001110011011101010110001101100011011001010111001101110011001011100010000001010111011001010010000001110011011001010110010101101101001000000111010001101111001000000110001001100101001000000110011101100101011011100110010101110010011000010111010001101001011011100110011100100000011000010010000001101100011011110111010000100000011011110110011000100000011000100111010101111010011110100010000001100001011000100110111101110101011101000010000001110100011010000110010100100000011011010110111101110110011001010110110101100101011011100111010000101110001000000101010001101000011001010010000001101011011001010111100100100000011001100110111101110010001000000110111001100101011110000111010000100000011101110110010101100101011010110111001100100000011011010110010101100101011101000110100101101110011001110010000001101001011100110010000001110010011001010111001101101001011100110111010001100001011011100110001101100101001011100010000001001001011001100010000001110100011010000110010101110010011001010010000001101001011100110010000001100001011011100111100101101111011011100110010100100000011001010110110001110011011001010010000001111001011011110111010100100000011010110110111001101111011101110010000001101111011001100010000001110100011010000110000101110100001000000110110101100001011110010010000001100010011001010010000001101001011011100111010001100101011100100110010101110011011101000110010101100100001000000110100101101110001000000110101001101111011010010110111001101001011011100110011100100000011000100111001001101001011011100110011100100000011101000110100001100101011011010010000001110100011011110010000001110100011010000110010100100000011011010110010101100101011101000110100101101110011001110010000001110100011010000110100101110011001000000111011101100101011001010110101100101110001000000100100101110100001000000111011101101001011011000110110000100000011000100110010100100000011010000110010101101100011001000010000001110011011000010110110101100101001000000111010001101001011011010110010100101100001000000111001101100001011011010110010100100000011100000110110001100001011000110110010100101110

Questo è un codice binario.Quindi basta andare su xlate e convertire:

Last weeks meeting was a great success. We seem to be generating a lot of buzz about the movement. The key for next weeks meeting is resistance. If there is anyone else you know of that may be interested in joining bring them to the meeting this week. It will be held same time, same place.

Quarto esempio
 
VGhhdCBtZWV0aW5nIHdhcyBhIGxpdHRsZSBjcmF6eS4gV2UgaGF2ZSBubyBpZGVhIHdoZXJlIHRob3NlIGd1eXMgaW4gdGhlIGJsYWNrIHN1aXRzIGNhbWUgZnJvbSwgYnV0IHdlIGFyZSBsb29raW5nIGludG8gaXQuIFVzZSB0aGUga2V5IGluZmlsdHJhdGlvbiBmb3IgbmV4dCB3ZWVrknMgbWVldGluZy4gU3RheSB3aXRoIHRoZSBjYXVzZSBhbmQgd2Ugd2lsbCBzdWNjZWVkLg

Questo è un codice base64.Quindi basta andare su xlate e convertire:

That meeting was a little crazy. We have no idea where those guys in the black suits came from, but we are looking into it. Use the key infiltration for next week's meeting. Stay with the cause and we will succeed.

Quinto esempio
 
JR UNIR QVFPBIRERQ GUNG BHE YNFG GUERR GENAFZVFFVBAF JR'ER RNFVYL QRPVCURERQ. JR UNIR GNXRA PNER BS GUR CNEGL ERFCBAFVOYR SBE GURVE RAPBQVAT NAQ NER ABJ HFVAT N ARJ ZRGUBQ. HFR GUR VASBEZNGVBA CEBIVQRQ NG YNFG JRRX.F ZRRGVAT GB QRPVCURE NYY ARJ ZRFFNTRF. NAQ ERZRZORE, GUVF JRRX.F XRL VF BOSHFPNGRQ.

Questo è un codice derivato dalla cifratura di Cesare. E' abbastanza semplice da decifrare con un attacco bruteforce. Si va a tentativi, al max saranno 26(mi sono dimenticato di segnare lo shift effettuato). Il testo in chiaro lo si può decifrare qui:

WE HAVE DISCOVERED THAT OUR LAST THREE TRANSMISSIONS WE'RE EASILY DECIPHERED. WE HAVE TAKEN CARE OF THE PARTY RESPONSIBLE FOR THEIR ENCODING AND ARE NOW USING A NEW METHOD. USE THE INFORMATION PROVIDED AT LAST WEEK.S MEETING TO DECIPHER ALL NEW MESSAGES. AND REMEMBER, THIS WEEK.S KEY IS OBFUSCATED.

Sesto esempio
 
PYB DRO XOHD WOODSXQ LO CEBO DY ECO UOI WKXUSXN. DROBO RKFO LOOX CYWO QBOKD SNOKC PVISXQ KBYEXN YEB WOODSXQC KC YP VKDO. DRO KEDRYBSDI GSVV QY YFOB CYWO YP DROW DY COO SP DROI PSD SXDY YEB KQOXNK.

Questo è un codice derivato dalla cifratura di Cesare. E' abbastanza semplice da decifrare con un attacco bruteforce. Si va a tentativi, al max saranno 26(qui si ha uno shift di 10). Il testo in chiaro lo si può decifrare qui:

FOR THE NEXT MEETING BE SURE TO USE KEY MANKIND. THERE HAVE BEEN SOME GREAT IDEAS FLYING AROUND OUR MEETINGS AS OF LATE. THE AUTHORITY WILL GO OVER SOME OF THEM TO SEE IF THEY FIT INTO OUR AGENDA.

Settimo esempio
 
VAOZM HPXC YZGDWZMVODJI OCZ VPOCJMDOT CVN YZXDYZY OCVO OCZMZ DN JIZ DYZV RCDXC RZ RDGG OVFZ PK VN KVMO JA JPM XVPNZ. OJ CZVM HJMZ VWJPO DO, WZ NPMZ OJ VOOZIY OCZ IZSO HZZODIB, PNZ OCZ FZT BZIZMVODJI OJ BZO DI. OCZMZ DN HPXC KGVIIDIB IZZYZY OJ WZ YJIZ, WPO DA RZ XVI ZSZXPOZ OCZ KGVI RZ RDGG WZ AMZZY.

Questo è un codice derivato dalla cifratura di Cesare. E' abbastanza semplice da decifrare con un attacco bruteforce. Si va a tentativi, al max saranno 26. Il testo in chiaro lo si può decifrare qui:

AftEr muCh DEliBErAtion thE Authority hAs DECiDED thAt thErE is onE iDEA whiCh wE will tAkE up As pArt of our CAusE. to hEAr morE ABout it, BE surE to AttEnD thE nExt mEEting, usE thE kEy gEnErAtion to gEt in. thErE is muCh plAnning nEEDED to BE DonE, But if wE CAn ExECutE thE plAn wE will BE frEED.

Ottavo esempio
 
EKEMQ XI LEWI CIESQIH ULEU BVS USEQTPMTTMBQT ESI FIMQK PBQMUBSIH. ET E SITVCU XI ESI GLEQKMQK ULI IQGSDAUMBQ PIULBH EKEMQ. ULI QIX OID JBS QIYU PIIUMQK XMCC FI ABCDKBQ. MU MT MPAISEUMWI ULEU DBV ECC EUUIQH ECC PIIUMQKT JSBP LISI BQ MQ.

Questo è un codice derivato dalla cifratura di Cesare. E' abbastanza semplice da decifrare con un attacco bruteforce. Si va a tentativi, al max saranno 26(qui si ha uno shift di 4). Il testo in chiaro lo si può decifrare qui:

AGAIn wE HAvE lEArnED tHAt Our trAnsmIssIOns ArE BEInG mOnItOrED. As A rEsult wE ArE CHAnGInG tHE EnCrYPtIOn mEtHOD AGAIn. tHE nEw KEY FOr nExt mEEtInG wIll BE POlYGOn. It Is ImPErAtIvE tHAt YOu All AttEnD All mEEtInGs FrOm HErE On In.

Difficilmente questo codice è comprensibile. Così ho salvato il codice in un file(crypto8.txt) e poi tramite shell ho modificato i caratteri che facilmente ci intuiscono errati. Allora si prova a modificare le singole lettere, avevo provato tramite shell, ma diventa impossibile con tr perchè poi risostituisce i valori già cambiati. Allora si procede con un word editor. Il testo diventa:

AGAIn wE HAvE lEArnED tHAt Our trAnsmIssIOns ArE BEInG mOnItOrED. As A rEsult wE ArE CHAnGInG tHE EnCrYPtIOn mEtHOD AGAIn. tHE nEw KEY FOr nExt mEEtInG wIll BE POlYGOn. It Is ImPErAtIvE tHAt YOu All AttEnD All mEEtInGs FrOm HErE On In.

Nono esempio
 
XI VQHISTUEQH ULEU ULMT XMCC FI QB IETD UETO UB IYIGVUI EQH ULEU XI ESI ETOMQK E CBU JSBP ECC BJ DBV. WI HB QBU JEVCU EQD PIPFIST JBS CIEWMQK, XI ESI FIUUIS BJJ XMULBVU ULBTI XIEO CMQOT. ULI ACEQQMQK TUEKI MT QBX BWIS. ULI OID JBS BVS JMQEC PIIUMQK MT JEXOIT. SIEHD DBVSTICWIT. ULI UMPI LET GBPI JBS VT UB FI JSIIH.

Questo è stato veramente difficile per me. Si procede come il precedente, sostituendo le lettere. L'unico particolare è trovare l'alfabeto che si deve sostituire.E' stato molto difficile, anche perchè per gli italiani non è sempre facile intuire la parola inglese:

we understand that this will be no easy task to esecute and that we are asking a lot from all of you. we do not fault any members for leaving, we are better off without those weak links. the planning stage is now over. the key for our final meeting is fawkes. ready yourselves. the time has come for us to be freed.

L'alfabeto sostituente è : poly*bcdefghi*kmn*rstuvws* dove l'asterisco indica una lettera non nota, perchè non è usata all'interno della frase e non è possibile dedurre, comunque è inutile.

Decimo esempio
 
XI VQHISTUEQH ULEU ULMT XMCC FI QB IETD UETO UB IYIGVUI EQH ULEU XI ESI ETOMQK E CBU JSBP ECC BJ DBV. WI HB QBU JEVCU EQD PIPFIST JBS CIEWMQK, XI ESI FIUUIS BJJ XMULBVU ULBTI XIEO CMQOT. ULI ACEQQMQK TUEKI MT QBX BWIS. ULI OID JBS BVS JMQEC PIIUMQK MT JEXOIT. SIEHD DBVSTICWIT. ULI UMPI LET GBPI JBS VT UB FI JSIIH.

Come l'esempio precedente.

this will be our last transmission. you've all received your assignments. if you are able to complete your task reply back to us using the keyword from this encryption algorithm as your key. Just remember that this is all for the greater good.

L'alfabeto sostituente è : final*eob*ctvsdghkmp*ruw*y dove l'asterisco indica una lettera non nota, perchè non è usata all'interno della frase e non è possibile dedurre, comunque è inutile.

Note
Ovviamente gli ultimi due sono i più difficili di tutti... Per completarli occorre un po' di pazienza e soprattutto la cara vecchia carta, dove riportare i due alfabeti... I primi esempi era veramente troppo facili...