-
Notifications
You must be signed in to change notification settings - Fork 0
/
stream-pdf.py
executable file
·293 lines (252 loc) · 14.8 KB
/
stream-pdf.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
from colorama import Fore, Style
import base64
import zlib
import os
import re
# Fichier PDF par défaut :
default_pdf_file = 'bac2004.pdf' # A modifier par le nom du fichier PDF à analyser pour faciliter l'utilisation du script (appuyer sur Entrée pour utiliser le fichier par défaut)
def print_banner():
banner = """ ______ ___ ___ ____
/ __/ /________ ___ ___ _ / _ \/ _ \/ __/
_\ \/ __/ __/ -_) _ `/ ' \ / ___/ // / _/
/___/\__/_/ \__/\_,_/_/_/_/ /_/ /____/_/ """
info = """
[+] Github : https://github.com/CalValmar
[+] Auteur : Valmar
[+] Version : 1.0 """
print(Fore.GREEN + Style.BRIGHT + banner)
print(Fore.LIGHTYELLOW_EX + Style.BRIGHT + info)
# Fonction pour clear le terminal
def clear_screen():
os.system('cls' if os.name == 'nt' else 'clear')
# Fonction pour supprimer les fichiers
def remove_files():
for file in os.listdir('generated_files'):
os.remove(os.path.join('generated_files', file))
for file in os.listdir('objects_streams_list'):
os.remove(os.path.join('objects_streams_list', file))
# Fonction pour afficher la liste des objets / streams
def object_stream_list(pdf_file):
with open(pdf_file, 'rb') as f:
content = f.read()
list_obj = []
for i, obj in enumerate(content.split(b'endobj')):
if b'obj' in obj:
list_obj.append(i + 1)
print(Fore.LIGHTCYAN_EX + Style.BRIGHT + f"\n Liste des objets dans '{pdf_file}' :" + Fore.LIGHTYELLOW_EX + Style.BRIGHT + f"\n {list_obj}")
list_stream = []
for i, obj in enumerate(content.split(b'endobj')):
if b'stream' in obj:
list_stream.append(i + 1)
print(Fore.LIGHTCYAN_EX + Style.BRIGHT + f"\n Liste des stream dans '{pdf_file}' :" + Fore.LIGHTYELLOW_EX + Style.BRIGHT + f"\n {list_stream}")
# Enregistrer la liste des objets / streams dans un le fichier '/objects_streams_list/{pdf_file}_list.txt'
if not os.path.exists(f"objects_streams_list/{pdf_file}_list.txt"):
with open(os.path.join('objects_streams_list', f'{pdf_file}_list.txt'), 'w') as f:
f.write(f"Liste des objets dans '{pdf_file}' :\n {list_obj}\n\nListe des stream dans '{pdf_file}' :\n {list_stream}")
print(Fore.LIGHTGREEN_EX + Style.BRIGHT + f"\n [+] Liste des objets / streams enregistrée dans" + Fore.LIGHTYELLOW_EX + Style.BRIGHT + f" objects_streams_list/{pdf_file}_list.txt")
# Fonction pour extraire les objets / streams
def extract_object_stream(pdf_file, object_stream_number, output_file):
try:
# Ouvrir le fichier PDF en mode binaire
with open(pdf_file, 'rb') as f:
content = f.read()
# Séparer les objets Stream
object_streams = content.split(b'endobj')
if object_stream_number == -1:
# Extraction de tous les objets
with open(output_file, 'wb') as out_f:
for obj in object_streams:
out_f.write(obj + b'endobj\n')
print(Fore.LIGHTGREEN_EX + Style.BRIGHT + f"\n [+] Tous les objets / streams ont été extraits avec succès dans" + Fore.LIGHTYELLOW_EX + Style.BRIGHT + f" {output_file}")
elif object_stream_number >= 1 and object_stream_number <= len(object_streams):
# Récupérer l'objet Stream demandé
object_stream = object_streams[object_stream_number - 1] + b'endobj\n'
with open(output_file, 'wb') as out_f:
out_f.write(object_stream)
print(Fore.LIGHTGREEN_EX + Style.BRIGHT + f"\n [+] Objet / Stream {object_stream_number} extrait avec succès dans" + Fore.LIGHTYELLOW_EX + Style.BRIGHT + f" {output_file}")
else:
print(Fore.LIGHTRED_EX + Style.BRIGHT + f"\n [-] L'objet / Stream {object_stream_number} n'existe pas dans ce PDF.")
except FileNotFoundError:
print(Fore.LIGHTRED_EX + Style.BRIGHT + f"\n [-] Le fichier PDF '{pdf_file}' n'a pas été trouvé.")
except Exception as e:
print(Fore.LIGHTRED_EX + Style.BRIGHT + f"\n [-] Une erreur s'est produite : {str(e)}")
# Fonction pour extraire le FlateDecode
def extract_flatedecode(pdf_file, output_file):
# Récupérer les objets Stream FlateDecode
stream = re.compile(rb'.*?FlateDecode.*?stream(.*?)endstream', re.S)
# Extraire les objets Stream FlateDecode
for i, s in enumerate(stream.findall(open(pdf_file, 'rb').read())):
s = s.strip(b'\r\n')
try:
with open(os.path.join('generated_files', output_file), 'wb') as f:
f.write(zlib.decompress(s))
if i == 0:
print(Fore.LIGHTGREEN_EX + Style.BRIGHT + f"\n [+] Objet / Stream extrait avec succès dans" + Fore.LIGHTYELLOW_EX + Style.BRIGHT + f" generated_files/{output_file}")
print(Fore.LIGHTBLACK_EX + Style.BRIGHT + " Cela peut prendre quelques secondes...")
else:
continue
except zlib.error:
print(Fore.LIGHTRED_EX + Style.BRIGHT + "\n [-] Le contenu n'est pas compressé avec FlateDecode.")
except Exception as e:
print(Fore.LIGHTRED_EX + Style.BRIGHT + f"\n [-] Une erreur s'est produite : {str(e)}")
# Fonction pour décoder le contenu du Flatecode en base64
def decode_flatedecode(pdf_file, outpout_file):
try:
# Décoder le contenu en base64
decoded_content = base64.b64decode(open(pdf_file, 'rb').read())
with open(os.path.join('generated_files', outpout_file), 'wb') as out_f:
out_f.write(decoded_content)
print(Fore.LIGHTGREEN_EX + Style.BRIGHT + "\n [+] Décodé avec succès dans" + Fore.LIGHTYELLOW_EX + Style.BRIGHT + f" generated_files/{outpout_file}")
except FileNotFoundError:
print(Fore.LIGHTRED_EX + Style.BRIGHT + "\n [-] Le fichier PDF n'a pas été trouvé.")
except Exception as e:
print(Fore.LIGHTRED_EX + Style.BRIGHT + f"\n [-] Une erreur s'est produite : {str(e)}")
# Fonction principale
def main():
print_banner()
while True:
print(Fore.LIGHTYELLOW_EX + Style.BRIGHT + "\n Sélectionnez le type d'extraction :")
print(Fore.LIGHTCYAN_EX + Style.BRIGHT + "\n [1] ➙ Afficher tous les objets/streams")
print(" [2] ➙ Extraire tous les objets/streams")
print(" [3] ➙ Extraire un objets/streams spécifique")
print(" [4] ➙ Extraire le FlateDecode")
print(" [5] ➙ Supprimer les fichiers générés")
print(" [6] ➙ Quitter le programme")
extraction_type = input(Fore.GREEN + "\n Option : ")
# [1] Afficher tous les objets Stream
if extraction_type == '1':
pdf_file = input(Fore.GREEN + " Entrez le nom du fichier PDF à analyser : ")
if pdf_file == '':
pdf_file = default_pdf_file
if not os.path.exists("objects_streams_list"):
os.makedirs("objects_streams_list")
clear_screen(), object_stream_list(pdf_file)
choice = input(Fore.LIGHTBLACK_EX + "\n Appuyez sur Entrée pour continuer...")
if choice == '':
clear_screen(), print_banner()
continue
else:
break
# [2] Extraire tous les objets Stream
elif extraction_type == '2':
pdf_file = input(Fore.GREEN + " Entrez le nom du fichier PDF à analyser : ")
if pdf_file == '':
pdf_file = default_pdf_file
extract_object_stream(pdf_file, -1, os.path.join('generated_files', 'all_objects.txt'))
choice = input(Fore.LIGHTBLACK_EX + "\n Appuyez sur Entrée pour continuer...")
if choice == '':
clear_screen(), print_banner()
continue
else:
break
# [3] Extraire un objet Stream spécifique
elif extraction_type == '3':
pdf_file = input(Fore.GREEN + " Entrez le nom du fichier PDF à analyser : ")
if pdf_file == '':
pdf_file = default_pdf_file
object_stream_number = input(Fore.GREEN + " Entrez le numéro de l'objet / stream à extraire : ")
try:
object_stream_number = int(object_stream_number)
except ValueError:
print(Fore.LIGHTRED_EX + Style.BRIGHT + "\n [-] Le numéro de l'objet / stream doit être un entier.")
choice = input(Fore.LIGHTBLACK_EX + "\n Appuyez sur Entrée pour continuer...")
clear_screen(), print_banner()
continue
if object_stream_number <= 0:
print(Fore.LIGHTRED_EX + Style.BRIGHT + "\n [-] Le numéro de l'objet / stream doit être supérieur à zéro.")
choice = input(Fore.LIGHTBLACK_EX + "\n Appuyez sur Entrée pour continuer...")
clear_screen(), print_banner()
continue
if object_stream_number > len(open(pdf_file, 'rb').read().split(b'endobj')):
print(Fore.LIGHTRED_EX + Style.BRIGHT + "\n [-] Le numéro de l'objet / stream est supérieur au nombre d'objets / stream dans ce PDF.")
choice = input(Fore.LIGHTBLACK_EX + "\n Appuyez sur Entrée pour continuer...")
clear_screen(), print_banner()
continue
try:
extract_object_stream(pdf_file, object_stream_number, os.path.join('generated_files', f'object_stream_{object_stream_number}.txt'))
# Demander à l'utilisateur s'il veut visualiser le contenu de l'objet Stream
while True:
print(Fore.LIGHTYELLOW_EX + Style.BRIGHT + "\n Voulez-vous visualiser le contenu de l'objet / stream ?")
print(Fore.LIGHTCYAN_EX + Style.BRIGHT + "\n [1] ➙ Oui")
print(" [2] ➙ Non")
choice = input(Fore.GREEN + "\n Option : ")
if choice == '1':
try:
print(Fore.LIGHTYELLOW_EX + Style.BRIGHT + f"\n\n Contenu de l'objet / stream {object_stream_number} :" + Fore.LIGHTCYAN_EX + Style.BRIGHT + "\n" + "-" * 80 + Fore.WHITE + Style.BRIGHT + open(os.path.join('generated_files', f'object_stream_{object_stream_number}.txt'), 'r').read() + Fore.LIGHTCYAN_EX + Style.BRIGHT + "\n" + "-" * 80)
choice = input(Fore.LIGHTBLACK_EX + "\n Appuyez sur Entrée pour continuer...")
clear_screen(), print_banner()
break
except UnicodeDecodeError:
print(Fore.LIGHTRED_EX + Style.BRIGHT + f"\n [-] Impossible d'afficher le contenu de l'objet / stream {object_stream_number}.")
choice = input(Fore.LIGHTBLACK_EX + "\n Appuyez sur Entrée pour continuer...")
clear_screen(), print_banner()
break
except FileNotFoundError:
print(Fore.LIGHTRED_EX + Style.BRIGHT + f"\n [-] Le fichier 'object_stream_{object_stream_number}.txt' n'a pas été trouvé.")
choice = input(Fore.LIGHTBLACK_EX + "\n Appuyez sur Entrée pour continuer...")
clear_screen(), print_banner()
break
elif choice == '2':
clear_screen(), print_banner()
break
else:
clear_screen()
print(Fore.RED + Style.BRIGHT + "\n [-] Veuillez entrer 1 ou 2 pour sélectionner une option.")
except FileNotFoundError:
print(Fore.LIGHTRED_EX + Style.BRIGHT + "\n [-] Le fichier PDF n'a pas été trouvé.")
choice = input(Fore.LIGHTBLACK_EX + "\n Appuyez sur Entrée pour continuer...")
clear_screen(), print_banner()
continue
except Exception as e:
print(Fore.LIGHTRED_EX + Style.BRIGHT + f"\n [-] Une erreur s'est produite : {str(e)}")
choice = input(Fore.LIGHTBLACK_EX + "\n Appuyez sur Entrée pour continuer...")
clear_screen(), print_banner()
continue
# [4] Extraire le flatecode du pdf
elif extraction_type == '4':
pdf_file = input(Fore.GREEN + " Entrez le nom du fichier PDF à analyser : ")
if pdf_file == '':
pdf_file = default_pdf_file
extract_flatedecode(pdf_file, 'raw_flatedecode.txt')
# Demander à l'utilisateur s'il veut décoder le contenu du FlateDecode
while True:
print(Fore.LIGHTYELLOW_EX + Style.BRIGHT + "\n Voulez-vous decoder (base64) le contenu de l'objet / stream ?")
print(Fore.LIGHTCYAN_EX + Style.BRIGHT + "\n [1] ➙ Oui")
print(" [2] ➙ Non")
choice = input(Fore.GREEN + "\n Option : ")
if choice == '1':
decode_flatedecode(os.path.join('generated_files', 'raw_flatedecode.txt'), 'decoded_flatedecode.jpg') # Modifier l'extension du fichier de sortie si nécessaire
choice = input(Fore.LIGHTBLACK_EX + "\n Appuyez sur Entrée pour continuer...")
clear_screen(), print_banner()
break
elif choice == '2':
clear_screen(), print_banner()
break
else:
clear_screen()
print(Fore.RED + Style.BRIGHT + "\n [-] Veuillez entrer 1 ou 2 pour sélectionner une option.")
# [5] Supprimer les fichiers générés
elif extraction_type == '5':
try:
remove_files()
print(Fore.LIGHTGREEN_EX + Style.BRIGHT + "\n [+] Les fichiers générés ont été supprimés avec succès.")
choice = input(Fore.LIGHTBLACK_EX + "\n Appuyez sur Entrée pour continuer...")
clear_screen(), print_banner()
continue
except Exception as e:
print(Fore.LIGHTRED_EX + Style.BRIGHT + f"\n [-] Une erreur s'est produite : {str(e)}")
choice = input(Fore.LIGHTBLACK_EX + "\n Appuyez sur Entrée pour continuer...")
clear_screen(), print_banner()
continue
# [6] Quitter le programme
elif extraction_type == '6':
clear_screen()
break
else:
clear_screen()
print(Fore.RED + Style.BRIGHT + "\n [-] Veuillez entrer un nombre entre 1 et 5.")
# Lancement du programme
if __name__ == '__main__':
if not os.path.exists('generated_files'):
os.makedirs('generated_files')
main()