LoginSignup
20

More than 3 years have passed since last update.

自分用メモ

Last updated at Posted at 2016-08-28

限定公開への戻し方を教えてください

はじめに

ここに色々なメモを残す
本当に自分用なので乱雑
最近始めたばっかだからわかんないことしかない

写真のコメントを見る

$ identify -verbose decrypted.png 
Image: decrypted.png
  Format: PNG (Portable Network Graphics)
  Mime type: image/png
  Class: DirectClass

〜〜〜〜〜〜略〜〜〜〜〜〜

  Properties:
    Comment: This is コメント

〜〜〜〜〜〜略〜〜〜〜〜〜

TypeError: Unicode-objects must be encoded before hashing

Python3系でこうなる

hash=hashlib.sha256("AAAAAAABBBBBBB").hexdigest()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: Unicode-objects must be encoded before hashing

Python3系では変数に文字列を代入する際にUnicode型のものが入るが、hashlibではこれに対応していないので、エラーの言うとおりencodeしてやる

hash=hashlib.sha256("AAAAAAABBBBBBB".encode('utf-8')).hexdigest()
print(hash)
eec1fa5f1af83a56fe49b550ff052373581076fb4f1972f16f18760e23c11fbe

AESのデコード With Python

decode_aes.py
secret_key = "0123456789abcdef"
iv = "0123456789abcdef"
aes = AES.new(secret_key, AES.MODE_CBC, iv)

decrypt_data = aes.decrypt(encrypt_data)

keyは16, 24, 32のいずれかのバイト長でなければならない

ValueError: AES key must be either 16, 24, or 32 bytes long

uuencodeうまくいかなかった

デコードがうまくいかなかったからどうやるかわからなかった

$ cat uue.txt | uuencode 
usage: uuencode [-m] [-o outfile] [infile] remotefile

$ uuencode < uue.txt 
usage: uuencode [-m] [-o outfile] [infile] remotefile

結局わからなかったのでPythonのuuをつかった

import uu
uu.decode("uue.txt", "dec.txt")

追記(2016/11/11)

Python2系だとこうできる

3は無理

>>> print "test".encode("uu")
begin 666 <data>
$=&5S=   

end

>>> print "test".encode("uu").decode("uu")
test

複数のファイルが結合されたファイルの情報を見る

fileは3種類のテストをやってくれるけど、最初にテストが成功した時点で実行結果を出力してしまう。

そのため、CTFでしばしば見られる、複数の形式のファイルが1つのファイルに結合されてるやつの情報がうまくつかめなかったりする。

よくしらんけどbinwalkはファイルのどこからどういうフォーマットのファイルがあるかを出してくれる。

$ binwalk test.png

DECIMAL       HEXADECIMAL     DESCRIPTION
--------------------------------------------------------------------------------
0             0x0             PNG image, 1024 x 32, 8-bit/color RGB, non-interlaced
41            0x29            Zlib compressed data, default compression

こんな感じの拡張子を変えると別の形式のファイルになるとかそういうのはこれ使ったら判断できるんじゃないかな

$ binwalk gazou.png

DECIMAL       HEXADECIMAL     DESCRIPTION
--------------------------------------------------------------------------------
0             0x0             PNG image, 640 x 480, 8-bit/color RGBA, non-interlaced
41            0x29            Zlib compressed data, default compression
95256         0x17418         Zip archive data, at least v2.0 to extract, compressed size: 93428, uncompressed size: 94601, name: share1.png
188724        0x2E134         Zip archive data, at least v2.0 to extract, compressed size: 93663, uncompressed size: 94902, name: share2.png
282539        0x44FAB         End of Zip archive

pkcrack

$ pkcrack -C test.zip -c plaintext.txt -p plaintext.txt -P plaintext.zip -d target.zip

コマンドラインの意味は
-C 暗号化ZIPファイル名
-c 暗号化ZIPファイル内にある平文が分かっているファイル名
-P 平文が分かっているファイルのZIP圧縮したファイル名(暗号化されていないZIP)
-p 平文のファイル
-d 出力するZIPファイル名

-Pは単純に平文のファイルを$ zip -0 [平文]すればいい (?)
ここよくわからんのでちゃんと調べること
コマンドのヘルプ曰く、-c、-pと他のオプションが1つ以上必要ということらしい

Usage: pkcrack -c <crypted_file> -p <plaintext_file> [other_options],
where [other_options] may be one or more of
 -o <offset>    for an offset of the plaintext into the ciphertext,
            (may be negative)
 -C <c-ZIP> where c-ZIP is a ZIP-archive containing <crypted_file>
 -P <p-ZIP> where p-ZIP is a ZIP-archive containing <plaintext_file>
 -d <d-file>    where d-file is the name of the decrypted archive which
        will be created by this program if the correct keys are found
        (can only be used in conjunction with the -C option)
 -i switch off case-insensitive filename matching in ZIP-archives
 -a abort keys searching after first success
 -n no progress indicator

最初のもこれでいい

$ pkcrack -C test.zip -c plaintext.txt -p plaintext.txt -d target.zip

Standard-lock-key.jpg

これまじなの?

apkから読めるjavaファイルができるまで

参考: apkファイルからjavaソースコードを見られるようになるまで

必要物

手順

  1. apk -> リソース
  2. apk -> dex
  3. dex -> jar
  4. jar -> class
  5. class -> java

apk -> リソース

$ apktool d target.apk

リソースがいらないならこのステップはいらない

apk -> dex

$ unzip target.apk

でてくるclasses.dexがそれ

dex -> jar

$ sh dex2jar-2.0/d2j-dex2jar.sh target.apk

jar -> class

$ unzip さっきできた.jar

class -> java

JD-GUIだとかJD-Eclipseだとか好きな方法で見ればいい

apk2dex

apk2jar.rb

必要物をそろえたらいい感じに自動でやってくれるやつ?

RSAに向けた攻撃方法

全て

Common Moduls Attack

Common Modulus Attack

コード GOTO Laboratory様のコードをPython3向けに修正

def gcd(a,b):
    while b != 0:
        a,b = b, a % b
    return a

# Extended Greatest Common Divisor
def egcd(a, b):
    if (a == 0):
        return [b, 0, 1]
    else:
        g, y, x = egcd(b % a, a)
        return [g, x - (b // a) * y, y]

# Modular multiplicative inverse
def modInv(a, m):
    g, x, y = egcd(a, m)
    if (g != 1):
        raise Exception("[-]No modular multiplicative inverse of %d under modulus %d" % (a, m))
    else:
        return x % m

def common_modulus_attack(c, e, n):
    # c = [c1,c2,c3, ... ,cn]
    # e = [e1,e2,e3, ... ,en]
    assert len(c) > 1
    assert len(e) > 1
    assert len(c) == len(e)

    for i,e_a in enumerate(e):
        for j,e_b in enumerate(e[i:]):
            if gcd(e_a,e_b) == 1:
                break
        else:
            continue
        break

    a = egcd(e_a,e_b)
    c1 = c[i]
    c2 = c[j]

    if a[1] < 0:
        m = (pow(modInv(c1,n),a[1]*-1,n) * pow(c2,a[2],n)) % n
    elif a[2] < 0:
        m = (pow(c1,a[1],n) * pow(modInv(c2,n),a[2]*-1,n)) % n

    return m

print(common_modulus_attack([c,C], [e,E], n))

様々なツールの紹介、使い方を教えてくれたページ

CTFに用意するツール

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
20