美文网首页网络安全
54.实验吧——trival

54.实验吧——trival

作者: 扶桑啊啊 | 来源:发表于2018-09-13 09:14 被阅读0次

http://www.shiyanbar.com/ctf/1980

image.png
将源码下载下来,进行解读
#!/usr/bin/env python
import sys

alphaL = "abcdefghijklnmopqrstuvqxyz"
alphaU = "ABCDEFGHIJKLMNOPQRSTUVQXYZ"
num    = "0123456789"
keychars = num+alphaL+alphaU

if len(sys.argv) != 3:   #如果用户输入的参数不是三个的话,就输出本身文件路径,且结束程序
  print "Usage: %s SECRET_KEY PLAINTEXT"%(sys.argv[0])
  sys.exit()

key = sys.argv[1]  #key接收第二个参数
if not key.isalnum():   #如果第二个参数不是全数字与字母组合,则打印不合法,并结束程序
  print "Your key is invalid, it may only be alphanumeric characters"
  sys.exit()

plaintext = sys.argv[2]  #接收第三个参数

ciphertext = ""
for i in range(len(plaintext)):
  rotate_amount = keychars.index(key[i%len(key)])  #检测第二个参数的每一位,并返回与keychars匹配的位置
  if plaintext[i] in alphaL:    #遍历第三个参数的每一位,转为ASCII码,分别按照不同的加密方式进行加密,再转为字符型
    enc_char = ord('a') + (ord(plaintext[i])-ord('a')+rotate_amount)%26
  elif plaintext[i] in alphaU:
    enc_char = ord('A') + (ord(plaintext[i])-ord('A')+rotate_amount)%26
  elif plaintext[i] in num:
    enc_char = ord('0') + (ord(plaintext[i])-ord('0')+rotate_amount)%10
  else:
    enc_char = ord(plaintext[i])
  ciphertext = ciphertext + chr(enc_char)

print "Encryption complete, ENC(%s,%s) = %s"%(plaintext,key,ciphertext)

按照原题的意思,key=T0pS3cre7key,ciphertext=Bot kmws mikferuigmzf rmfrxrwqe abs perudsf! Nvm kda ut ab8bv_w4ue0_ab8v_DDU,所以我们需要反求plaintext的值
解题思路:
1.将字符串转为ASCII码
2.len(key)=12


image.png

可以看到rotate_amount值一共有12个,会一直循环下去。所以解码配置rotate_amount值时只配置12个。
解码代码:

#!/usr/bin/env python
import sys

alphaL = "abcdefghijklnmopqrstuvqxyz"
alphaU = "ABCDEFGHIJKLMNOPQRSTUVQXYZ"
num    = "0123456789"
keychars = num+alphaL+alphaU

key='T0pS3cre7key'
ciphertext = "Bot kmws mikferuigmzf rmfrxrwqe abs perudsf! Nvm kda ut ab8bv_w4ue0_ab8v_DDU"
rotate_amount=[]
for i in key:
  rotate_amount.append(keychars.index(i))
print rotate_amount
plain_text=''
for j in range(len(ciphertext)):
  if ciphertext[j] in alphaL:
    for i in range(26):
      if (rotate_amount[j%12]+i)%26==(ord(ciphertext[j])-ord('a')):
         plain_text=plain_text+chr(ord('a')+i)
  elif ciphertext[j] in alphaU:
    for i in range(26):
      if (rotate_amount[j%12]+i)%26==(ord(ciphertext[j])-ord('A')):
          plain_text=plain_text+chr(ord('A')+i)
  elif ciphertext[j] in num:
    for i in range(10):
      if (rotate_amount[j%12]+i)%10==(ord(ciphertext[j])-ord('0')):
         plain_text=plain_text+chr(ord('0')+i)
  else:
      plain_text=plain_text+ciphertext[j]
                                      
print(plain_text)
image.png

得到flag:th4ts_w0rs3_th4n_DES

难点在于取余数的逆运算,且得琢磨一会儿

相关文章

网友评论

    本文标题:54.实验吧——trival

    本文链接:https://www.haomeiwen.com/subject/zdnvgftx.html