Hi I'm building my own antivirus in batch and want to add a zip bomb or decompression bomb detecting feature, how would I determine final uncompressed size of a zip in batch or vbscript? Thanks in advance
Asked
Active
Viewed 579 times
1
-
You can query the command line archiver to get the total filesize. Which archiver are you using? – foxidrive Dec 30 '14 at 07:16
-
I'm not using any particular archiver for my program. Could the one embedded in windows explorer be accessed from the command line? – GeriatricJacob Dec 30 '14 at 07:26
1 Answers
1
Save this as .bat
:
@if (@x)==(@y) @end /***** jscript comment ******
@echo off
cscript //E:JScript //nologo "%~f0" %*
exit /b 0
@if (@x)==(@y) @end ****** end comment *********/
var wshShell = WScript.CreateObject("WScript.Shell");
var args=WScript.Arguments;
var zipFile=args.Item(0);
getSize = function(path){
var ShellObj=new ActiveXObject("Shell.Application");
var targetObject = new Object;
var targetObject=ShellObj.NameSpace(path);
if (typeof size === 'undefined'){
var size=0;
}
if (targetObject != null ){
for (var i=0; i<targetObject.Items().Count;i++){
//WScript.Echo("Checking: "+targetObject.Items().Item(i));
if(!targetObject.Items().Item(i).IsFolder){
size=size+targetObject.Items().Item(i).Size;
} else if (targetObject.Items().Item(i).Count!=0){
size=size+getSize(targetObject.Items().Item(i).Path);
}
}
} else {
return 0;
}
return size;
}
WScript.Echo(getSize(zipFile));
It takes only one argument - the zip file and prints its size in bytes. (the Item.Size
property gets the uncompressed size)
EDIT it does not work with relative paths.Full paths should be used. EDIT zipjs.bat (introduced here ) can be used for this purpose:
call zipjs.bat getSize -source C:\\zipFile.zip
and no longer full path is required and a relative one can be used.
-
@Towtow10 - try with the full path.Apparently it does not work with relative paths. – npocmaka Dec 30 '14 at 10:48
-
I was already using the full path but I tried relative and it worked thanks. – GeriatricJacob Dec 30 '14 at 10:50
-
1I'd change line 5 to `exit /b %errorlevel%` so that the exit code of the VBScript is passed to the caller. – Ansgar Wiechers Dec 30 '14 at 10:53
-
For some reason when I run this program inside my program it stops the main program. Can you fix this? – GeriatricJacob Dec 30 '14 at 12:00
-
@Towtow10 - what you mean with main program? How do you call the script? From batch file? – npocmaka Dec 30 '14 at 12:06
-
@npocmaka no I can call it from my batch file when your script finishes running it closes my batch file. – GeriatricJacob Dec 30 '14 at 12:09
-
-
@npocmaka - Thanks that worked, but can I have the output as a variable instead of printing it to the screen? – GeriatricJacob Dec 30 '14 at 12:18
-
-
-
That worked but your code does not go all the way through the zip bomb, it only calculates the first layer. A zip bomb is an archive which has more archives in it and each of those have more archives and more archives and at the end in each archive is a really large file say 4 gb, the zip bomb I'm testing on should come to well over 4 billion gb. is there any way you can modify your code to detect this? – GeriatricJacob Dec 30 '14 at 23:00
-
@Towtow10 - No.`Shell.Application` sees a zip file as a directory but the zip files inside a zip are taken as a files so cannot be traversed.But if you use `Shell.Application` for unzip the zip bomb will not work as it can unzip only on level1. – npocmaka Dec 31 '14 at 09:32