EventHandlers.vbs CurrentFolder

Forum for things that doesn't really have anything to do with hMailServer. Such as php.ini, beer, etc etc.
Post Reply
User avatar
RvdH
Senior user
Senior user
Posts: 1327
Joined: 2008-06-27 14:42
Location: Netherlands

EventHandlers.vbs CurrentFolder

Post by RvdH » 2021-04-03 15:39

I was trying to make some path(s) in my script dynamic, eg: compatible between x86 and x64 version
One of the first things i tried is to include some external files relative to EventHandlers.vbs

Code: Select all

Dim FSO
Set FSO= CreateObject("Scripting.FileSystemObject")
WriteLog(FSO.GetAbsolutePathName("."))
Set FSO = Mnothing

Dim WshShell, strCurDir
Set WshShell = CreateObject("WScript.Shell")
strCurDir = WshShell.CurrentDirectory
WriteLog(strCurDir)
Set WshShell = Nothing

Sub WriteLog(strMessage)
	Const FOR_APPENDING = 8
	Dim strFileName, objFS, oFile
	strFileName = "C:\Temp\test.txt"

	Set objFS = CreateObject("Scripting.FileSystemObject")

	If objFS.FileExists(strFileName) Then
		Set oFile = objFS.OpenTextFile(strFileName, FOR_APPENDING)
	Else
		Set oFile = objFS.CreateTextFile(strFileName)
	End If

	oFile.WriteLine strMessage
End Sub

When ran directly from a vbs both methods return the proper directory, but when EventHandlers.vbs is called from within hmailserver the returned value is always: C:\Windows\System32

Weird huh?
CIDR to RegEx: d-fault.nl/CIDRtoRegEx
DNS Lookup: d-fault.nl/DNSTools
DNSBL Lookup: d-fault.nl/DNSBLLookup
GEOIP Lookup: d-fault.nl/GeoipLookup

User avatar
SorenR
Senior user
Senior user
Posts: 4412
Joined: 2006-08-21 15:38
Location: Denmark

Re: EventHandlers.vbs CurrentFolder

Post by SorenR » 2021-04-03 18:19

I guess cscript.exe is installed in %SYSTEMROOT%\System32 :wink:

It's the same issue when using vbs as CGI.

https://support.microsoft.com/da-dk/top ... a5db3cb9ef
SørenR.

Algorithm (noun.)
Word used by programmers when they do not want to explain what they did.

User avatar
RvdH
Senior user
Senior user
Posts: 1327
Joined: 2008-06-27 14:42
Location: Netherlands

Re: EventHandlers.vbs CurrentFolder

Post by RvdH » 2021-04-03 19:58

I Tried reading InstallLocation from registry to get the path dynamically (without calling oApp.Authenticate) ....but this seems wrong for 64-bit, already opened a issue

We have various options after CreateObject("hMailServer.Application") and .Authenticate is called, but the point is i need it before

Code: Select all

dim oApp
Set oApp = CreateObject("hMailServer.Application")
Call oApp.Authenticate(ADMIN,PASSWORD) ' set your administrator password in this line 
MsgBox oApp.Settings.Directories.TempDirectory
MsgBox oApp.Settings.Directories.LogDirectory
MsgBox oApp.Settings.Directories.EventDirectory
MsgBox oApp.Settings.Scripting.CurrentScriptFile
MsgBox oApp.Settings.Scripting.Directory
' MsgBox oApp.Settings.Scripting.Language
CIDR to RegEx: d-fault.nl/CIDRtoRegEx
DNS Lookup: d-fault.nl/DNSTools
DNSBL Lookup: d-fault.nl/DNSBLLookup
GEOIP Lookup: d-fault.nl/GeoipLookup

User avatar
mattg
Moderator
Moderator
Posts: 21319
Joined: 2007-06-14 05:12
Location: 'The Outback' Australia

Re: EventHandlers.vbs CurrentFolder

Post by mattg » 2021-04-05 04:39

RvdH wrote:
2021-04-03 15:39
I was trying to make some path(s) in my script dynamic, eg: compatible between x86 and x64 version
One of the first things i tried is to include some external files relative to EventHandlers.vbs
Doesn't the .ini file [Directories] section do that already?

Code: Select all

[Directories]
ProgramFolder=C:\hMailServer
DataFolder=c:\hMailServer\Data
LogFolder=c:\hMailServer\Logs
TempFolder=C:\hMailServer\Temp
EventFolder=C:\hMailServer\Events
DatabaseFolder=C:\hMailServer\Database
Just 'cause I link to a page and say little else doesn't mean I am not being nice.
https://www.hmailserver.com/documentation

User avatar
SorenR
Senior user
Senior user
Posts: 4412
Joined: 2006-08-21 15:38
Location: Denmark

Re: EventHandlers.vbs CurrentFolder

Post by SorenR » 2021-04-05 08:13

mattg wrote:
2021-04-05 04:39
RvdH wrote:
2021-04-03 15:39
I was trying to make some path(s) in my script dynamic, eg: compatible between x86 and x64 version
One of the first things i tried is to include some external files relative to EventHandlers.vbs
Doesn't the .ini file [Directories] section do that already?

Code: Select all

[Directories]
ProgramFolder=C:\hMailServer
DataFolder=c:\hMailServer\Data
LogFolder=c:\hMailServer\Logs
TempFolder=C:\hMailServer\Temp
EventFolder=C:\hMailServer\Events
DatabaseFolder=C:\hMailServer\Database
Yes, question is how to locate ini file without using API...
SørenR.

Algorithm (noun.)
Word used by programmers when they do not want to explain what they did.

User avatar
katip
Senior user
Senior user
Posts: 823
Joined: 2006-12-22 07:58
Location: Istanbul

Re: EventHandlers.vbs CurrentFolder

Post by katip » 2021-04-05 10:22

SorenR wrote:
2021-04-05 08:13
Yes, question is how to locate ini file without using API...
assuming service name is hMailServer (why not??)

Code: Select all

wmic service hmailserver get PathName
should return full path to exe
could be useful no?
Katip
--
HMS 5.7.0 x64, MariaDB 10.4.10 x64, SA 3.4.2, ClamAV 0.101.2 + SaneS

User avatar
RvdH
Senior user
Senior user
Posts: 1327
Joined: 2008-06-27 14:42
Location: Netherlands

Re: EventHandlers.vbs CurrentFolder

Post by RvdH » 2021-04-05 12:53

Constants.vbs is an obfuscated vbs (pentest...to hide sensitive information from prying eyes), containing public constants, like: ADMIN, PASSWORD, MYSQL table, username, password and some API keys (blocklist.de, abusipdb)

Code: Select all

Dim InstallLocation : InstallLocation = "C:\Program Files\hMailServer"
If RegKeyExists("HKLM\SOFTWARE\hMailServer\InstallLocation") Then
	InstallLocation = RegKeyRead("HKLM\SOFTWARE\hMailServer\InstallLocation")
ElseIf RegKeyExists("HKLM\SOFTWARE\WOW6432Node\hMailServer\InstallLocation") Then
	InstallLocation = RegKeyRead("HKLM\SOFTWARE\WOW6432Node\hMailServer\InstallLocation")
End If

With CreateObject("Scripting.FileSystemObject")
	dim ScriptDir : ScriptDir = .BuildPath(InstallLocation,"\Events\")
	includeFile .BuildPath(ScriptDir,"Constants.vbs")
End With

Function RegKeyRead(strRegistryKey)
    Dim oShell, value
    On Error Resume Next
    Set oShell = CreateObject("WScript.Shell")
    value = oShell.RegRead(strRegistryKey)
	If Err.Number <> 0 Then
		Err.Clear
        RegKeyRead=Empty
    Else
		Err.Clear
        RegKeyRead=value
	End If
    Set oShell = Nothing
end function

Function RegKeyExists(strRegistryKey)
	Dim oShell, entry
	On Error Resume Next
	Set oShell = CreateObject("WScript.Shell")
	entry = oShell.RegRead(strRegistryKey)
	If Err.Number <> 0 Then
		Err.Clear
		RegKeyExists = False
	Else
		Err.Clear
		RegKeyExists = True
	End If
	Set oShell = Nothing
End Function

Sub includeFile(fSpec)
    executeGlobal CreateObject("Scripting.FileSystemObject").openTextFile(fSpec).readAll()
End Sub
I know the obfuscated Constants.vbs won't stop an experienced hacker, but it might help to prevent disclosure of sensitive that is in plain sight for other (legal) users on the system
Last edited by RvdH on 2021-04-05 13:09, edited 3 times in total.
CIDR to RegEx: d-fault.nl/CIDRtoRegEx
DNS Lookup: d-fault.nl/DNSTools
DNSBL Lookup: d-fault.nl/DNSBLLookup
GEOIP Lookup: d-fault.nl/GeoipLookup

User avatar
SorenR
Senior user
Senior user
Posts: 4412
Joined: 2006-08-21 15:38
Location: Denmark

Re: EventHandlers.vbs CurrentFolder

Post by SorenR » 2021-04-05 12:55

katip wrote:
2021-04-05 10:22
SorenR wrote:
2021-04-05 08:13
Yes, question is how to locate ini file without using API...
assuming service name is hMailServer (why not??)

Code: Select all

wmic service hmailserver get PathName
should return full path to exe
could be useful no?
Can it really be that simple :mrgreen:

Nice catch 8)
Confirmed on Windows Server 2003 R2, Windows 10 Home 20H2 and Windows Server 2019 Essential 1809.
SørenR.

Algorithm (noun.)
Word used by programmers when they do not want to explain what they did.

User avatar
RvdH
Senior user
Senior user
Posts: 1327
Joined: 2008-06-27 14:42
Location: Netherlands

Re: EventHandlers.vbs CurrentFolder

Post by RvdH » 2021-04-05 13:03

SorenR wrote:
2021-04-05 12:55
katip wrote:
2021-04-05 10:22
SorenR wrote:
2021-04-05 08:13
Yes, question is how to locate ini file without using API...
assuming service name is hMailServer (why not??)

Code: Select all

wmic service hmailserver get PathName
should return full path to exe
could be useful no?
Can it really be that simple :mrgreen:

Nice catch 8)
Confirmed on Windows Server 2003 R2, Windows 10 Home 20H2 and Windows Server 2019 Essential 1809.

Code: Select all

wmic service hmailserver get PathName
PathName
"C:\Program Files\hMailServer\Bin\hMailServer.exe" RunAsService
If you need the executable file and/or directory sure... but needed the /Events directory for script file location
CIDR to RegEx: d-fault.nl/CIDRtoRegEx
DNS Lookup: d-fault.nl/DNSTools
DNSBL Lookup: d-fault.nl/DNSBLLookup
GEOIP Lookup: d-fault.nl/GeoipLookup

User avatar
SorenR
Senior user
Senior user
Posts: 4412
Joined: 2006-08-21 15:38
Location: Denmark

Re: EventHandlers.vbs CurrentFolder

Post by SorenR » 2021-04-05 13:11

Code: Select all

strComputer = "." 
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2") 
Set colItems = objWMIService.ExecQuery("SELECT * FROM Win32_Service WHERE name LIKE '%hmailserver%'",,48) 
For Each objItem in colItems 
    Wscript.Echo "Name: " & objItem.Name & vbNewLine & "Pathname: " & objItem.PathName
Next
https://docs.microsoft.com/en-us/window ... 32-service
SørenR.

Algorithm (noun.)
Word used by programmers when they do not want to explain what they did.

User avatar
SorenR
Senior user
Senior user
Posts: 4412
Joined: 2006-08-21 15:38
Location: Denmark

Re: EventHandlers.vbs CurrentFolder

Post by SorenR » 2021-04-05 13:16

RvdH wrote:
2021-04-05 13:03
SorenR wrote:
2021-04-05 12:55
katip wrote:
2021-04-05 10:22


assuming service name is hMailServer (why not??)

Code: Select all

wmic service hmailserver get PathName
should return full path to exe
could be useful no?
Can it really be that simple :mrgreen:

Nice catch 8)
Confirmed on Windows Server 2003 R2, Windows 10 Home 20H2 and Windows Server 2019 Essential 1809.

Code: Select all

wmic service hmailserver get PathName
PathName
"C:\Program Files\hMailServer\Bin\hMailServer.exe" RunAsService
If you need the executable file and/or directory sure... but needed the /Events directory for script file location
You get the location where the INI file where the information you seek is stored...
SørenR.

Algorithm (noun.)
Word used by programmers when they do not want to explain what they did.

User avatar
RvdH
Senior user
Senior user
Posts: 1327
Joined: 2008-06-27 14:42
Location: Netherlands

Re: EventHandlers.vbs CurrentFolder

Post by RvdH » 2021-04-05 13:19

There is more then one road to Rome... i prefer reading it from registry (especially if/when that little issue is fixed)
CIDR to RegEx: d-fault.nl/CIDRtoRegEx
DNS Lookup: d-fault.nl/DNSTools
DNSBL Lookup: d-fault.nl/DNSBLLookup
GEOIP Lookup: d-fault.nl/GeoipLookup

User avatar
RvdH
Senior user
Senior user
Posts: 1327
Joined: 2008-06-27 14:42
Location: Netherlands

Re: EventHandlers.vbs CurrentFolder

Post by RvdH » 2021-04-05 13:37

i do agree i should read "EventFolder" from ini file, as these might (manually) be located outside the InstallLocation folder

Code: Select all

Dim InstallLocation : InstallLocation = "C:\Program Files\hMailServer"
If RegKeyExists("HKLM\SOFTWARE\hMailServer\InstallLocation") Then
	InstallLocation = RegKeyRead("HKLM\SOFTWARE\hMailServer\InstallLocation")
ElseIf RegKeyExists("HKLM\SOFTWARE\WOW6432Node\hMailServer\InstallLocation") Then
	InstallLocation = RegKeyRead("HKLM\SOFTWARE\WOW6432Node\hMailServer\InstallLocation")
End If

With CreateObject("Scripting.FileSystemObject")
	dim INIPath : INIPath = .BuildPath(InstallLocation,"\Bin\hmailserver.ini")
        dim EventFolder : EventFolder = ReadIni(INIPath, "Directories", "EventFolder")
	includeFile .BuildPath(EventFolder,"Constants.vbs")
End With

Function ReadIni( myFilePath, mySection, myKey )
    ' This function returns a value read from an INI file
    '
    ' Arguments:
    ' myFilePath  [string]  the (path and) file name of the INI file
    ' mySection   [string]  the section in the INI file to be searched
    ' myKey       [string]  the key whose value is to be returned
    '
    ' Returns:
    ' the [string] value for the specified key in the specified section
    '
    ' CAVEAT:     Will return a space if key exists but value is blank
    '
    ' Written by Keith Lacelle
    ' Modified by Denis St-Pierre and Rob van der Woude

    Const ForReading   = 1
    Const ForWriting   = 2
    Const ForAppending = 8

    Dim intEqualPos
    Dim objFSO, objIniFile
    Dim strFilePath, strKey, strLeftString, strLine, strSection

    Set objFSO = CreateObject( "Scripting.FileSystemObject" )

    ReadIni     = ""
    strFilePath = Trim( myFilePath )
    strSection  = Trim( mySection )
    strKey      = Trim( myKey )

    If objFSO.FileExists( strFilePath ) Then
        Set objIniFile = objFSO.OpenTextFile( strFilePath, ForReading, False )
        Do While objIniFile.AtEndOfStream = False
            strLine = Trim( objIniFile.ReadLine )

            ' Check if section is found in the current line
            If LCase( strLine ) = "[" & LCase( strSection ) & "]" Then
                strLine = Trim( objIniFile.ReadLine )

                ' Parse lines until the next section is reached
                Do While Left( strLine, 1 ) <> "["
                    ' Find position of equal sign in the line
                    intEqualPos = InStr( 1, strLine, "=", 1 )
                    If intEqualPos > 0 Then
                        strLeftString = Trim( Left( strLine, intEqualPos - 1 ) )
                        ' Check if item is found in the current line
                        If LCase( strLeftString ) = LCase( strKey ) Then
                            ReadIni = Trim( Mid( strLine, intEqualPos + 1 ) )
                            ' In case the item exists but value is blank
                            If ReadIni = "" Then
                                ReadIni = " "
                            End If
                            ' Abort loop when item is found
                            Exit Do
                        End If
                    End If

                    ' Abort if the end of the INI file is reached
                    If objIniFile.AtEndOfStream Then Exit Do

                    ' Continue with next line
                    strLine = Trim( objIniFile.ReadLine )
                Loop
            Exit Do
            End If
        Loop
        objIniFile.Close
    Else
        WScript.Echo strFilePath & " doesn't exists. Exiting..."
        Wscript.Quit 1
    End If
End Function

Function RegKeyRead(strRegistryKey)
    Dim oShell, value
    On Error Resume Next
    Set oShell = CreateObject("WScript.Shell")
    value = oShell.RegRead(strRegistryKey)
	If Err.Number <> 0 Then
		Err.Clear
        RegKeyRead=Empty
    Else
		Err.Clear
        RegKeyRead=value
	End If
    Set oShell = Nothing
end function

Function RegKeyExists(strRegistryKey)
	Dim oShell, entry
	On Error Resume Next
	Set oShell = CreateObject("WScript.Shell")
	entry = oShell.RegRead(strRegistryKey)
	If Err.Number <> 0 Then
		Err.Clear
		RegKeyExists = False
	Else
		Err.Clear
		RegKeyExists = True
	End If
	Set oShell = Nothing
End Function

Sub includeFile(fSpec)
    executeGlobal CreateObject("Scripting.FileSystemObject").openTextFile(fSpec).readAll()
End Sub
CIDR to RegEx: d-fault.nl/CIDRtoRegEx
DNS Lookup: d-fault.nl/DNSTools
DNSBL Lookup: d-fault.nl/DNSBLLookup
GEOIP Lookup: d-fault.nl/GeoipLookup

Post Reply