CTF – BUGKU – 红绿灯

题目说明

题目来源: ctf.bugku.com

题目: Traffic_Light.gif

解题步骤

分析题目

看到图片扔到PS里发现有1168帧
红绿黄灯可能是二进制

假设红为1绿为0
3-15帧的二进制为1100110

1
2
3
4
5
Python 3.6.4 (v3.6.4:d48eceb, Dec 19 2017, 06:54:40) [MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> chr(int('1100110',2))
'f'
>>>

第二段

1
2
3
>>> chr(int('01101100',2))
'l'
>>>

可能就是flag

但是人工去对照太慢了

分离gif

使用Python的PIL库分离gif并保存为png

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#!/usr/bin/env python
#-*- coding:utf-8 -*-
# Author: virgin-forest
# Time: 2019-03-21 15:09:33
# Describe:

import os
from PIL import Image

def seoaration_gif(gif_file):
png_dir = gif_file[:-4] + '/'
os.mkdir(png_dir)
img = Image.open(gif_file)
try:
while True:
current = img.tell()
img.save(png_dir+str(current)+'.png')
img.seek(current+1)
except:
pass

if __name__=='__main__':
gif_file = 'Traffic_Light.gif'
seoaration_gif(gif_file)

一共1168张图片

识别图片

同样使用python的PIL库进行图像颜色识别

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
#!/usr/bin/env python
#-*- coding:utf-8 -*-
# Author: virgin-forest
# Time: 2019-03-21 15:28:22
# Describe:

import colorsys
import PIL.Image as Image

def get_dominant_color(image):
max_score = 0.0001
dominant_color = None
for count,(r,g,b) in image.getcolors(image.size[0]*image.size[1]):
# 转为HSV标准
saturation = colorsys.rgb_to_hsv(r/255.0, g/255.0, b/255.0)[1]
y = min(abs(r*2104+g*4130+b*802+4096+131072)>>13,235)
y = (y-16.0)/(235-16)

#忽略高亮色
if y > 0.9:
continue
score = (saturation+0.1)*count
if score > max_score:
max_score = score
dominant_color = (r,g,b)
return dominant_color


if __name__ == '__main__':
image = Image.open('Traffic_Light/1.png')
image = image.convert('RGB')
print(get_dominant_color(image))

经过测试获得四种颜色的RGB值:

  • 灰色(172, 172, 172)
  • 红色(254, 0, 0)
  • 绿色(7, 253, 8)
  • 黄色(254, 254, 0)

分析出颜色后的判断:

  • 红为1
  • 绿为0
  • 黄色作为分隔
  • 灰色直接跳过

得到执行脚本

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
#!/usr/bin/env python
#-*- coding:utf-8 -*-
# Author: virgin-forest
# Time: 2019-03-21 15:31:53
# Describe:

import colorsys
import PIL.Image as Image

def get_dominant_color(image):
max_score = 0.0001
dominant_color = None
for count,(r,g,b) in image.getcolors(image.size[0]*image.size[1]):
# 转为HSV标准
saturation = colorsys.rgb_to_hsv(r/255.0, g/255.0, b/255.0)[1]
y = min(abs(r*2104+g*4130+b*802+4096+131072)>>13,235)
y = (y-16.0)/(235-16)

#忽略高亮色
if y > 0.9:
continue
score = (saturation+0.1)*count
if score > max_score:
max_score = score
dominant_color = (r,g,b)
return dominant_color


if __name__ == '__main__':
numbers = ''
flag = ''
for i in range(1,1168):
file_name = 'Traffic_Light/'+str(i)+'.png'
image = Image.open(file_name)
image = image.convert('RGB')
rgb = get_dominant_color(image)
if rgb == (254, 254, 0): numbers += ' '
if rgb == (254, 0, 0): numbers += '1'
if rgb == (7, 253, 8): numbers += '0'

for i in numbers.split(' '):
flag += chr(int(i,2))

print(flag)

执行得到flag

flag{Pl34s3_p4y_4tt3nt10n_t0_tr4ff1c_s4f3ty_wh3n_y0u_4r3_0uts1d3}