Skip to main content

Finishing cryptography challenge (Matasano) with Kotlin part 2 (day 19)



Howdy, folks !

the following blog is the continuation of previous content, solving matasano with kotlin part 1  . In this post, i will talk about how to solve next two challenges in set 1 (challenge 3 to 4).

Challenge 3:

Description:

The challenge expected us to create a program that crack a hex string that has been encrypted with single byte XOR. In order to solve this question you just need to reuse the code from challenge 2 and give some little bit of alteration.




I try to design the UI really simple so you guys don't have difficulty following the code.The following is the snipped code to solve the challenge 3.



Lets try to break it down so you guys know how this function work. First, in onCreate method i have button that ready to be trigger if its been pressed by user and once the user enter the string, it will get the input from the editText element and finally passed it to the solve_chal3 function now this where the real deal going to take a place.

single byte XOR means its only encrypted with only one character from ASCII letter thus all we need to do is just brute force by creating a loop that try all character from the ascii table.


and after its done try all the possible key it will append to the variable result for the final result. The following program will generate an output like this


There we go ! we got the original string "cooking mc's like a pound of bacon" and the secret character is 88

Challenge 4:


Description:

Fortunately the challenge has almost the same task in challenge 3 but the different is that rather than decrypting one lousy string it expected us to decrypted 327 strings from the file that have been encrypted with single byte XOR and find which one of it is the real string. Like finding a needle in the haystack



Again i just going to make the UI really simple is just comprise with a button and scroll view.





The following is the code that i create to finish the challenge. First i create a listener a button that will call a function called set4_solves(). Its responsible to load the file that contain encrypted string and brute force until find the right secret using cal_english_score function. In kotlin you can load the file by the following syntax:

applicationContext.resources.openRawResource(R.raw.chall_4).bufferedReader().user{

it.forEachLine{

} => iterate every line, the it variable act as a cursor that will contain the each of the line

}

in cal_english_score function i just copy paste the previous code from challenge 3 but we don't want exhaust the resources to show all the result of brute force so i put an extra line of code that do a check of string if they have an English letter once we have hit it will append to the result variable and show it to the user


yea we got the secret string. TBH i don't recommend you guys to run the code on the vm or real phone device i try it once and it cost me a lot of time waiting (20 minutes ) because our approach is using O(n2) i suggest you guys to try solve this challenge with other programming language like python. I also create a python version of this code:

#One of the 60-character strings in this file has been encrypted by single-character XOR.
#Find it.

#have to use python enchant to detect english word

import enchant
import binascii
import string
import sys

alpha = string.ascii_lowercase
special = string.punctuation


file = open("4.txt","r")
file_encrypted = file.readlines()

d = enchant.Dict("en_US")

def single_xor(string_s,key):
    result = ""
    for x in string_s:
        result += chr(ord(x) ^ key)
    return result

data_index = 0
counter = 0
for e in file_encrypted:
    encrypted = binascii.unhexlify(e.rstrip("\n"))

    for x in range(0,256):
        counter += 1
        data = single_xor(encrypted,x)
        score = 0

        for x in data:
            if x in alpha:
                score += 1
       
        if score > 10:
            if (" " in data):

                temp = data.split(" ")
                try:
                    if d.check(temp[0]) and len(temp[0])>1:
                        print data
                except:
                    pass


Thank you :)

Comments

Popular posts from this blog

Having fun analyzing nginx log to find malicious attacker in the net (ง'̀-'́)ง (day 37)

  What makes you sleepless at night? is it because of a ghost or scary stories? is it because you have an important meeting tomorrow? or is it because you have an exam? For me, what keeps me up all night is that I keep thinking about what happens to a website that I just created, is it safe from an attacker (certainly not) or did I missing some security adjustments that lead to vulnerability? well I'm not the best secure programmer in the world, I'm still learning and there is a big possibility that I can make a mistake but for me, a mistake can be a valuable investment to myself or yourself to be better so from this idea, I want to know more about what attackers casually do when attacking a website. Here in this post, I'm going to show you how I analyzed attack to the website that I have permission to design and also some interesting findings that I could get from the analysis Background: All of this analysis comes from the traffic that is targeted to th

Utilize Pwntools for crafting ROP chain :') (day 69)

who doesn't like pwntools? it is a very versatile tool and can be customized according to our need using the python script but did you need to know that pwntools itself can help us to automatically craft a rop chain for us? so in this post, I will show you how to make rop chain less painful and make pwntools do all the heavy lifting. To demonstrate this I will use the binary challenge callme 64 bit from ropemporium link: https://ropemporium.com/challenge/callme.html Crashing the app: Like any other exploitation process, we need to crash the program by generating a long string pattern to determine the offset. based on the information from the above figure we can see that we required to provide 40 bytes of offset Fun stuff: now this where the fun stuff began write the following python script: as in the guideline of the challenged said we need to chain the function call by first to call the callme_one function, callme_two function and then callme_three funct

WriteUp PWN tarzan ROP UNICTF ಠ_ಠ (day 61)

So in this post, I'm going to talk about how to solve the Tarzan pwn challenge from UNICTF 2019. Back in the day when the competition is still going I couldn't finish it and don't have any clue to solve this but this time I was able to finish it :) Also in this post, we will be going to be heavily focused on how to utilize pwntools to construct a ROP chain. If you kinda confused about my explanation in this post you can refer to this following youtube video, link: https://www.youtube.com/watch?v=gWU2yOu0COk I build the python script based on this video Ok, let's get started! In this challenge, you will get two binary first go with tarzan and libc-2.29.so by providing .so file it tell us what version library that the target machine is using this could help us to do ROP chain. first, we run the Tarzan binary to get the basic idea of the program work and as you can see it just show you some text, newline and when you try to input something it doesn't gi