1
+ import tkinter as tk
2
+ from tkinter import ttk
3
+ from tkinter import filedialog
4
+ from tkinter import messagebox
5
+ from tkinter .font import Font
6
+ import os
7
+
8
+ try :
9
+ from pypdf import PdfReader ,PdfWriter
10
+ except ImportError as e :
11
+ messagebox .showerror ("运行失败" ,"没有下载依赖项" )
12
+ print (e )
13
+ print ("正在下载工具依赖项" )
14
+ os .system ("pip install pypdf" )
15
+
16
+ def pdf_js_inject (template_file_path ,save_path ,js_code :str = None ,js_file :str = None ,encrypt_password = None ):
17
+ output_pdf = PdfWriter ()
18
+ temp_pdf = PdfReader (template_file_path )
19
+
20
+ for i in range (len (temp_pdf .pages )):
21
+ page = temp_pdf .pages [i ]
22
+ output_pdf .add_page (page )
23
+
24
+ if encrypt_password is not None :
25
+ output_pdf .encrypt (encrypt_password ,algorithm = 'AES-256' )
26
+ # print("encrypt success")
27
+ try :
28
+ with open (save_path ,'wb' ) as evil_pdf :
29
+ if js_code is not None :
30
+ output_pdf .add_js (js_code )
31
+ if js_file is not None :
32
+ with open (js_file ,'r' ) as js :
33
+ output_pdf .add_js (js .read ())
34
+ output_pdf .write (evil_pdf )
35
+
36
+ messagebox .showinfo ("注入成功" ,"注入成功" )
37
+ except :
38
+ messagebox .showerror ("注入失败" ,"注入失败" )
39
+
40
+ # print("\npdf inject js success\n")
41
+
42
+ class Application (tk .Tk ):
43
+ def __init__ (self , screenName : str | None = None , baseName : str | None = None , className : str = "Tk" , useTk : bool = True , sync : bool = False , use : str | None = None ) -> None :
44
+ super ().__init__ (screenName , baseName , className , useTk , sync , use )
45
+
46
+ self .custom_font = Font (family = "Microsoft YaHei UI" , size = 11 )
47
+ self .option_add ("*Font" ,self .custom_font )
48
+
49
+ self .menu_bar = tk .Menu (self )
50
+ sub_about_menu = tk .Menu (self .menu_bar ,tearoff = 0 )
51
+ sub_about_menu .add_cascade (label = "作者" ,command = lambda :messagebox .showinfo ("作者" ,"柚木梨酱" ))
52
+ sub_about_menu .add_cascade (label = "版本" ,command = lambda :messagebox .showinfo ("版本" ,"pdf-js-inject-gui:1.0" ))
53
+ sub_about_menu .add_cascade (label = "退出" ,command = lambda :exit (1 ))
54
+ self .tab = self .menu_bar .add_cascade (label = "about" ,menu = sub_about_menu )
55
+
56
+ self .grid_columnconfigure (index = 1 ,pad = "20" )
57
+
58
+
59
+ self .config (menu = self .menu_bar )
60
+
61
+ self .cheak_var = tk .IntVar (value = 0 )
62
+ self .pdf_encrypt_passwd = None
63
+
64
+ self .labal_1 = ttk .Label (self ,text = "请输入pdf模板路径:" )
65
+ self .entry_1 = ttk .Entry (self )
66
+ self .labal_2 = ttk .Label (self ,text = "请输入pdf保存路径:" )
67
+ self .entry_2 = ttk .Entry (self )
68
+ self .button_1 = ttk .Button (self ,text = "请选择pdf模板文件的路径" ,command = self .button_get_file_path )
69
+ self .button_2 = ttk .Button (self ,text = "请选择保存pdf的路径" ,command = self .button_save_file_path )
70
+ self .checkbutton = ttk .Checkbutton (self ,text = "加密pdf" ,command = self .encrypt_pdf ,variable = self .cheak_var )
71
+ # self.message = tk.Message(self,width=300,bg="red")
72
+ self .text = tk .Text (self ,width = "52" ,height = "10" )
73
+ self .text .insert ("1.0" ,"请输入要注入的js" )
74
+
75
+ def focusin (event ):
76
+ if self .text .get ('1.0' ,'end-1c' ) == "请输入要注入的js" :
77
+ self .text .delete ("0.0" ,tk .END )
78
+
79
+ def focusout (event ):
80
+ if self .text .get ('1.0' ,'end-1c' ) == "" :
81
+ self .text .insert ("0.0" ,"请输入要注入的js" )
82
+
83
+ self .text .bind ("<FocusIn>" ,focusin )
84
+ self .text .bind ("<FocusOut>" ,focusout )
85
+
86
+ self .button_3 = ttk .Button (self ,text = "请选择想输入的js文件" ,command = self .inject_js_file )
87
+ self .button_4 = ttk .Button (self ,text = "注入js" ,command = self .start_inject_js )
88
+
89
+
90
+ self .labal_1 .grid (row = 0 ,column = 0 ,sticky = "w" ,pady = "10" )
91
+ self .labal_2 .grid (row = 1 ,column = 0 ,sticky = "w" )
92
+ self .entry_1 .grid (row = 0 ,column = 1 ,sticky = "w" )
93
+ self .entry_2 .grid (row = 1 ,column = 1 ,sticky = "w" )
94
+ self .button_1 .grid (row = 0 ,column = 2 ,sticky = "w" )
95
+ self .button_2 .grid (row = 1 ,column = 2 ,sticky = "w" )
96
+ self .checkbutton .grid (row = 2 ,column = 0 ,sticky = "w" )
97
+ # self.text.grid(row=3,column=0,sticky="nsew")
98
+
99
+ self .text .place (x = 10 ,y = 110 )
100
+ self .button_3 .place (x = 10 ,y = 320 )
101
+ self .button_4 .place (x = 10 ,y = 360 )
102
+
103
+ def button_get_file_path (self ):
104
+ file_name = filedialog .askopenfilename (title = "请选择pdf模板路径" ,filetypes = [('PDF file' ,'*.pdf' )])
105
+ self .entry_1 .delete (0 ,tk .END )
106
+ self .entry_1 .insert (0 ,file_name )
107
+
108
+ def button_save_file_path (self ):
109
+ file_name = filedialog .asksaveasfilename (title = "请选择pdf保存路径" ,filetypes = [("PDF file" ,"*.pdf" )],defaultextension = ".pdf" )
110
+ self .entry_2 .delete (0 ,tk .END )
111
+ self .entry_2 .insert (0 ,file_name )
112
+
113
+ def encrypt_pdf (self ):
114
+ if self .cheak_var .get () == 1 :
115
+ self .encrypt_pdf_entry = ttk .Entry (self )
116
+ self .encrypt_pdf_entry .grid (row = 2 ,column = 1 )
117
+ self .encrypt_pdf_entry .insert (0 ,"请输入密码" )
118
+
119
+ def focusin (event ):
120
+ if self .encrypt_pdf_entry .get () == "请输入密码" :
121
+ self .encrypt_pdf_entry .delete (0 ,tk .END )
122
+
123
+ def focusout (event ):
124
+ if self .encrypt_pdf_entry .get () == "" :
125
+ self .encrypt_pdf_entry .insert (0 ,"请输入密码" )
126
+ else :
127
+ self .pdf_encrypt_passwd = self .encrypt_pdf_entry .get ()
128
+
129
+ self .encrypt_pdf_entry .bind ("<FocusIn>" ,focusin )
130
+ self .encrypt_pdf_entry .bind ("<FocusOut>" ,focusout )
131
+
132
+
133
+ else :
134
+ if hasattr (self , "encrypt_pdf_entry" ): # 检查组件是否存在
135
+ self .encrypt_pdf_entry .grid_forget ()
136
+ self .pdf_encrypt_passwd = None
137
+
138
+ def inject_js_file (self ):
139
+
140
+ js_file_path = filedialog .askopenfilename (filetypes = [("js_inject_file" ,["*.js" ,"*.txt" ])])
141
+ with open (js_file_path ,"r" ,encoding = "utf-8" ) as js_file :
142
+ js_code = js_file .read ()
143
+ if self .text .get ("1.0" ,"end-1c" ) != "" :
144
+ self .text .delete ("1.0" ,tk .END )
145
+ if self .text .get ("1.0" ,"end-1c" ) == "" :
146
+ self .text .insert ("1.0" ,js_code )
147
+
148
+ def start_inject_js (self ):
149
+ template_pdf = self .entry_1 .get ()
150
+ save_path = self .entry_2 .get ()
151
+ js_code = self .text .get ("0.0" ,"end-1c" )
152
+ pdf_js_inject (template_pdf ,save_path ,encrypt_password = self .pdf_encrypt_passwd ,js_code = js_code )
153
+ # print("passwd:",self.pdf_encrypt_passwd)
154
+
155
+
156
+
157
+ if __name__ == '__main__' :
158
+ app = Application ()
159
+ app .title ("pdf-js-inject-gui" )
160
+ app .wm_geometry ('500x400+100+100' )
161
+ app .wm_iconphoto (False ,tk .PhotoImage (file = r'pdf-js-inject\icon\hacker.png' ))
162
+ try :
163
+ app .mainloop ()
164
+ except KeyboardInterrupt :
165
+ os ._exit (1 )
166
+ pass
0 commit comments