★CTF:Python脚本记录

字典生成

生成数字字典:包括规定起始、终止和步长

def printnumber(path):
    a = eval(input("输入起始数"))
    b = eval(input("输入最后一个数"))
    c = eval(input("输入步长"))
    with open(path, 'w') as f:
        for i in range(a, b+1, c):
            f.write(str(i)+"\n")

生成固定位数的数字字典:比如四位1——0001

def printnumber_for_2FA(path):  # 类似于0001这种验证码
    with open(path, 'w') as f:
        a = eval(input("输入起始数"))
        b = eval(input("输入最后一个数"))
        c = eval(input("输入验证码长度"))
        for i in range(a,b+1):
            if len(str(i))<c:
                s="0"*(c-len(str(i)))+str(i)
                f.write(s+"\n")
            else:
                f.write(str(i)+"\n")

功能实现

循环zip解压缩:适用于压缩文件里面有压缩文件的情况

这是一个循环解压缩的脚本
主要是今天遇到了一个杂项题目要求循环解压缩

import zipfile
number=516 #初始被压缩次数是516次
for i in range(516):
    path="./518/1/"+str(number)+".zip" #压缩文件被保存的目录
    zip_file=zipfile.ZipFile(path)
    zip_list=zip_file.namelist()
    for f in zip_list:
        zip_file.extract(f,"./518/1/") #解压后生成的目录
    number=number-1
    if number==1:
        break

zip_file.close() #文件需要关闭

提取文件里面的IP地址、去重复、nslookup查询

import fileinput
import re
import os
import shutil
def readIP():
    with open(r'honeypot.log','r') as f:
        for line in f.readlines():
            result2=re.findall('[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}',line)
            if not result2==[]:
                result=result2[0]+'\n'
            with open('ip.txt','a+') as w:
                w.write(result);
def setIp():
    a=0
    readDir='ip.txt'
    writeDir='newip.txt'
    lines_seen=set()
    outfile=open(writeDir,"w");
    f=open(readDir,"r")
    for line in f:
        if line not in lines_seen:
            a+=1
            outfile.write(line)
            lines_seen.add(line)
        print(a)
    outfile.close()

def readDNS():
    with open(r'newip.txt','r') as g:
        for i in g.readlines():
            com=os.popen('nslookup %s'%i)
            comm=com.read();
            if comm.find('NXDOMAIN')==-1:
                print(comm)

if __name__=='__main__':
    readIP()
    setIp()
    readDNS()

十进制转ASCII:多个转换用","隔开

s=map(int,input().split(","))
print(s)
l=[]
for i in s:
    l.append(chr(i))

for i in l:
    print(i,end="")

将黑白图片生成01串并解密:来自攻防世界misc的gif

flag_dic = ""

white = open(r"C:\Users\Zeker62\Desktop\dbbc971bf4da461fb8939ed8fc9c4c9d\gif\0.jpg","rb").read()
black = open(r"C:\Users\Zeker62\Desktop\dbbc971bf4da461fb8939ed8fc9c4c9d\gif\1.jpg","rb").read()
#上面两句用于为下面的图片识别做个实例,定义什么图片是白色,什么是黑色

for i in range(104):
    with open(r"C:\Users\Zeker62\Desktop\dbbc971bf4da461fb8939ed8fc9c4c9d\gif\%d.jpg"%i,"rb") as f:
        if f.read() == white:
            flag_dic += "0"
        else:
            flag_dic += "1"
#上面循环语句用于识别D:\攻防世界\Misc\gif\gif\%d.jpg路径下的104张图片的颜色,并给白色附值为0,黑色赋值为1

#print (flag_dic)
#用于测试打印识别后的二进制

flag = ""

for i in range(len(flag_dic)//8):
    flag += chr(int(flag_dic[i*8:(i+1)*8],2))

print(flag)
#将二进制转化为字符串

截取两个字符、16进制转为10进制、ASCII编码——来自misc:掀桌子

import re
s="c8e9aca0c6f2e5f3e8c4efe7a1a0d4e8e5a0e6ece1e7a0e9f3baa0e8eafae3f9e4eafae2eae4e3eaebfaebe3f5e7e9f3e4e3e8eaf9eaf3e2e4e6f2"
b=re.findall(r'.{2}',s)#截取两个字符的正则表达
print(b)
flag=""
for i in b:
    flag+=chr(int(int(i,16)-128))
print(flag)

密码破解

二进制生成摩斯密码并解码

CODE_TABLE = {
    # 26 个英文字符
    'A': '.-', 'B': '-...', 'C': '-.-.',
    'D': '-..', 'E': '.', 'F': '..-.',
    'G': '--.', 'H': '....', 'I': '..',
    'J': '.---', 'K': '-.-', 'L': '.-..',
    'M': '--', 'N': '-.', 'O': '---',
    'P': '.--.', 'Q': '--.-', 'R': '.-.',
    'S': '...', 'T': '-', 'U': '..-',
    'V': '...-', 'W': '.--', 'X': '-..-',
    'Y': '-.--', 'Z': '--..',

    # 10 个数字
    '0': '-----', '1': '.----', '2': '..---',
    '3': '...--', '4': '....-', '5': '.....',
    '6': '-....', '7': '--...', '8': '---..',
    '9': '----.',

    # 16 个特殊字符
    ',': '--..--', '.': '.-.-.-', ':': '---...', ';': '-.-.-.',
    '?': '..--..', '=': '-...-', "'": '.----.', '/': '-..-.',
    '!': '-.-.--', '-': '-....-', '_': '..--.-', '(': '-.--.',
    ')': '-.--.-', '$': '...-..-', '&': '. . . .', '@': '.--.-.'

}

def morsedecode(morse):
    msg =''
    codes = morse.split(' ')
    for code in codes:
        if code =='':
            msg += ' '
        else:
            UNCODE =dict(map(lambda t:(t[1],t[0]),CODE_TABLE.items()))
            msg += UNCODE[code]
    return msg

c = "11 111 010 000 0 1010 111 100 0 00 000 000 111 00 10 1 0 010 0 000 1 00 10 110"

c = c.replace('1','-')
c = c.replace('0','.')

FLAG = morsedecode(c)
#转换为小写
flag = FLAG.lower()
#加壳拿flag
flag = 'cyberpeace{'+flag+'}'
print('flag is ',flag)

base64 隐写加密与解密 python2

隐写加密

# -*- coding: cp936 -*-
import base64

flag = 'Tr0y{Base64isF4n}' #flag
bin_str = ''.join([bin(ord(c)).replace('0b', '').zfill(8) for c in flag])

base64chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'

with open('0.txt', 'rb') as f0, open('1.txt', 'wb') as f1: #'0.txt'是明文, '1.txt'用于存放隐写后的 base64
    for line in f0.readlines():
        rowstr = base64.b64encode(line.replace('\n', ''))
        equalnum = rowstr.count('=')

        if equalnum and len(bin_str):
            offset = int('0b'+bin_str[:equalnum * 2], 2)
            char = rowstr[len(rowstr) - equalnum - 1]
            rowstr = rowstr.replace(char, base64chars[base64chars.index(char) + offset])
            bin_str = bin_str[equalnum*2:]

        f1.write(rowstr + '\n')

隐写解密

# -*- coding: cp936 -*-
import base64

flag = 'Tr0y{Base64isF4n}' #flag
bin_str = ''.join([bin(ord(c)).replace('0b', '').zfill(8) for c in flag])

base64chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'

with open('0.txt', 'rb') as f0, open('1.txt', 'wb') as f1: #'0.txt'是明文, '1.txt'用于存放隐写后的 base64
    for line in f0.readlines():
        rowstr = base64.b64encode(line.replace('\n', ''))
        equalnum = rowstr.count('=')

        if equalnum and len(bin_str):
            offset = int('0b'+bin_str[:equalnum * 2], 2)
            char = rowstr[len(rowstr) - equalnum - 1]
            rowstr = rowstr.replace(char, base64chars[base64chars.index(char) + offset])
            bin_str = bin_str[equalnum*2:]

        f1.write(rowstr + '\n')

01248 云影密码解密

# 云影密码
a="8842101220480224404014224202480122"
s=a.split('0')
l=[]
print(s)
for i in s:
    sum=0
    for j in i:
        sum+=eval(j)
    l.append(chr(sum+64))
print(l)
posted @ 2021-09-11 19:38  Zeker62  阅读(601)  评论(0编辑  收藏  举报