HSCTF - RSA Cryptography (Reverse Search Algorithm)

John Hammond · Intermediate ·⚡ Algorithms & Data Structures ·7y ago

Key Takeaways

The video covers RSA cryptography, specifically the Reverse Search Algorithm challenge from HSCTF 2019, and demonstrates how to solve it using Python and various tools such as factorDB and katana.

Full Transcript

[Music] what's going on everybody my name is John Hammond welcome back to the YouTube video we're looking at the cryptography section of HS CTF 2019 this challenge which was the first in the category is called reverse search algorithm to point value got knocked down pretty low so it's not a difficult challenge in fact you can probably already guess by the challenge title it's a reference to RSA so we have these variables that are given to us an e and c we can assume that this is our ciphertext and our public key and as our modulus and E as the exponent so let's go ahead and create a file for this I'll just call it like RSA prompt text and I'm gonna throw these in here because I will use them later in the video and then I'll go ahead and create a new file that will just be subl RSA or api whatever we want yeah let's just do RSA not pi that's fine and we'll have our shebang line because it's going to be a Python script to solve this and we'll paste those numbers in there and then we'll start to work so with RSA I've covered this in way too many videos already we need to factor n so I want to show you a couple ways to do this right normally you do this just by factor DB factor DB calm you can face it in here and you have number 29 looks like that is one of our peas and our other factor Q is gonna be this other value so we can copy that guy in here and then we would be able to calculate fee and then we can calculate D and then we can calculate the MA message the plaintext and that works just fine for us let's go ahead and do that just whip it out real quick so from crypto dot util dot number we can import inverse and I also want to import long two bytes because I was remembered thank you to that fellow viewer and thankful and very grateful for you and sharing that long two bytes can totally be able to print out an integer representation and straight to ASCII and give us the flag so let's calculate fee right fees inverse e and fee I'm sorry that's D but we need to actually calculate fee so given that these are both prime we can calculate that totient euler's totient by P minus 1 times Q - one and then D will be the modular inverse and M can equal the power of C raised to D all mod n so we have M calculated just fine here and then we can go ahead and display it as long - bytes which will simply give us the flag right that's easy RSA decryption H s CTF yes or si is solved that's it I want to sprinkle in some other cool niceties here I'd like to show you the prime factor a in Python in fact this is a fork of the library that originally has prime fact itself that will go ahead and attempt to factor integers and offer other cool mathematics and stuff that's built off of GM py or Gympie I don't know however you'd like to say that or pronounce it but gimpy - this fork actually adds support if it's not able to determine a factor by its mathematical means it will just go ahead and ask factor DB and try and pull it off from the internet so that's that's an option here you can install it using the github repository so pip install gets and then lists location you could just tack on a get plus if you're using the URL and I know that works just fine for me I have had to stumble a little bit because I got it working in Python 3 but I needed to install a couple other modules here because it needs Gympie what I needed to do when pip was trying to install was actually install this library Lib MPC dev so I wanted to make that noted for you if you needed to install that you can and then you should be able to suit up a pip install can't be - again I was I had the most success with Python 3 you saw a lot of red because I was in pip - right if I specify pip 3 I've got 2 installed and it work just fine note you don't need to sudo if you set up a virtual environment and that is a recommended way to do it again or just kind of blazing through stuff because this is the CTF realm right we just kind of want to roll through the answer be very very careful if you pseudo install pip packages because maybe if it's maybe it could be malicious right you want to create a virtual environment now we have Gympie installed we can go ahead and actually grab at this URL from the git repository and then we can just use git plus and then plop in that URL that should go ahead and install it for us now in my code I can import Prime FAQ we have P and Q that no longer be no longer have to be manually inserted but Prime type will be able to print it out for us remember if I try and run this sublime is kind of tuned to use Python 2 right now and I don't have installed for Python 2 you've got to be cognizant of what Python version you're using so in my command line I can totally use Python 3 and P is not defined anymore so let's just exit but now you know okay there aren't any errors it would pop up because we were able to import prime factor just fine sublime text is the one that's in the wrong right now because it's using Python 2 because I have primetime installed for Python 3 where I okay okay so now prime FAQ has this nice handy handy function called factor int and you just pass in the number or variable in this case that you want to factor so let's display those and we'll print it out here 29 is one factor and this other value is another factors and that's the number of occurrences that is in the prime factorization so we can pull these out right we can say items or I'm sorry it is key doesn't it ticked yes ticked keys so then we can say P and Q will equal those that are not printed I'll actually just print out P and Q to make sure we got these a okay and we do so now we don't need to like hard code those values in prime faculty calculate them and figure them out for us and fee we'll be able to calculate this D will calculate M we'll calculate it all works perfectly fine now now we have a bytes representation of the flag so yes we have solved our essay awesome so that's it I want to showcase one last thing because I want to show off katana katana will be able to do this and just crank it out so let's get into get up Ratana I will activate the virtual environment as said again that's the better practice let's remove results let's run katana will use all here and then we'll offer so tack a for Auto not all right it'll use all units that it can find applicable HS is now our essay prompt text and our flag format is HS c TF curly braces paste that in here and it should crank through it because all that prompt has are any and see those values including in there it's found those and detected okay that's gonna be likely RSA factored it with that prime factor like just told you about knew how to calculate fee and know how to calculate d and spat it out for us so all you need to do is give it the prompt and katana will solve it cool alrighty thank you guys for watching I hope you enjoyed this if you did like this video I know I've done RSA like way too many times but in the nature of a CTF write-up I wanted to give it to you and I wanted to sprinkle in those other niceties between prime fact pulling down at github library and pip and showcasing how katana can just rip through this so soon we'll be in the hands of you the people alrighty if you liked this video please do like comment and subscribe love to see you in the discord server link in the description join the party I'd love to see you on patreon let me see you on PayPal thank you for your support and whatever you're going to help the channel see you the next video [Music]

Original Description

If you would like to support me, please like, comment & subscribe, and check me out on Patreon: https://patreon.com/johnhammond010 E-mail: johnhammond010@gmail.com PayPal: http://paypal.me/johnhammond010 GitHub: https://github.com/JohnHammond Site: http://www.johnhammond.org Twitter: https://twitter.com/_johnhammond
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from John Hammond · John Hammond · 0 of 60

← Previous Next →
1 Code Commentaries? PHP to JavaScript in Bash and PHP!
Code Commentaries? PHP to JavaScript in Bash and PHP!
John Hammond
2 Tutorials? MySQL connection with PHP and Bash!
Tutorials? MySQL connection with PHP and Bash!
John Hammond
3 Variable Naming in Python! Happy Birthday, Linux! Nokia N900!
Variable Naming in Python! Happy Birthday, Linux! Nokia N900!
John Hammond
4 JavaScript Splits The URL!
JavaScript Splits The URL!
John Hammond
5 HTML Tables in Python!
HTML Tables in Python!
John Hammond
6 HTML, Net Shares, GML!
HTML, Net Shares, GML!
John Hammond
7 Python 08 Programming Style and Comments
Python 08 Programming Style and Comments
John Hammond
8 Python 26 Object Oriented Programming
Python 26 Object Oriented Programming
John Hammond
9 75 Python Tutorials, Out Now!
75 Python Tutorials, Out Now!
John Hammond
10 Batch 14 Mathematical Expressions
Batch 14 Mathematical Expressions
John Hammond
11 Batch 85 Array Append
Batch 85 Array Append
John Hammond
12 Batch 86 Array Count
Batch 86 Array Count
John Hammond
13 Batch 87 Array Index
Batch 87 Array Index
John Hammond
14 Batch 88 Array Insert
Batch 88 Array Insert
John Hammond
15 Batch 89 Array Remove
Batch 89 Array Remove
John Hammond
16 Batch 90 Array Reverse
Batch 90 Array Reverse
John Hammond
17 Python [colorama] 00 Installing on Linux
Python [colorama] 00 Installing on Linux
John Hammond
18 Python [colorama] 09 Cursor Position
Python [colorama] 09 Cursor Position
John Hammond
19 Python [hashlib] 02 Algorithms
Python [hashlib] 02 Algorithms
John Hammond
20 Python 00 Installing IDLE on Linux
Python 00 Installing IDLE on Linux
John Hammond
21 Python [pygame] 11 Rectangular Collision Detection
Python [pygame] 11 Rectangular Collision Detection
John Hammond
22 Python [pygame] 12 Platforming Rectangular Collision Resolution
Python [pygame] 12 Platforming Rectangular Collision Resolution
John Hammond
23 Python [XML-RPC] 01 Research
Python [XML-RPC] 01 Research
John Hammond
24 Python [pyenchant] 03 Personal Word Lists
Python [pyenchant] 03 Personal Word Lists
John Hammond
25 FancyURLopener Authentication and User-Agent [urllib] 03
FancyURLopener Authentication and User-Agent [urllib] 03
John Hammond
26 Python 04: PEP8 Coding
Python 04: PEP8 Coding
John Hammond
27 Python Challenge! 17 COOKIES
Python Challenge! 17 COOKIES
John Hammond
28 Google CTF 2016: Ernst Echidna
Google CTF 2016: Ernst Echidna
John Hammond
29 Google CTF 2016: Spotted Quoll
Google CTF 2016: Spotted Quoll
John Hammond
30 Google CTF 2016: Can you Repo It?
Google CTF 2016: Can you Repo It?
John Hammond
31 Google CTF 2016: No Big Deal
Google CTF 2016: No Big Deal
John Hammond
32 Google CTF 2016: In Recorded Conversation
Google CTF 2016: In Recorded Conversation
John Hammond
33 Homemade CTF Challenge: 01 "Orchestra"
Homemade CTF Challenge: 01 "Orchestra"
John Hammond
34 Homemade CTF Challenge: 02 "Bae's Base"
Homemade CTF Challenge: 02 "Bae's Base"
John Hammond
35 Homemade CTF Challenge: 03 "Web Hunt"
Homemade CTF Challenge: 03 "Web Hunt"
John Hammond
36 Homemade CTF Challenge: 04 "UPX"
Homemade CTF Challenge: 04 "UPX"
John Hammond
37 Homemade CTF Challenge: 05 "The Assumption Song"
Homemade CTF Challenge: 05 "The Assumption Song"
John Hammond
38 Homemade CTF Challenge: 06 "A Brisk Stroll"
Homemade CTF Challenge: 06 "A Brisk Stroll"
John Hammond
39 Homemade CTF Challenge: 06 "I lost my password!"
Homemade CTF Challenge: 06 "I lost my password!"
John Hammond
40 web25 :: Mr. Robot : EKOPARTY CTF 2016
web25 :: Mr. Robot : EKOPARTY CTF 2016
John Hammond
41 web50 : RFC 7230 :: EKOPARTY CTF 2016
web50 : RFC 7230 :: EKOPARTY CTF 2016
John Hammond
42 misc50 : Hidden inside EKO :: EKOPARTY CTF 2016
misc50 : Hidden inside EKO :: EKOPARTY CTF 2016
John Hammond
43 Hack The Vote 2016 CTF: Sander's Fan Club [web100]
Hack The Vote 2016 CTF: Sander's Fan Club [web100]
John Hammond
44 Hack The Vote 2016 CTF Warpspeed [forensics150]
Hack The Vote 2016 CTF Warpspeed [forensics150]
John Hammond
45 Juniors CTF 2016 :: Black Suprematic Square
Juniors CTF 2016 :: Black Suprematic Square
John Hammond
46 Juniors CTF 2016 :: Six Strange Tales
Juniors CTF 2016 :: Six Strange Tales
John Hammond
47 Juniors CTF 2016 :: Lost Code
Juniors CTF 2016 :: Lost Code
John Hammond
48 Juniors CTF 2016 :: Here Goes!
Juniors CTF 2016 :: Here Goes!
John Hammond
49 Juniors CTF 2016 :: Southern Cross
Juniors CTF 2016 :: Southern Cross
John Hammond
50 Juniors CTF 2016 :: Clone Attack
Juniors CTF 2016 :: Clone Attack
John Hammond
51 Juniors CTF 2016 :: Dirty Repo
Juniors CTF 2016 :: Dirty Repo
John Hammond
52 Juniors CTF 2016 :: Hackers Blog
Juniors CTF 2016 :: Hackers Blog
John Hammond
53 Juniors CTF 2016 :: Voting!!!
Juniors CTF 2016 :: Voting!!!
John Hammond
54 Juniors CTF 2016 :: The Good, The Bad and The Junkman
Juniors CTF 2016 :: The Good, The Bad and The Junkman
John Hammond
55 Juniors CTF 2016 :: Stop Thief!
Juniors CTF 2016 :: Stop Thief!
John Hammond
56 Juniors CTF 2016 :: ROFL
Juniors CTF 2016 :: ROFL
John Hammond
57 Juniors CTF 2016 :: Restriced Area
Juniors CTF 2016 :: Restriced Area
John Hammond
58 Juniors CTF 2016 :: Oh SSH!
Juniors CTF 2016 :: Oh SSH!
John Hammond
59 HackCon CTF 2017 TRIVIA and BONUS Challenges
HackCon CTF 2017 TRIVIA and BONUS Challenges
John Hammond
60 HackCon CTF 2017 "Bacche" Challenges
HackCon CTF 2017 "Bacche" Challenges
John Hammond

The video teaches how to solve the Reverse Search Algorithm challenge from HSCTF 2019 using RSA cryptography and various tools.

Key Takeaways
  1. Import necessary libraries and define variables
  2. Calculate Euler's totient and modular inverse
  3. Use factorDB or katana to factorize the modulus
  4. Calculate the plaintext message
  5. Use Python to implement RSA decryption
💡 The video demonstrates how to use various tools and techniques to solve an RSA cryptography challenge, highlighting the importance of prime factorization and modular inverse calculation.

Related AI Lessons

Bloom Filters, Explained Properly
Learn how Bloom filters work and their benefits, including tiny memory and blazing speed, in exchange for potential false positives.
Dev.to · Daksh Gargas
Prefix Sums: The Preprocessing Trick That Makes Range Queries Instant
Learn how prefix sums enable instant range queries in arrays, boosting performance in various applications
Medium · Programming
I Thought I Was Ready for the Interview — Then One Simple Math Question Destroyed Me
A simple math question can destroy a developer's interview, highlighting the importance of being prepared for unexpected questions
Medium · Programming
Week 2(Day 10): LeetCode Two Pointers(slow & fast): Remove Duplicates from Sorted Array (Brute…
Learn to remove duplicates from a sorted array using the two pointers technique, improving from brute force to optimized solutions
Medium · Python
Up next
Stump Grinder Carbide Wheel Grinds Hardwood To Chips
Innoforge Studio
Watch →