In this recipe, you will use UPX to pack a directory of files.
- Place upx.exe in a directory, A, and place a collection of samples in a directory, B, in A. For this example, B is Benign PE Samples UPX.
- List the files of directory B:
import osfiles_path = "Benign PE Samples UPX/"files = os.listdir(files_path)file_paths = [files_path+x for x in files]
- Run upx against each file in B:
from subprocess import Popen, PIPEcmd = "upx.exe"for path in file_paths: cmd2 = cmd+" \""+path+"\"" res = Popen(cmd2, stdout=PIPE).communicate() print(res)
- Whenever an error occurs in packing, remove the original sample:
if "error" in str(res): print(path) os.remove(path)