前言:
安全工作中经常会遇到需要转码或加密的情况,比较好用的有Burp的Decoder模块
以及IBM APPSCAN的PowerTools中的编码/解码工具。
具体用法都非常简单。今天就用Python编写一个有GUI界面的简单的转码器,效果如下图:
代码:
#!/usr/bin/env python
# -*- coding: UTF-8 -*-
# @Time :2020/2/6 21:01
# @Author :Donvin.li
# @File :encodergui.py
import tkinter as tk
from urllib import parse
import base64
import binascii
from hashlib import md5
import html
'''
基本思路:
1、定义一个encode_type变量用于接收编码/解码方法;
2、定义Radiobutton控件用于选择编码/解码方法,Radiobutton控件每次选择将值传给encode_type;
3、定义两个Button按钮,一个用于编码选择函数:mian(),一个用于解码选择函数:un_mian();
4、定义一个ENTRY控件,用于接收用户输入,传给具体的编码函数;
5、定义一个TEXT控件,用于显示具体编码函数的输出结果;
6、定义编码选择函数mian(),在该函数中判断encode_type变量的值以选择具体的编码函数,如:encode_type='A'时,执行url_encoding();
7、定义具体的编码函数,如:url_encoding(),传入的值从ENTRY控件获得,输出的结果传入到TEXT控件进行显示;
8、定义解码选择函数un_mian(),在该函数中判断encode_type变量的值以选择具体的解码函数,如:encode_type='A'时,执行url_decoding();
9、定义具体的解码函数,如:url_decoding(),传入的值从ENTRY控件获得,输出的结果传入到TEXT控件进行显示;
'''
# 定义GUI主体框架
window=tk.Tk()
window.title('转码器')
window.geometry('810x355')
encode_type=tk.StringVar()
# 编码选择函数
def mian():
encode_type_value = encode_type.get()
if encode_type_value=='A':
url_encoding()
#l4.config(text='you have selected ' + encode_type_value)
elif encode_type_value=='B':
base64_encoding()
elif encode_type_value=='C':
hex_encoding()
elif encode_type_value=='D':
ascii_encoding()
elif encode_type_value=='E':
md5_encoding()
elif encode_type_value=='F':
unicode_encoding()
elif encode_type_value=='G':
htmlescape()
# URL编码
def url_encoding():
t2.delete('1.0', 'end') #用于清空TEXT框
string=e1.get()
reslut=parse.quote(string)
#return parse.quote(string)
#t2.insert('insert',reslut)
#l4.config(text='you have selected ' + reslut)
t2.insert(tk.INSERT,reslut)
# BASE64编码
def base64_encoding():
t2.delete('1.0', 'end')
string=e1.get()
str1=base64.b64encode(string.encode()) #base64.b64encode()传入的是bytes类型,输出的也是bytes类型
reslut=str1.decode()
t2.insert(tk.INSERT, reslut)
# HEX 十六进制编码
def hex_encoding():
t2.delete('1.0', 'end')
string=e1.get()
hex=binascii.b2a_hex(string.encode())
reslut='0x'+hex.decode()
t2.insert(tk.INSERT, reslut)
# Ascii编码
def ascii_encoding():
t2.delete('1.0', 'end')
string=e1.get()
reslut=list(map(ord,string)) #map(ord,string)函数是将string中的每个字符分别传给ord函数,转换成数字编号
t2.insert(tk.INSERT, reslut) #输出的是列表
# MD5加密
def md5_encoding():
t2.delete('1.0', 'end')
string=e1.get()
reslut=md5(string.encode()).hexdigest()
t2.insert(tk.INSERT, reslut)
# unicode编码函数
def unicode_encoding(): #传入的是中文
t2.delete('1.0', 'end')
string=e1.get()
reslut=string.encode('unicode_escape').decode()
t2.insert(tk.INSERT, reslut)
# HTML实体编码
def htmlescape():
t2.delete('1.0', 'end')
string=e1.get()
reslut=html.escape(string)
t2.insert(tk.INSERT, reslut)
# 解码选择函数
def un_mian():
encode_type_value = encode_type.get()
if encode_type_value=='A':
url_decoding()
#l4.config(text='you have selected ' + encode_type_value)
elif encode_type_value=='B':
base64_decoding()
elif encode_type_value=='C':
hex_decoding()
elif encode_type_value=='D':
ascii_decoding()
elif encode_type_value=='E':
md5_decoding()
elif encode_type_value=='F':
unicode_decoding()
elif encode_type_value=='G':
unhtmlescape()
# URL解码
def url_decoding():
t2.delete('1.0', 'end')
string=e1.get()
reslut=parse.unquote(string)
t2.insert(tk.INSERT, reslut)
# BASE64解码
def base64_decoding():
t2.delete('1.0', 'end')
string=e1.get()
str1=base64.b64decode(string.encode())
reslut=str1.decode()
t2.insert(tk.INSERT, reslut)
# HEX 十六进制解码
def hex_decoding():
t2.delete('1.0', 'end')
string=e1.get()
str1=string.replace('0x','') #字符串方法,将0x替换为空
str2=binascii.a2b_hex(str1.encode())
reslut=str2.decode()
t2.insert(tk.INSERT, reslut)
# Ascii解码
def ascii_decoding(): #输入的是列表,例如[116, 101, 115, 116]
t2.delete('1.0','end')
string=e1.get()
l1=eval(string)
str1=list(map(chr,l1))
reslut=''.join(str1) #join方法用''将所有字符连接起来
t2.insert(tk.INSERT, reslut)
# MD5解密
def md5_decoding():
t2.delete('1.0', 'end')
t2.insert(tk.INSERT,'你咋不上天呢')
# unicode解码函数
def unicode_decoding(): #传入的是Unicode编码,如:\u6d4b\u8bd5
t2.delete('1.0', 'end')
string=e1.get()
t2.insert(tk.INSERT, string)
# HTML实体解码
def unhtmlescape(string):
t2.delete('1.0', 'end')
string=e1.get()
reslut=html.unescape(string)
t2.insert(tk.INSERT, string)
l1=tk.Label(window,text='选择方式',font=('Arial',10))
l1.place(x=10,y=10)
r1 = tk.Radiobutton(window, text='URL', variable=encode_type, value='A')
r1.place(x=10,y=40)
r2 = tk.Radiobutton(window, text='BASE64', variable=encode_type, value='B')
r2.place(x=100,y=40)
r3 = tk.Radiobutton(window, text='HEX', variable=encode_type, value='C')
r3.place(x=200,y=40)
r4 = tk.Radiobutton(window, text='ASCII', variable=encode_type, value='D')
r4.place(x=300,y=40)
r5 = tk.Radiobutton(window, text='MD5', variable=encode_type, value='E')
r5.place(x=400,y=40)
r6 = tk.Radiobutton(window, text='UNICODE', variable=encode_type, value='F')
r6.place(x=500,y=40)
r7 = tk.Radiobutton(window, text='HTML', variable=encode_type, value='G')
r7.place(x=600,y=40)
b1 = tk.Button(window, text='编码', width=10,height=1, command=mian)
b1.place(x=10,y=70)
b2 = tk.Button(window, text='解码', width=10,height=1, command=un_mian)
b2.place(x=100,y=70)
l2=tk.Label(window,text='输入',font=('Arial',10))
l2.place(x=10,y=110)
# t1=tk.Text(window,width=111,height=8)
# t1.place(x=10,y=140)
# string=t1.get()
e1=tk.Entry(window,width=111,show = None) #显示成明文形式
e1.place(x=10,y=140)
l3=tk.Label(window,text='输出',font=('Arial',10))
l3.place(x=10,y=170)
# l4=tk.Label(window, bg='yellow', width=111, text='empty')
# l4.place(x=10,y=300)
t2=tk.Text(window,bg='green',fg='white',width=111, height=6)
# 说明: bg为背景,fg为字体颜色,font为字体,width为长,height为高,这里的长和高是字符的长和高,比如height=2,就是标签有2个字符这么高
t2.place(x=10,y=200)
l3=tk.Label(window,text='作者:李东锋',font=('Arial',10))
l3.place(x=350,y=330)
window.mainloop()
使用:
打包成exe双击执行即可:
pyinstaller -F -w encoding.py