File tree Expand file tree Collapse file tree 2 files changed +24
-2
lines changed
Expand file tree Collapse file tree 2 files changed +24
-2
lines changed Original file line number Diff line number Diff line change @@ -43,7 +43,7 @@ def preferred_filename(self):
4343 return self ._preferred_filename
4444
4545 @preferred_filename .setter
46- def preferred_filename (self , filename ):
46+ def preferred_filename (self , filename : str ):
4747 """Sets the preferred filename"""
4848 if self .closed :
4949 raise IOError ("FileHandler name cannot be changed once closed" )
@@ -57,6 +57,18 @@ def preferred_filename(self, filename):
5757 def close (self ):
5858 """Method that commits the file and fixes the final filename for use"""
5959
60+ @staticmethod
61+ def sanitize_filename (filename : str ) -> str :
62+ """Sanititizes the filename to ensure only a specific whitelist of characters is allowed through"""
63+ allowed = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789.- ()[]\{\}!$%^:#~?<>,|"
64+ result = ""
65+ for char in filename :
66+ if char in allowed :
67+ result += char
68+ else :
69+ result += "?"
70+ return result
71+
6072 def __enter__ (self ):
6173 return self
6274
Original file line number Diff line number Diff line change @@ -90,9 +90,19 @@ def process_dump(
9090 offset = peb .ImageBaseAddress ,
9191 layer_name = proc_layer_name ,
9292 )
93+
94+ process_name = proc .ImageFileName .cast (
95+ "string" ,
96+ max_length = proc .ImageFileName .vol .count ,
97+ errors = "replace" ,
98+ )
99+
93100 file_handle = open_method (
94- f"pid.{ proc .UniqueProcessId } .{ peb .ImageBaseAddress :#x} .dmp"
101+ open_method .sanitize_filename (
102+ f"{ proc .UniqueProcessId } .{ process_name } .{ peb .ImageBaseAddress :#x} .dmp"
103+ )
95104 )
105+
96106 for offset , data in dos_header .reconstruct ():
97107 file_handle .seek (offset )
98108 file_handle .write (data )
You can’t perform that action at this time.
0 commit comments