由于有几个内置的函数,这个开发者可以使用Python轻松地解决一个复杂的难题。继续阅读,看看如何!
在我第一次CTF(夺旗)比赛中,我注意到一些挑战需要角色转换才能发现一个字符串的真实价值。我花了几分钟的时间试图找出一个可以用来“揭露”给定内容的模式。
挑战1
收集有关给定IP地址的信息后,找到一个文件,其中包含以下内容:
rxms {} DqhqdeqpRxms它看起来完全是随机的,但是鉴于这些标志有一个模式:“flag {some-hash-here}”,我们可以尝试一个字符转换:
rxms{DqhqdeqpRxms}synttzouuapvvbqwwcrxxdsyyetzzfuaagvbbhwccixddjyeekzfflag{...} <- real content
我们不得不在字母表中跳12个位置来获取字符串的真实内容。
挑战2
为了连接到一个服务,我们必须解决密码短语:
AQW UJCNN PQV RCUU乍一看,密码短语看起来像凯撒的密码。解决这个挑战也需要改变角色,但是这次倒退了:
AQW UJCNN PQV RCUUZPV TIBMM OPU QBTTYOU SHALL NOT PASS <- backward content
Python的救援
Python有内置的函数ord()和chr()可以帮助我们完成这个任务:
class Alphanumeric(object): ALPHABET_LENGTH = 26 def __init__(self, nrange=None): self.current_letter = 'z' self.current_number = 0 self.nrange = nrange def forward_letter(self, letter, positions): if letter.islower(): unicode_point = ord('a') else: unicode_point = ord('A') start = ord(letter) - unicode_point offset = ((start + positions) % self.ALPHABET_LENGTH) + unicode_point self.current_letter = chr(offset) return self.current_letter def backward_letter(self, letter, positions): if letter.islower(): unicode_point = ord('a') else: unicode_point = ord('A') start = ord(letter) - unicode_point offset = ((start - positions) % self.ALPHABET_LENGTH) + unicode_point self.current_letter = chr(offset) return self.current_letter def next_letter(self): return self.forward_letter(self.current_letter, 1) def previous_letter(self): return self.backward_letter(self.current_letter, 1) def forward_number(self, number, positions): if not self.nrange: self.current_number = number + positions return self.current_number index = self.nrange.index(number) start = index + positions offset = (start % len(self.nrange)) self.current_number = self.nrange[offset] return self.current_number def backward_number(self, number, positions): if not self.nrange: return number - positions index = self.nrange.index(number) start = index - positions offset = (start % len(self.nrange)) self.current_number = self.nrange[offset] return self.current_number def next_number(self): return self.forward_number(self.current_number, 1) def previous_number(self): return self.backward_number(self.current_number, 1) def forward_alphanumeric(self, alpha, positions, ignore_numbers=False, ignore_letters=False): result = "" for char in alpha: if char.isdigit() and not ignore_numbers: char = str(self.forward_number(int(char), positions)) if char.isalpha() and not ignore_letters: char = self.forward_letter(char, positions) result += char return result def backward_alphanumeric(self, alpha, positions, ignore_numbers=False, ignore_letters=False): result = "" for char in alpha: if char.isdigit() and not ignore_numbers: char = str(self.backward_number(int(char), positions)) if char.isalpha() and not ignore_letters: char = self.backward_letter(char, positions) result += char return result
使用字母数字类:
alpha = Alphanumeric()print alpha.forward_alphanumeric('abc123', 1)print alpha.backward_alphanumeric('abc123', 1)// prints: bcd234// prints: zab012
请注意,您可以定义一个数字范围,以便在移动数字时执行“循环循环”:
alpha = Alphanumeric([1, 2, 3, 4 , 5])print alpha.forward_alphanumeric('123', 6)// prints: 234
一个CLI工具的救援
考虑下一个CTF的挑战,我写了一个名为shift的小CLI工具,可以更容易地转换字母数字字符。
echo "Dqhqdeqp Oazfqzf" | python shift.py -p 12 --backwards// prints: "Reversed Content"python shift.py abc123 -p 5// prints: "fgh678"
您可以在GitHub上看到文档和源代码。