๊ธ€ ์ž‘์„ฑ์ž: heogi

- Description

๊ฐ€์œ„๋ฐ”์œ„๋ณด ๊ฒŒ์ž„์˜ ๊ธฐ๋Šฅ์ด ์žˆ๋Š” ํŽ˜์ด์ง€์ด๋‹ค.

 

sorcecode๋ฅผ ๋ณผ ์ˆ˜ ์žˆ๋„๋ก ํ•˜์˜€๋Š”๋ฐ, ์†Œ์Šค์ฝ”๋“œ๋Š” ์•„๋ž˜์ฒ˜๋Ÿผ ๊ตฌ์„ฑ๋˜์–ด์žˆ๋‹ค.

# !/usr/bin/python

# [Game source]
# https://codereview.stackexchange.com/questions/172337/rock-paper-scissors-game-in-python

import random
import sys

def playGame():
	flag = fopen("flag", "r");
	usr_choice = input()

	'''
	cpu_random = random.randint(1,3)
	cpu_choice = cpu_random
	'''
	if usr_choice == "Rock":
		cpu_random = 2
	elif usr_choice == "Paper":
		cpu_random = 3
	elif usr_choice == "Scissors":
		cpu_random = 1
	else:
		cpu_random = 0

	if cpu_random == 1:
		cpu_choice = "Rock"
	elif cpu_random == 2:
		cpu_choice = "Paper"
	elif cpu_random == 3:
		cpu_choice = "Scissors"
	else:
		cpu_choice = "Invalid Input!!"

	if usr_choice == cpu_choice:
		result = "Tie -_-"
	elif usr_choice == "Rock" and cpu_choice == "Paper":
		result = "You Lose :("
	elif usr_choice == "Rock" and cpu_choice == "Scissors":
		result = "You Win :)" + flag
	elif usr_choice == "Paper" and cpu_choice == "Scissors":
		result = "You Lose :("
	elif usr_choice == "Paper" and cpu_choice == "Rock":
		result = "You Win :)" + flag
	elif usr_choice == "Scissors" and cpu_choice == "Rock":
		result = "You Lose :("
	elif usr_choice == "Scissors" and cpu_choice == "Paper":
		result = "You Win :)" + flag
	else:
		result = "You Lose :("

	out_text = "Your choice is " + usr_choice + "./"
	out_text += "Computer\'s choice is " + cpu_choice + "!/"
	out_text += result
	print (out_text)

if __name__ == "__main__":
	playGame()

์‚ฌ์šฉ์ž์˜ ์ž…๋ ฅ์„ input() ํ•จ์ˆ˜๋กœ ๋ฐ›์•„์„œ ์ฒ˜๋ฆฌํ•˜๊ณ , ๊ฒŒ์ž„์—์„œ ์ ˆ๋Œ€ ์ด๊ธธ ์ˆ˜ ์—†๊ฒŒ ์‚ฌ์šฉ์ž์˜ ์ž…๋ ฅ์— ๋”ฐ๋ผ ์นด์šดํ„ฐ๋ฅผ ์น˜๊ณ ์žˆ๋‹ค.

usr_choice๋ฅผ input() ํ•จ์ˆ˜๋กœ ๋ฐ›๋Š”๋ฐ ํ•ด๋‹น ํ•จ์ˆ˜์—์„œ ์ทจ์•ฝ์ ์ด ์กด์žฌํ•œ๋‹ค.

 

์•„๋ž˜ ๋งํฌ๋ฅผ ๋ณด๋ฉด

https://medium.com/@abdelazimmohmmed/python-input-vulnerability-30b0bfea22c9

 

์‚ฌ์šฉ์ž๊ฐ€ input() ํ•จ์ˆ˜์— ์ž…๋ ฅํ•œ ๋ฐ์ดํ„ฐ๊ฐ€ ์ธ์‹๋˜์–ด ์‹คํ–‰์ด๋˜๋Š” ์ทจ์•ฝ์ ์ด ์กด์žฌํ•œ๋‹ค๋Š”๊ฑธ ์•Œ ์ˆ˜ ์žˆ๋‹ค.

ํ•ด๋‹น ์ทจ์•ฝ์ ์„ ์ด์šฉํ•ด system ํ•จ์ˆ˜๋ฅผ ํ†ตํ•ด ls ๋ช…๋ น์–ด๋ฅผ ์‹คํ–‰ํ•˜๋ฉด ์•„๋ž˜์ฒ˜๋Ÿผ ๋ช…๋ น์–ด๊ฐ€ ์ˆ˜ํ–‰์ด ๋œ๋‹ค.

์ด์ œ flag ํŒŒ์ผ์˜ ์œ„์น˜๋ฅผ ์ฐพ์•„์„œ flag๋ฅผ ํ™•์ธํ•˜๋ฉด ๋œ๋‹ค.

 

- Mitigation

input() ํ•จ์ˆ˜๋ฅผ ํ†ตํ•ด ์‚ฌ์šฉ์ž๊ฐ€ ์ž…๋ ฅํ•œ ๊ฐ’์ด ๊ทธ๋Œ€๋กœ ํ•ด์„์ด๋˜๋Š” ๋ถ€๋ถ„์ด ์ทจ์•ฝํ•˜๋‹ค.

ํ•ด๋‹น ํ•จ์ˆ˜๋ฅผ raw_input() ํ•จ์ˆ˜๋กœ ๋ณ€๊ฒฝํ•˜์—ฌ ์‚ฌ์šฉ์ž ์ž…๋ ฅ์„ str์œผ๋กœ๋งŒ ๋ฐ›์„ ์ˆ˜ ์žˆ๋„๋ก ํ•œ๋‹ค.