CTF_WriteUps/2015_CTF
[2015_TrendMicro_CTF] (Programming 200) 사칙연산 풀기
hackability
2015. 10. 27. 00:21
최종 수정: 2015-10-27
hackability@TenDollar
안녕하세요. Hackability 입니다.
이번 포스팅은 TrendMicro CTF에 출제 되었던 Programming 문제인 사칙연산에 대한 내용입니다.
프로그래밍 200 문제는 서버에서 사칙 연산에 대해 문제를 내줍니다. 이 문제를 시간내에 빠르게 풀어야 합니다.
처음에는
7 * 9 =
4 + 3 =
이런식으로 나오다가 중간 부터
2394234 * 53945345 + ( 293429304 + 293429034 ) * 93481934 =
이런식으로 나옵니다. 하지만 이런 문제는 당황하지말고 eval 로 끝냅니다 ..
eval ( recv_msg.split("=")[0] )
하면 알아서 풀어 줍니다. -_-
근데 여기서 끝이냐? 아닙니다.....
중간에 이런 문제가 나옵니다.
IV + IIV * VIII + CI =
....... -_-
로마 문자도 숫자로 번역 해야 합니다.
근데 여기서 끝이냐? 아닙니다......
(six hundred five * two million fifty six) + one thousand five hundred twenty five =
......... -_-
요곳도 .... 풉니다... 계속 풉니다....
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 | import socket def text2int(textnum, numwords={}): if not numwords: units = [ "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen", ] tens = ["", "", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety"] scales = ["hundred", "thousand", "million", "billion", "trillion"] numwords["and"] = (1, 0) for idx, word in enumerate(units): numwords[word] = (1, idx) for idx, word in enumerate(tens): numwords[word] = (1, idx * 10) for idx, word in enumerate(scales): numwords[word] = (10 ** (idx * 3 or 2), 0) current = result = 0 for word in textnum.split(): if word not in numwords: raise Exception("Illegal word: " + word) scale, increment = numwords[word] current = current * scale + increment if scale > 100: result += current current = 0 return result + current def isNumber(s): try: float(s) return True except ValueError: return False def roman2int(roman): def getdv(r): p = 'IVXLCDMF'.find(r) return 0 if p < 0 else 10**(p / 2) * (1+(4*(p % 2))) i = 0 v = map(getdv, roman) s = 0 while i < len(v): if i+1 == len(v) or v[i] >= v[i+1]: s += v[i] else: s -= v[i] i += 1 return s server = "ctfquest.trendmicro.co.jp" port = 51740 s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.connect((server, port)) for i in range(0, 500): data = s.recv(1024) print data data = data.replace(",", "") data = data.split("=")[0] print data try: nums = eval(data) except Exception as e: tmp = data.split(" ") test = "" print tmp j = -1 while j < len(tmp)-2: j = j + 1 if tmp[j] == "(" or tmp[j] == ")": test = test + tmp[j] continue if tmp[j] == "+" or tmp[j] == "-" or tmp[j] == "*" or tmp[j] == "/": test = test + tmp[j] continue if isNumber(tmp[j]): test = test + tmp[j] continue if roman2int(tmp[j]) > 0: test = test + str(roman2int(tmp[j])) continue tt = j tmp_2 = '' for k in range(j, len(tmp)-1): if tmp[k] == "-" or tmp[k] == "*" or tmp[k] == "+" or tmp[k] == "/" or tmp[k] == ')': tt = k - 1 break tmp_2 = tmp_2 + " " + tmp[k] tt = k print tmp_2 test = test + str(text2int(tmp_2)) j = tt print "TEST : " + test nums = eval(test) s.send(str(nums) + "\n") s.close() | cs |
해결...후;;