Cara menggunakan unhexlify in python

Kita akan menggunakan bantuan python untuk mengerjakan soal ini. Berikut script sederhana yang dapat kita gunakan.

from Crypto.Cipher import AES
import base64
encoded_key = “6v3TyEgjUcQRnWuIhjdTBA==”
encoded_chiper = “rW4q3swEuIOEy8RTIp/DCMdNPtdYopSRXKSLYnX9NQe8z+LMsZ6Mx/x8pwGwofdZ”
key = base64.b64decode(encoded_key)ciphertext = base64.b64decode(encoded_chiper)decipher = AES.new(key, AES.MODE_ECB)msg =decipher.decrypt(ciphertext)print msg
computeRSA

RSA encryption/decryption is based on a formula that anyone can find and use, as long as they know the values to plug in. Given the encrypted number 150815, d = 1941, and N = 435979, what is the decrypted number?

HINTS:

decrypted = ((encrypted) ^ d) mod N

Write-up:

Persamaan di hints tampaknya cukup sederhana, namun berikut ini adalah hasil yang didapat bila menggunakan kalkulator di sistem operasi Windows:

Hasil di MS Excel:

Persamaan enkripsi dan dekripsi algoritma RSA sebenarnya terbilang cukup sederhana. Problemnya adalah angka yang digunakan itu terlalu besar, sehingga akan sulit dilakukan perhitunganya apabila menggunakan teknik coba-coba misalnya (brute force). Dalam kasus RSA, kita memiliki pasangan kunci privat dan pasangan kunci publik.

Kunci Publik: PU = {e,N} → e merupakan bilangan integer dengan persyaratan tertentu

Kunci Privat PR = {d,N}

Apabila kita hendak mengirimkan pesan rahasia ke X, maka kita akan mengunci pesan kita dengan kunci publik milik X. Kemudian hanya X saja yang memiliki kunci privat yang dapat membaca pesan tersebut.

Kasus lain yang bisa digunakan oleh algoritma kunci asimetris seperti RSA ini adalah untuk proses tanda tangan digital. Skemanya adalah pesan kita kunci dengan kunci privat milik kita. Untuk membuka pesan ini, maka dibutuhkan kunci publik milik kita. Oleh karena itu, orang lain dapat memverifikasi bahwa pesan ini berasal dari kita dengan cara mendekripsi pesan ini dengan kunci publik milik kita.

What will u do??

Try it in python!

Kalau kita coba operasi pangkatnya saja dulu, begini hasilnya…

Angka pada gambar diatas itu belum selesai ya, masih ada angka2 lagi dibawahnya :v

Selanjutnya untuk menghitung nilai Plaintext kita bisa langsung menggunakan persamaan berikut di python:

>>> P = pow (150815,1941)%435979
>>> print P
133337
Hash101

Prove your knowledge of hashes and claim a flag as your prize! Connect to the service at shell2017.picoctf.com:17428

HINTS:

All concepts required to complete this challenge, including simple modular math, are quickly found by googling :)

Write-up:

Untuk mengerjakan soal ini, kita bisa memanfaatkan netcat dengan webshell yang tersedia di website pico ctf.

Some time ago, a dear friend of mine came up to me and asked about the Python module binascii – particularly about the methods

long(ord('A'))
65L
1 and
long(ord('A'))
65L
2. Since he asked for it, I’m going to share my answer publicly with you.

First of all, I’m defining the used nomenclature:

- ASCII characters are being written in single quotes
- decimal numbers are of the type Long with a L suffix
- hex values have a x prefix

First, let me quote the documentation:

binascii.b2a_hex(data) binascii.hexlify(data) Return the hexadecimal representation of the binary data. Every byte of data is converted into the corresponding 2-digit hex representation. The resulting string is therefore twice as long as the length of data.

binascii.a2b_hex(hexstr) binascii.unhexlify(hexstr) Return the binary data represented by the hexadecimal string hexstr. This function is the inverse of b2a_hex(). hexstr must contain an even number of hexadecimal digits (which can be upper or lower case), otherwise a TypeError is raised.

I’ll begin with

long(ord('A'))
65L
1. As the documentation states, this method splits a string which consists of hex-tuples into distinct bytes.

The ASCII character ‘A’ has 65L as numerical representation. To verify this in Python:

long(ord('A'))
65L

You might ask “Why is this even relevant to understand binascii?” Well, we don’t know anything about how ord() does its job. But with binascii we can re-calculate manually and verify.

binascii.hexlify('A')
'41'

Now we know that an ‘A’ – interpreted as binary data and shown in hex – resembles ’41’. But wait, ’41’ is a string and no hex value! That’s no biggy,

long(ord('A'))
65L
1 represents its result as string.

To stay with the example, let’s convert 41 into a decimal number and check if it equals 65L.

long('41', 16)
65L

Tada! It seems that ‘A’ = 41 = 65L. You might have known that already, but please, stay with me a minute longer.

To make it look a little more complex:

binascii.hexlify('A') == '%x' % long('41', 16)
True

Be aware that

long(ord('A'))
65L
5 converts a decimal number
long(ord('A'))
65L
6 into its hex representation.


long(ord('A'))
65L
7 naturally does the same thing as
long(ord('A'))
65L
1, but in reverse. It takes binary data and displays it in tuples of hex-values.

I’ll start off with an example:

binascii.unhexlify('41')
'A'
binascii.unhexlify('%x' % ord('A'))
'A'

Here,

long(ord('A'))
65L
2 takes the numerical representation 65L from the ASCII character ‘A’

ord('A')
65

converts it into hex 41

'%x' % ord('A')
'41'

and represents it as a 1-tuple (meaning dimension of one) of hex values.

And now the conclusio – why might all of this be useful? Right now, I can think of at least four use cases:

  • cryptography
  • data-transformation (i.e. Base64 for MIME/E-Mail attachments)
  • security (deciphering binary readings off a network, pattern matching, …)
  • textual representation of escape sequences

Taking up the last example, I’ll show you how to visualize the Bell escape sequence (you know, that thing that keeps beeping in your terminal). Taken from the ASCII table, the numerical representation of the Bell is 7. Programmers might know it better as

binascii.hexlify('A')
'41'
0.

ord('\a') == 7
True

Presuming you read such a character in some kind of binary data – for example from a socket and you want to visualize this data with

binascii.hexlify('A')
'41'
1, you will not get any results – at least none visible. You might hear the Bell sound if you’re not on a silent terminal.