1
+ #!/usr/bin/env python
2
+ # encoding: utf-8
3
+
4
+ import logging
5
+ from modules .mp_module import MpModule
6
+ import re
7
+
8
+ HTA_TEMPLATE = \
9
+ r"""
10
+ <!DOCTYPE html>
11
+ <html>
12
+ <head>
13
+ <HTA:APPLICATION />
14
+ <script type="text/vbscript">
15
+ <<<VBS>>>
16
+ <<<MAIN>>>
17
+ Close
18
+ </script>
19
+ </head>
20
+ <body>
21
+ </body>
22
+ </html>
23
+
24
+ """
25
+
26
+ class HTAGenerator (MpModule ):
27
+ """ Module used to generate HTA file from working dir content"""
28
+
29
+ def vbScriptCheck (self ):
30
+ logging .info (" [-] Check if VBA->VBScript is possible..." )
31
+ # Check nb of source file
32
+ if len (self .getVBAFiles ())> 1 :
33
+ logging .info (" [-] This module cannot handle more than one VBA file. Abort!" )
34
+ return False
35
+
36
+ f = open (self .getMainVBAFile ())
37
+ content = f .readlines ()
38
+ f .close ()
39
+ # Check there are no call to Application object
40
+ for line in content :
41
+ if line .find ("Application." ) != - 1 :
42
+ logging .info (" [-] You cannot access Application object in VBScript. Abort!" )
43
+ return False
44
+
45
+ # Check there are no DLL import
46
+ for line in content :
47
+ matchObj = re .match ( r'.*(Sub|Function)\s*([a-zA-Z0-9_]+)\s*Lib\s*"(.+)"\s*.*' , line , re .M | re .I )
48
+ if matchObj :
49
+ logging .info (" [-] VBScript does not support DLL import. Abort!" )
50
+ return False
51
+ return True
52
+
53
+
54
+ def vbScriptConvert (self ):
55
+ logging .info (" [-] Convert VBA to VBScript..." )
56
+ translators = [("Val(" ,"CInt(" ),(" Chr$" ," Chr" ),(" Mid$" ," Mid" ),("On Error" ,"//On Error" ),("byebye:" ,"" ), ("Next " , "Next //" )]
57
+ translators .extend ([(" As String" ," " ),(" As Object" ," " ),(" As Long" ," " ),(" As Integer" ," " )])
58
+ f = open (self .getMainVBAFile ())
59
+ content = f .readlines ()
60
+ f .close ()
61
+ isUsingEnviron = False
62
+ for n ,line in enumerate (content ):
63
+ # Do easy translations
64
+ for translator in translators :
65
+ line = line .replace (translator [0 ],translator [1 ])
66
+
67
+ # Check if ENVIRON is used
68
+ if line .find ("Environ(" )!= - 1 :
69
+ isUsingEnviron = True
70
+ line = re .sub ('Environ\("([A-Z_]+)"\)' ,r'wshShell.ExpandEnvironmentStrings( "%\1%" )' , line , flags = re .I )
71
+ content [n ] = line
72
+ # ENVIRON("COMPUTERNAME") ->
73
+ #Set wshShell = CreateObject( "WScript.Shell" )
74
+ #strComputerName = wshShell.ExpandEnvironmentStrings( "%COMPUTERNAME%" )
75
+
76
+ # Write in new VBS file
77
+ f = open (self .getMainVBAFile ()+ ".vbs" , 'a' )
78
+ if isUsingEnviron :
79
+ f .write ('Set wshShell = CreateObject( "WScript.Shell" )\n ' )
80
+ f .writelines (content )
81
+ f .close ()
82
+
83
+
84
+ def genHTA (self ):
85
+ logging .info (" [-] Generating HTA file..." )
86
+ f = open (self .getMainVBAFile ()+ ".vbs" )
87
+ vbsContent = f .read ()
88
+ f .close ()
89
+ # Write VBS in template
90
+ htaContent = HTA_TEMPLATE
91
+ htaContent = htaContent .replace ("<<<VBS>>>" , vbsContent )
92
+ htaContent = htaContent .replace ("<<<MAIN>>>" , self .startFunction )
93
+ # Write in new HTA file
94
+ f = open (self .outputFilePath , 'w' )
95
+ f .writelines (htaContent )
96
+ f .close ()
97
+ logging .info (" [-] Generated HTA file: %s" % self .outputFilePath )
98
+
99
+
100
+ def run (self ):
101
+ logging .info (" [+] Generating HTA file from VBA..." )
102
+ if not self .vbScriptCheck ():
103
+ return
104
+ self .vbScriptConvert ()
105
+ self .genHTA ()
106
+
107
+
108
+
0 commit comments