cryptography 1 - ctfd · what is cryptography •transmitting a message between 2 parties without...

36
Cryptography 1 Introduction to Offensive Security

Upload: others

Post on 13-Oct-2020

2 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Cryptography 1 - CTFd · What is Cryptography •Transmitting a message between 2 parties without having a listener be able to tell what's being said •Alice wants to talk to Bob

Cryptography1IntroductiontoOffensiveSecurity

Page 2: Cryptography 1 - CTFd · What is Cryptography •Transmitting a message between 2 parties without having a listener be able to tell what's being said •Alice wants to talk to Bob

WhatisCryptography

• Transmittingamessagebetween2partieswithouthavingalistenerbeabletotellwhat'sbeingsaid• AlicewantstotalktoBob• ButEveiseavesdropping!• HowcanAlicetalkwithBobprivately?

Page 3: Cryptography 1 - CTFd · What is Cryptography •Transmitting a message between 2 parties without having a listener be able to tell what's being said •Alice wants to talk to Bob

ClassicalCiphers

• Substitutionciphers• Bijectivemapfromeachcharactertoanother

• Whybijective?• Monoalphabetic• Leta->z,b->y,c->x,…

• hello• svool

Page 4: Cryptography 1 - CTFd · What is Cryptography •Transmitting a message between 2 parties without having a listener be able to tell what's being said •Alice wants to talk to Bob

ClassicalCiphers

• Caesarcipher• a.k.a.shiftcipher• Asimplefamilyofsubstitutionciphers• Eachcharactermapstothecharactersomenumberofpositionsdownthealphabet• Ifshiftis1:a->b,b->c,c->d,etc.

• Hello!• Ifmmp!

• ROT13

Page 5: Cryptography 1 - CTFd · What is Cryptography •Transmitting a message between 2 parties without having a listener be able to tell what's being said •Alice wants to talk to Bob

ClassicalCiphers

• BreakingaCaesarcipher• Thereareonly26options…• Trythemall!

• Butwecandobetter!• Frequencyanalysis

Page 6: Cryptography 1 - CTFd · What is Cryptography •Transmitting a message between 2 parties without having a listener be able to tell what's being said •Alice wants to talk to Bob

FrequencyAnalysis

• Somecharactersareusedmorethanothers• ForEnglish,""(space)and"e"aresupercommoncharacterswhile"j","q","x",and"z"arealmostneverused• Foralargeenoughciphertext,thefrequencydistributionshouldroughlymatchthegeneralEnglishdistribution• Then,wejusthavetomatchtheletterstogether!

Page 7: Cryptography 1 - CTFd · What is Cryptography •Transmitting a message between 2 parties without having a listener be able to tell what's being said •Alice wants to talk to Bob
Page 8: Cryptography 1 - CTFd · What is Cryptography •Transmitting a message between 2 parties without having a listener be able to tell what's being said •Alice wants to talk to Bob

FrequencyAnalysis

• Thiscanalsobeextendedtomorethanonecharacter:n-gramanalysis• Bigramanalysis:matchingthedistributionofcharacterpairs• https://en.wikipedia.org/wiki/Bigram#Bigram_frequency_in_the_English_language

Page 9: Cryptography 1 - CTFd · What is Cryptography •Transmitting a message between 2 parties without having a listener be able to tell what's being said •Alice wants to talk to Bob

Englishbigramfrequencies

Page 10: Cryptography 1 - CTFd · What is Cryptography •Transmitting a message between 2 parties without having a listener be able to tell what's being said •Alice wants to talk to Bob

PolyalphabeticCiphers

• Onecharacternolongeralwaysencryptstothesameresult!• Theresultsomehowcombinesthemessageandakey

• Vigenère cipher• Shiftcipherwheretheoffsetisbasedonthecurrentcharacterinthekey• Messageis"ABC",keyis"ABC"

• A->A• B->C• C->E• Ciphertext is"ACE"

Page 11: Cryptography 1 - CTFd · What is Cryptography •Transmitting a message between 2 parties without having a listener be able to tell what's being said •Alice wants to talk to Bob

Vigenère square

Page 12: Cryptography 1 - CTFd · What is Cryptography •Transmitting a message between 2 parties without having a listener be able to tell what's being said •Alice wants to talk to Bob

XOR

• eXclusive OR• Youprobablyrememberthisfromoneofmany priorclasses• Outputis1ifAor Bis1,butnotifbothare• ⊕ isthesymboltorepresenttheoperation

• Programminglanguagesusuallyuse^• Hassomeniceproperties:

• A⊕B⊕A=B• Itisitsowninverse

• (A⊕B)⊕C=A⊕(B⊕C)• Commutativity

Page 13: Cryptography 1 - CTFd · What is Cryptography •Transmitting a message between 2 parties without having a listener be able to tell what's being said •Alice wants to talk to Bob

XOR

• A⊕B⊕A=B• Itisitsowninverse

• IfAisakeyandcanbetransmittedsecurely,wecanusethistoencrypt!• Example• Alicewantstosendm=13toBob• They'veagreedonk=60• Alicecomputes13^60=49,andsendsittoBob• Bobcomputes49^60=13.• Evemeanwhileonlysees49!

Page 14: Cryptography 1 - CTFd · What is Cryptography •Transmitting a message between 2 parties without having a listener be able to tell what's being said •Alice wants to talk to Bob

XOR

• Example2• Alicewantstosendm="Hello!"([72,101,108,108,111,33])toBob• They'veagreedonk="foobar"([102,111,111,98,97,114])• Alicecomputes72^102=46,101^111=10,etc.andsendsittoBob• Bobcomputes46^102=72,10^111=101,etc.• Eveonlysees[46,10,3,14,14,83]

• This is aOne Time Pad (OTP)which is perfectly secure• Aslongasyou don’t reuse the one time pad• Aslongasthe one time pad is truly random

• However,thisreliesonkbeingtransmittedsecurelyoutofband• Sowhynottransfermthisway?

Page 15: Cryptography 1 - CTFd · What is Cryptography •Transmitting a message between 2 parties without having a listener be able to tell what's being said •Alice wants to talk to Bob

XOR

• Solet'suseasinglebyte!• Alicewantstosendm="Hello!"([72,101,108,108,111,33])toBob• They'veagreedonk=60• Alicecomputes72^60=116,101^60=89,etc.andsendsittoBob• Bobcomputes116^60=72,89^60=101,etc.• Eveonlysees[116,89,80,80,83,29]• But…

Page 16: Cryptography 1 - CTFd · What is Cryptography •Transmitting a message between 2 parties without having a listener be able to tell what's being said •Alice wants to talk to Bob

XOR

• Evetriestodecrypt[116,89,80,80,83,29]with all 1bytevalues• One of them works!60• Could also usefrequency analysis• Sometimes NULLbyte(or another constant)is common aswell…• 0^k=k,soalargechunkofthesamevaluesmaybethekey…

• This breaks the cryptosystem entirely asthe security hinges onasinglebytewhich can be quickly brute forced

Page 17: Cryptography 1 - CTFd · What is Cryptography •Transmitting a message between 2 parties without having a listener be able to tell what's being said •Alice wants to talk to Bob

XOR

• Ok,whataboutarepeatingmulti-bytekey?• AliceandBobagreeonk="SecretKey"([83,101,99,114,101,116,75,101,121])• Forthesakeofanalysis,letssaym={contentsofAliceinWonderland}• Howcanwebreakthis?

Page 18: Cryptography 1 - CTFd · What is Cryptography •Transmitting a message between 2 parties without having a listener be able to tell what's being said •Alice wants to talk to Bob

XOR

• k="SecretKey"([83,101,99,114,101,116,75,101,121])• Iflen(k)isknown,createanewciphertext witheverylen(k)'thcharacter• i.e.takechar0,9,18,27,etc.inonegroup,1,10,19,28,etc.inanother,…

• Thenwecanjustperformoursingle-byteattackoneachgroup

Page 19: Cryptography 1 - CTFd · What is Cryptography •Transmitting a message between 2 parties without having a listener be able to tell what's being said •Alice wants to talk to Bob

SymmetricCrypto

• BasicXORissymmetric– bothAliceandBobusethesamekeyk• Othercommonsymmetriccryptoschemes:• RC4• DES• 3DES• AES

Page 20: Cryptography 1 - CTFd · What is Cryptography •Transmitting a message between 2 parties without having a listener be able to tell what's being said •Alice wants to talk to Bob

AES

• Blockbasedsymmetricencryption• Inputischunkedintoblocksof16,24,or32bytes(128,192,256bits)• Multiplemodes• ECB

• Flaw:sameinputblockresultsinsameoutputblock• CBC

• Flaw:malleable.XORontheciphertext XORstheplaintextwhendecrypted• CTR• GCM• … manymore

Page 21: Cryptography 1 - CTFd · What is Cryptography •Transmitting a message between 2 parties without having a listener be able to tell what's being said •Alice wants to talk to Bob

ECBPenguin

Page 22: Cryptography 1 - CTFd · What is Cryptography •Transmitting a message between 2 parties without having a listener be able to tell what's being said •Alice wants to talk to Bob

CBCDecryptionOverview

Page 23: Cryptography 1 - CTFd · What is Cryptography •Transmitting a message between 2 parties without having a listener be able to tell what's being said •Alice wants to talk to Bob

CBCMalleabilityDemo

Page 24: Cryptography 1 - CTFd · What is Cryptography •Transmitting a message between 2 parties without having a listener be able to tell what's being said •Alice wants to talk to Bob

Real-WorldCBCMalleabilityAttacks

• https://security.stackexchange.com/questions/2202/lessons-learned-and-misconceptions-regarding-encryption-and-cryptology/2206#2206

Page 25: Cryptography 1 - CTFd · What is Cryptography •Transmitting a message between 2 parties without having a listener be able to tell what's being said •Alice wants to talk to Bob

SolutiontoMalleability?

• Useauthenticatedencryption!• Manyout-of-the-boxciphermodessupportauthenticationnatively• GCM,EAX,CCM,OCB,…

• Do-it-yourself(lessrecommended):• Wecanuseamessageauthenticationcode (MAC)tocomputeahashofthemessage,andthenuseittoverifythatthemessagewasdecryptedcorrectly• Encrypt-then-MAC:

• Encryptyourdata=>C.ThencomputeMAC(C),andsendC,MAC(C)• Encrypt-then-MACworksfine,butrequiressomecareinimplementing

Page 26: Cryptography 1 - CTFd · What is Cryptography •Transmitting a message between 2 parties without having a listener be able to tell what's being said •Alice wants to talk to Bob

WhynotMAC-then-encrypt?

• WecouldalsoimagineinsteadtakingaMACoftheplaintext,andthencheckingitafterdecryption• Theoreticalreason:Bellare andNamprempre,2000• “CryptographicDoomPrinciple”:ifyouhavetoperformanycryptographicoperationbeforevalidatingtheMAConamessage,itwillsomehow likelyleadtodoom(MoxieMarlinspike,2011)• https://moxie.org/blog/the-cryptographic-doom-principle/

• Examplesfromthearticle:• Vaudenay Attack• AttackonSSH

Page 27: Cryptography 1 - CTFd · What is Cryptography •Transmitting a message between 2 parties without having a listener be able to tell what's being said •Alice wants to talk to Bob

Bellare andNamprempre

Page 28: Cryptography 1 - CTFd · What is Cryptography •Transmitting a message between 2 parties without having a listener be able to tell what's being said •Alice wants to talk to Bob

AsymmetricCrypto

• a.k.a.publickeycryptography• Involves2distinctkeys:a"public"keyanda"private"key• Shouldbeobviouswhichoneyouneedtokeepsecret…

• Whenyouencryptwithone,itbecomesreadableonlybytheother• Worksbothways• Encryptingwithpublickeymakesitdecipherableonlytothosewiththe

privatekey• "Encrypting"withprivatekeycanbeusedtosignmessages

• Commonlyusedtobootstrapsymmetriccryptoalgorithmsastheyarefaster

Page 29: Cryptography 1 - CTFd · What is Cryptography •Transmitting a message between 2 parties without having a listener be able to tell what's being said •Alice wants to talk to Bob

RSA

• Themostwellknownasymmetriccryptosystem• Let'swalkthroughasampleencrypt/decryptcycle

Page 30: Cryptography 1 - CTFd · What is Cryptography •Transmitting a message between 2 parties without having a listener be able to tell what's being said •Alice wants to talk to Bob

RSAExample(“textbook”RSA)

• Alicegeneratesapublic/privatekeypair• Generates2(large)primes:pandq• Computesn=pq.Thisisthepublicmodulus• Computesthetotientofn:phi=(p-1)(q-1)• Pickanumberecoprimetophi(usually3,5,17,257,65537)• Computedasthemultiplicativeinverseofemodphi

• (n,e)isthepublickey• (n,d)istheprivatekey

Page 31: Cryptography 1 - CTFd · What is Cryptography •Transmitting a message between 2 parties without having a listener be able to tell what's being said •Alice wants to talk to Bob

RSAExample(“textbook”RSA)

• BobnowwantstotalkwithAlice• AlicesendsBobthepublickey• Bobencryptshismessagembyraisingittothepublicexponent(e)modn• c=me (modn)

• Bobthensendstheciphertext ctoAlice• Alicedecryptsitbyraisingctotheprivateexponent(d)• m=cd (modn)

• Thisturnsinto:• (me)d (modn)=>med (modn)=>m1 =m

Page 32: Cryptography 1 - CTFd · What is Cryptography •Transmitting a message between 2 parties without having a listener be able to tell what's being said •Alice wants to talk to Bob

SimpleRSAAttacks

• Therearemany,manywaystomessupimplementingRSA• Agreatoverviewofattacks:• DanBoneh:“TwentyyearsofattacksontheRSACryptosystem”• ForRSA-basedCTFchallenges,maybe90%ofthemwillbebasedonanattackfromthispaper

• We’llonlycoverafewofthesimpleronesinclass

Page 33: Cryptography 1 - CTFd · What is Cryptography •Transmitting a message between 2 parties without having a listener be able to tell what's being said •Alice wants to talk to Bob

Factoring

• Factoringtheproductoftwolargeprimenumbersisgenerallyconsideredtobeveryhard• Theorynote:althoughitisnot knowntobeNP-HardProbablyliesinsomecomplexityclassbetweenPandNP,butwedon’tknowforsure.• Futurenote:quantumcomputing,ifrealized,willchangethis.Shor’salgorithmisapolynomialtimealgorithmforfactoring– aslongasyou’vegotaquantumcomputer!

• Ifnissufficientlysmall,wecanjustfactorit!• Currentbestalgorithm:GeneralNumberFieldSieve(GNFS)

Page 34: Cryptography 1 - CTFd · What is Cryptography •Transmitting a message between 2 parties without having a listener be able to tell what's being said •Alice wants to talk to Bob

SmallModulusAttack

• Recallthattoencryptwedo• me (modN)

• Q:doestheattackerknowe?

Page 35: Cryptography 1 - CTFd · What is Cryptography •Transmitting a message between 2 parties without having a listener be able to tell what's being said •Alice wants to talk to Bob

SmallModulusAttack

• Recallthattoencryptwedo• me (modN)

• Q:doestheattackerknowe?YES• (N,e)isthepublickey – everyoneknowsit• Whathappensifme <N?

Page 36: Cryptography 1 - CTFd · What is Cryptography •Transmitting a message between 2 parties without having a listener be able to tell what's being said •Alice wants to talk to Bob

SmallModulusAttack

• Recallthattoencryptwedo• me (modN)

• Q:doestheattackerknowe?YES• (N,e)isthepublickey – everyoneknowsit• Whathappensifme <N?• Wecanjusttakethee-th roottorecoverm!