[SOLVED] Creation of a package allowing searching for a file on workstations

Questions about WAPT Packaging / Requests and help regarding Wapt packages.
Forum Rules
Community Forum Rules
* English support on www.reddit.com/r/wapt
* French community support is available on this forum
* Please prefix the topic title with [RESOLVED] if it is resolved.
* Please do not edit a topic that is tagged [RESOLVED]. Open a new topic referencing the old one.
* Specify the installed WAPT version, full version, and build number (2.2.1.11957 / 2.2.2.12337 / etc.) as well as the Enterprise/Discovery edition.
* Versions 1.8.2 and earlier are no longer supported. The only questions accepted regarding version 1.8.2 are related to upgrading to a supported version (2.1, 2.2, etc.).
* Specify the server OS (Linux/Windows) and version (Debian Buster/Bullseye - CentOS 7 - Windows Server 2012/2016/2019).
* Specify the OS of the administration/package creation machine and the machine with the problematic agent, if applicable (Windows 7/10/11/Debian 11/etc.).
* Avoid asking multiple questions when opening a topic, otherwise it may be ignored. If there are multiple topics, open separate topics, preferably one after the other and not all at the same time (i.e., do not spam the forum).
* Include code snippets, screenshots, and other images directly in the post. Links to Pastebin, Bitly, and other third-party sites will be systematically removed.
* As with any community forum, support is provided voluntarily by members. If you require commercial support, you can contact Tranquil IT's sales department at 02.40.97.57.55
Locked
alex_30
Messages: 14
Registration: July 8, 2020 - 5:33 PM

February 7, 2024 - 5:08 PM

Hello everyone,

My WAPT server version is WAPT Server 2.4.0.14143 Enterprise Edition under Windows Server 2019 Standard.
I create the packages from my machine running Microsoft Windows 11 Professional Version 10.0.22631 Build 22631 with WAPT Agent version 2.4.0.14143-4

I apologize in advance if this question has already been asked, but I couldn't find a package on the store or any information on the forum.

I want to create a package that allows me to find out if a file exists on the workstations.

I tried the following code:

Code: Select all

# -*- coding: utf-8 -*-
from setuphelpers import *

def install():
    for fn in find_all_files('c:\\','*nomdufichier'):
            print(fn)
But the package throws an error as soon as there are denied permissions; I also can't exclude these paths:

Code: Select all

File "C:\Program Files (x86)\wapt\common.py", line 4083, in install_wapt
    exitstatus = setup.install()
  File "C:\windows\TEMP\wapt4h6awkg2\setup.py", line 6, in install
  File "C:\Program Files (x86)\wapt\waptutils.py", line 1403, in find_all_files
    for f in do_find_all_files(rootdir):
  File "C:\Program Files (x86)\wapt\waptutils.py", line 1384, in do_find_all_files
    for fn in do_find_all_files(full_fn):
  File "C:\Program Files (x86)\wapt\waptutils.py", line 1384, in do_find_all_files
    for fn in do_find_all_files(full_fn):
  File "C:\Program Files (x86)\wapt\waptutils.py", line 1384, in do_find_all_files
    for fn in do_find_all_files(full_fn):
  [Previous line repeated 1 more time]
  File "C:\Program Files (x86)\wapt\waptutils.py", line 1379, in do_find_all_files
    for fn in os.listdir(absolute_rootdir):
PermissionError: [WinError 5] Accès refusé: 'c:\\users\\admaz\\AppData\\Local\\Application Data'

13 : Accès refusé

Can you assist me?

I might need to look into an audit function, but I haven't used that part yet.
Most of the time we use the packages from the store.

Thank you.
User avatar
dcardon
WAPT Expert
Messages: 1932
Registration: June 18, 2014 - 09:58
Location: Saint Sébastien sur Loire
Contact :

February 8, 2024 - 12:41

Hello Alexandre,

two things:

* Regarding the permissions issue, if you ran the command in PyScripter, it's normal to get "access denied" errors. Administrator isn't root; they're just "sudoer" and don't have default access to all directories. You can try running it as the LOCAL SYSTEM account if that's a better fit.

* To filter the directories, you can use more generic Python functions like os.walk or glob.glob. The setuphelper functions are designed to be user-friendly and therefore don't have all the available options.

Regards,

Dneis
Denis Cardon - Tranquil IT
Share your experiences on WAPT! Send us your blog and article URLs in the "Your Opinion of the forum, and we'll feature them on the WAPT
alex_30
Messages: 14
Registration: July 8, 2020 - 5:33 PM

February 13, 2024 - 1:39 PM

Thank you for the feedback.

Regarding the first point, the command was not launched by PyScripter.

The errors occurred during the package installation.

In the end, I succeeded with pathlib; here is the code:

Code: Select all

# -*- coding: utf-8 -*-
from setuphelpers import *
import pathlib

def install():
    pass

def audit():
    #Emplacement de la recherche
    fileDir = r'C:\\'
    #Partie du nom de fichier à rechercher
    fileExt = r"**\*nomdufichier*"
    liste_fichiers = list(pathlib.Path(fileDir).glob(fileExt))
    if liste_fichiers:
        print('Fichiers trouvés: %s ' %liste_fichiers)
        return 'WARNING'
    print('Aucun fichiers trouvés')
    return 'OK'
alex_30
Messages: 14
Registration: July 8, 2020 - 5:33 PM

February 13, 2024 - 2:00 PM

The goal was to be able to search for files on the workstations.

More specifically, mobile apps that don't appear in the inventory. And often not updated, therefore potentially vulnerable

The previous code works, but I would have liked to optimize it by also updating the version of the .exe file

Here's what I tested:

Code: Select all

# -*- coding: utf-8 -*-
from setuphelpers import *
import pathlib
import win32api

def install():
    pass

def audit():
    fileDir = r'C:\\'
    fileExt = r"**\*nomdufichier*"
    liste_fichiers = list(pathlib.Path(fileDir).glob(fileExt))
    for fname in liste_fichiers:
        properties= getFileProperties(fname)
        print('properties =  %s ' %properties)
        print('Fichiers trouvés: %s propriétés %s' %(fname , properties))
    if liste_fichiers:
        print('Fichiers trouvés: %s ' %liste_fichiers)
        return 'WARNING'
    print('Aucun fichiers trouvés')
    return 'OK'



#==============================================================================
def getFileProperties(fname):
#==============================================================================
    """
    Read all properties of the given file return them as a dictionary.
    """
    propNames = ('Comments', 'InternalName', 'ProductName',
        'CompanyName', 'LegalCopyright', 'ProductVersion',
        'FileDescription', 'LegalTrademarks', 'PrivateBuild',
        'FileVersion', 'OriginalFilename', 'SpecialBuild')

    props = {'FixedFileInfo': None, 'StringFileInfo': None, 'FileVersion': None}

    try:
        # backslash as parm returns dictionary of numeric info corresponding to VS_FIXEDFILEINFO struc
        fixedInfo = win32api.GetFileVersionInfo(fname, '\\')
        props['FixedFileInfo'] = fixedInfo
        props['FileVersion'] = "%d.%d.%d.%d" % (fixedInfo['FileVersionMS'] / 65536,
                fixedInfo['FileVersionMS'] % 65536, fixedInfo['FileVersionLS'] / 65536,
                fixedInfo['FileVersionLS'] % 65536)

        # \VarFileInfo\Translation returns list of available (language, codepage)
        # pairs that can be used to retreive string info. We are using only the first pair.
        lang, codepage = win32api.GetFileVersionInfo(fname, '\\VarFileInfo\\Translation')[0]

        # any other must be of the form \StringfileInfo\%04X%04X\parm_name, middle
        # two are language/codepage pair returned from above

        strInfo = {}
        for propName in propNames:
            strInfoPath = u'\\StringFileInfo\\%04X%04X\\%s' % (lang, codepage, propName)
            ## print str_info
            strInfo[propName] = win32api.GetFileVersionInfo(fname, strInfoPath)

        props['StringFileInfo'] = strInfo
    except:
        pass

    return props
The for loop should have been under the if statement, but it was for debugging purposes
In the end I was unable to use the function def getFileProperties(fname):
find on: https://stackoverflow.com/questions/580 ... on-windows

Here is the result:

Code: Select all

Auditing XXX-searchfile
properties =  {'FixedFileInfo': None, 'StringFileInfo': None, 'FileVersion': None} 
Fichiers trouvés: C:\Users\user1\AppData\Roaming\AnyDesk propriétés {'FixedFileInfo': None, 'StringFileInfo': None, 'FileVersion': None}
properties =  {'FixedFileInfo': None, 'StringFileInfo': None, 'FileVersion': None} 
Fichiers trouvés: C:\Users\user1\Downloads\AnyDesk.exe propriétés {'FixedFileInfo': None, 'StringFileInfo': None, 'FileVersion': None}
properties =  {'FixedFileInfo': None, 'StringFileInfo': None, 'FileVersion': None} 
Fichiers trouvés: C:\Users\user1\Pictures\AnyDesk propriétés {'FixedFileInfo': None, 'StringFileInfo': None, 'FileVersion': None}
properties =  {'FixedFileInfo': None, 'StringFileInfo': None, 'FileVersion': None} 
Fichiers trouvés: C:\Windows\Prefetch\ANYDESK.EXE-0A8BB3A0.pf propriétés {'FixedFileInfo': None, 'StringFileInfo': None, 'FileVersion': None}
Fichiers trouvés: [WindowsPath('C:/Users/user1/AppData/Roaming/AnyDesk'), WindowsPath('C:/Users/user1/Downloads/AnyDesk.exe'), WindowsPath('C:/Users/user1/Pictures/AnyDesk'), WindowsPath('C:/Windows/Prefetch/ANYDESK.EXE-0A8BB3A0.pf')] 
If someone could help me by optimizing the package to get the executable version, that would be great.

Otherwise I will close the topic.

Thank you.
User avatar
dcardon
WAPT Expert
Messages: 1932
Registration: June 18, 2014 - 09:58
Location: Saint Sébastien sur Loire
Contact :

February 15, 2024 - 4:57 PM

Hello Alexandre,

Have you tried using the getfileproperties function from setuphelpers?

Code: Select all

>>> get_file_properties(r'c:\\program files (x86)\\wapt\wapt-get.exe')
{'Comments': '',
 'CompanyName': 'Tranquil IT',
 'FileDescription': 'Wapt line command tool',
 'FileVersion': '2.5.4.15337',
 'InternalName': '',
 'LegalCopyright': 'Tranquil IT 2012-2024',
 'LegalTrademarks': '',
 'OriginalFilename': '',
 'PrivateBuild': '',
 'ProductName': 'WAPT',
 'ProductVersion': '2.5.4',
 'SpecialBuild': ''}
Denis Cardon - Tranquil IT
Share your experiences on WAPT! Send us your blog and article URLs in the "Your Opinion of the forum, and we'll feature them on the WAPT
Locked