甘味処。

甘いものの話はたぶんしません。

picoCTF2018 writeup

picoCTF2018に@progfay@mayonekoとチーム「NCC」で参加しました。15510pt獲得して320位でした。

自分が解いた問題は

  • General Warmup 1
  • General Warmup 2
  • General Warmup 3
  • Resources
  • grep1
  • net cat
  • strings
  • pipe
  • environ
  • what base is this?
  • Inspect Me
  • Client Side is Still Bad
  • Logon
  • No Login
  • Safe RSA
  • rsa-madlibs
  • Super Safe RSA
  • Super Safe RSA2
  • Super Safe RSA3
  • Reversing WarmUp 2
  • Forensics Warmup 1
  • Recovering From the Snap
  • admin Panel
  • hex editor
  • Truly an Artist
  • now you don't
  • Desrouleaux

でした。 この記事では上記の問題について書きます。

General Skills

General Warmup 1

If I told you your grade was 0x41 in hexadecimal, what would it be in ASCII? と言われる。0x41はASCIIでA。おわり。

General Warmup 2

Can you convert the number 27 (base 10) to binary (base 2)? と言われる。おとなしく10進数表記の27を2進数表記する。おわり。

General Warmup 3

What is 0x3D (base 16) in decimal (base 10).と言われる。0x3Dを10進数表記するだけ。おわり。

Resources

We put together a bunch of resources to help you out on our website! If you go over there, you might even find a flag! と言われる。リンク先としてhttps://picoctf.com/resourcesが渡されるのでpicoCTF{でページ内検索する。おわり。

grep1

fileが渡される。grepっていわれたので$strings file |grep picoCTF{する。おわり。

net cat

netcatしろっていわれたので$ nc 2018shell2.picoctf.com 49387する。おわり。

strings

fileが渡される。$ strings file | grep picoする。おわり。

pipe

Connect with 2018shell2.picoctf.com 37542.っていわれるのでnetcatする。たくさん流れてくるので $ nc 2018shell2.picoctf.com 37542 | grep pico する。おわり。

environ

environは環境的な意味をもってる英語(雑)。なので環境変数なのかな~と思って問題サーバーの中で$printenvする。おわり。

what base is this?

指示された通りnetcatすると、底を変換する問題がたくさん出てくる。pythonでhex()とか入力して貼り付けする。Webツール使っても良さそう。おわり。

Web Exploitation

Inspect Me

htmlとcssとjsのファイルにそれぞれflagが分割されているので集める。おわり。

Client Side is Still Bad

jsのコードを読む。おわり。

gyazo.com

Logon

Cookieにadmin:Falseがある。拡張機能なりなんなりつかってCookieを書き換える。Cookie Manager – Get this Extension for 🦊 Firefox (ja) をつかった。おわり。

No Login

link先に飛んでFlagボタンを押すとadminじゃないからFlagあげないよって言われる。あちこちさがすとadmin=FalseというCookieが見つかる。拡張機能なりなんなりつかってCookieを書き換える。Cookie Manager – Get this Extension for 🦊 Firefox (ja) をつかった。おわり。

Cryptography

Safe RSA

Nとeとciphertextが与えられてるRSA問題。e=3で小さいのでLow Public Exponent Attackってやつだ~ってする。

import gmpy2
m,_=gmpy2.iroot(2205316413931134031046440767620541984801091216351222789180593875373829950860542792110364325728088504479780803714561464250589795961097670884274813261496112882580892020487261058118157619586156815531561455215290361274334977137261636930849125,3)
print(m) #mpz(13016382529449106065839070830454998857466392684017754632233929110204433151964285)

hex(13016382529449106065839070830454998857466392684017754632233929110204433151964285)
# '0x7069636f4354467b655f7734795f7430305f736d3431315f39663564323436347d'

asciiに変換してやれば終わり。

rsa-madlibs

netcatするとRSA暗号に関する問題が順番に渡され、答えられるかどうか聞かれる(yes/no)。yesと答えた場合はそのまま回答にうつる。

- 1問目
    - p,qが与えられてnを求める。n=p*q
- 2問目
    - n,pが与えられてqを求める。q=n/p
- 3問目
    - e,nが与えられてp,qを求める
    - 素因数分解できない桁数なのであきらて答えられませんってする
- 4問目
    - p,qが与えられてtotient(n)を求める
- totient(n)=(p-1)*(q-1)

- 5問目
    - plaintext,e,nが与えられてciphertextを求める
- ciphertext=plaintext^e mod n
- pow(plaintext,e,n) 
- 6問目
    - ciphertext,e,nが与えられてplaintextを求める
    - 素因数分解が現実的でないので答えられませんってする
- 7問目
    - q,p,eが与えられてdを求める
    - totient(n)=(p-1)(q-1)
    - d=e^(-1)mod totient(n)
    - xgcd(e,totient(n))
def xgcd(b, a):
    x0, x1, y0, y1 = 1, 0, 0, 1
    while a != 0:
        q, b, a = b // a, a, b % a
        x0, x1 = x1, x0 - q * x1
        y0, y1 = y1, y0 - q * y1
    return b, x0, y0  
- 8問目
    - p,ciphertext,e,nが与えられてplaintextを求める
    - q=n//p
        - n/pだと桁が大きいので丸められてしまう
    - totient(n)=(p-1)*(q-1)
    - d=e^(-1)mod totient(n)
    - xgcd(e,totient(n))
        - 上記コードを実行するとdが負の数となるため、totient(n)を加算する
    -  plaintext=pow(ciphertext,d,n)
- 9問目
    - ascii変換しろっていわれる。おわり。

Super Safe RSA

ciphertextとNとeが与えられる問題。Nについて[https://factordb.com/:title]で検索すると素因数分解できるので、それをもとにdを求めて復号する。

 _n=(p-1)*(q-1)
 def xgcd(b, a):
     x0, x1, y0, y1 = 1, 0, 0, 1
     while a != 0:
             q, b, a = b // a, a, b % a
             x0, x1 = x1, x0 - q * x1
             y0, y1 = y1, y0 - q * y1
     return b, x0, y0
     
 xgcd(e,_n)
 # (1, -3791887882690606072381444883453477933509422520181053061460720538328000246236303, 28386)
 d=-3791887882690606072381444883453477933509422520181053061460720538328000246236303+_n
 pt=pow(c,d,n)
 hex(pt)
 # '0x7069636f4354467b7573335f6c40726733725f7072316d33245f363739317d'

ascii変換したらおわり。

Super Safe RSA2

ciphertextとNとeが与えられる問題。eの値が大きいのでwieners attackを疑う。 GitHub - orisano/owiener: A Python3 implementation of the Wiener attack on RSAに実装があるのでお借りする。

import owiener
 
 e = 41612567195059539237042339144213248885498194048946559716002349006807748846714117750820396613149784044717908233637646356892484239003595253242761997955443952981295737644406362196847280712216803724529054751915978485375745590671877539386118316322595071621055245914992460194263596241613969669897021758528439566785
 n = 94571655035635365085759398706325335236290014335257158654077953561714444504182235844072418519124645314654005337167889492376555798854895520053087805943958487865125705889152148392163149399684674863104849135010521387886940153492514295194610869892600350410689535971326333736353929224242288002733394049392037067061
 d = owiener.attack(e, n)
 
 if d is None:
     print("Failed")
 else:
     print("Hacked d={}".format(d))
 #Hacked d=65537

dが求まったのでpow(ciphertext,d,n)でplaintextも求まる。ascii変換する。おわり。

Super Safe RSA3

multi prime RSAの問題。 素因数がpとq以外にも増えるだけでeとdの算出方法は変わらない。 ciphertextとNとeが与えられるのでNを素因数分解する。 https://factordb.com/Integer factorization calculatorを利用した。

n=3352798877*2369387227*2431171217*2584404167*2659774739*2699291249*3128975077*3592874927*2484437773*2615438143*2618494273*2624594587*2638968127*2670090937*3407996899*3542100181*4156481587*2217677383*2294163019*2360089261*2481642209*2750663507*2781222713*2869402951*3183321367*3510049541*3534801841*3678931409*3714860263*3816169081*4125046879*4267109251
import binascii

c = 967423659976144422320526684513948978222873052874997628444834116664169862453024328079660763441932673618170544580223040334526399906457678605707715045865891898922525849277148177306814671533382959747679111362955790287148837808594332142213808332127835059659414298383140785818506776105499752340995329355686034
n = 1508547885745784901956544915558535724293536275843330479254283622986213460935121752835216459933606594073514738804678585260256102038249637956751407525024381745275550945663173358228639798583183241097702558167884351104283827556017494624405275607563140412199383210793765286714481226961015337761433908370379431
e = 65537

_n =  (3352798877-1)*(2369387227-1)*(2431171217-1)*(2584404167-1)*(2659774739-1)*(2699291249-1)*(3128975077-1)*(3592874927-1)*(2484437773-1)*(2615438143-1)*(2618494273-1)*(2624594587-1)*(2638968127-1)*(2670090937-1)*(3407996899-1)*(3542100181-1) * \
    (4156481587-1)*(2217677383-1)*(2294163019-1)*(2360089261-1)*(2481642209-1)*(2750663507-1)*(2781222713-1)*(2869402951-1) * \
    (3183321367-1)*(3510049541-1)*(3534801841-1)*(3678931409-1) * \
    (3714860263-1)*(3816169081-1)*(4125046879-1)*(4267109251-1)


def xgcd(b, a):
    x0, x1, y0, y1 = 1, 0, 0, 1
    while a != 0:
        q, b, a = b // a, a, b % a
        x0, x1 = x1, x0 - q * x1
        y0, y1 = y1, y0 - q * y1
    return x0

d = xgcd(e, _n)
if d < 0:
    d += _n

plaintext = pow(c, d, n)
print(hex(plaintext))

ascii変換するだけ。おわり。

Reversing

Reversing WarmUp 2

Can you decode the following string dGg0dF93NHNfczFtcEwz from base64 format to ASCII?と言われる。base64でdecodeする。

Base64のデコード - オンラインBase64のデコーダ

などを使う。おわり。

Forensics

Forensics Warmup 1

Can you unzip this file for me and retreive the flag?と言われるのでunzipすると画像が出てくる。おわり。

Recovering From the Snap

There used to be a bunch of animals here, what did Dr. Xernon do to them?と言われる。とりあえずもらったanimal.ddにfileコマンドする。disImageか~となるので$ foremost animals.ddで画像を引きずり出す。おわり。

admin Panel

WireSharkで開くと73パケット目でadmin認証完了のHTMLファイルが送られていることがわかる。passwordはその上の68パケット目で送信されている。

gyazo.com

おわり。

hex editor

This cat has a secret to teach you.と言われるのでそっか~って言いながら$ cat hex_editor.jpgする。おわり。

Truly an Artist

Can you help us find the flag in this Meta-Material?と言われる。CTFでもらったファイルはstringするものです。おわり。

now you don't

We heard that there is something hidden in this picture. Can you find it?と言われる。CTFでもらった画像ファイルは青空白猫(青い空を見上げればいつもそこに白い猫)にかけるものです。おわり。

Desrouleaux

netcatすると配られたファイルについて問題が出される。json処理して一番たくさん出現するsourceipやら同じハッシュ値のファイルの数を数える。あまりにも面倒で辛かったのでコードを残していない()。

感想

picoCTF 開催中、ctf4bに参加しました。Crypto講義でRSAの話を基礎から教わったおかげでRSA関連の問題をすべて解くことができてたのしかったです。 Reversing系ももう少し頑張りたいです。