Windows Defender detecting Python EXE as Trojan - python

I made a Python script that mails a Windows directory as zip to me. I added a scheduler using sched module that repeats this every hour.
I was attempting to make a simple sync application for personal use that starts at Windows Startup.
I converted it to exe using pyinstaller --onefile argument and it worked flawlessly.
But after a couple of days, Windows defender (Windows Security Centre of Windows 10) detected it as a Trojan.
This issue is not limited to Windows Security Centre, A quick scan on Virus Total says 4 antivirus detects it as Trojan.
What brings the Antiviruses to think this? I don't think sharing the code is any use as proper details have already been given, but still, a comment would make me do it.

Most antiviruses will mark this as a Win32.Trojan.whatever because it will detect that the EXE executable is simply extracting binary code. This works just like a ZIP, where Gmail blocks ZIPs.
To summarize, this is like a self extracting EXE, so kind of like an installer. For example, when you execute an installer, you sometimes get the User Account Control window, which is detected when the system is trying to grant advanced permissions for the EXE.

It must be a something else that made Windows Defender suddenly think it's bad. Maybe it's an update, try updating it.

Related

Why is my PyInstaller .exe file marked as a virus

This has happened several times before. No matter what I put inside of a Python file and use PyInstaller, it will be marked as a virus no matter what place you download it from. I want to be able to have my users download my file, but it will just be marked as a virus. What is going on and is there a way to fix it?
Example
since there are so many malware developers that use python
they make there python scripts a executable so people don't read the code which causes it to get detected
it could also be getting detected because it drops files when you use --onefile
and my advice is you either downgrade pyinstaller or you convert it to c# or you contact anti virus vendors

python exe starts and stops immediately on windows 10

Hey guys I created a program, used pyinstaller --onedir 'name' and created the exe.
Now I am having an issue while it works perfectly on my computer (windows 7) when I took the dist folder to a windows 10 machine the cmd window started and closed at once without running and without giving out an error message (or it was fast enough that I could not see it).
Is this an issue with windows 10 or did I do anything wrong when compiling to exe?
What happens is that probably your dist directory requires different dlls, probably some system ones that you need to add them into your one directory executable so that everything is linked correctly.
One way to find the missing dependencies of your exe is to use Processor monitor https://learn.microsoft.com/en-us/sysinternals/downloads/procmon in your Win 10. You see the traces of your process and then you investigate.
One other way is to install Process explorer https://learn.microsoft.com/en-us/sysinternals/downloads/process-explorer and check the dlls that did load to see where it stops.
There is also https://dependencywalker.com/ which is really powerful, but when I used it in similar scenarios, it didn't help me that much.
Also have a look at the possible errors that pyinstaller might raise, even the slightest warning can be helpful.
One other concern would be the machine you are using: let's say you have a generated process which contains dlls with 64 bits that you are trying to run in a machine with 32 bits.
I hope this helps.

Installing a python script as a windows service or hidden

I wrote a script with a bunch of modules to convert txt files to pdfs. There's a watchdog module which checks a certain folder for newly created txts, python siphons the data into a latex template which is being compiled into a pdf.
I created my abomination in PyCharm and if I start the script within PyCharm, it works flawlessly. I compiled it as exe via 'pyinstaller --onefile' and it works flawlessly.
However, I need to run it as a windows service or at least without the window existing in the task bar because... reasons. I tried:
installing it as service via non-sucking service manager
starting it as hidden programme via a vbs script, powershell script and nircmd
and nothing helped. I'm at a loss here, there's no interaction whatsoever with the programme so it should work, also scripts are enabled on the machine and the process is visible in the task manager. I somehow suspect that it has problems calling other python scripts while it's a service or hidden programme but it doesn't seem to be logical and I have no means to verify anything whatsoever that I'm aware of.
My last idea is to somehow let it exist as a task bar tray thingy but that would be more than a shabby solution for this whole ordeal. Could anyone help me out here? Thanks

Make a Python app package/install for Mac

I have developed an application for a friend. Aplication is not that complex, involves only two .py files, main.py and main_web.py, main being the application code, and _web being the web interface for it. As the web was done later, it's kept in this format, I know it can be done with one app but not to complicate it too much, I kept it that way. Two two communicate with some files, and web part uses Flask so there's "templates" directory too.
Now, I want to make a package or somehow make this easier for distribution, on a OSX system. I see that there is a nice py2app thingy, but I am running Windows and I can't really use it since it won't work on Win. I also don't know will py2app make problems since some configs are in text files in the directory, and they change during the runtime.
So, I am wondering, is there any other way to make a package of this, some sort of setup like program, or maybe some script or something? Some simple "way" of doing this would be to just copy the files in the directory in the "Documents", and add some shortcuts to the desktop to run those two apps, and that would be it, no need for anything else. DMG would be fine, but not mandatory.
I believe what you are looking for is to add: #!/usr/bin/python to the first line of your code will allow your friend to just double click on the file and it should open. Just as a warning osx does not tell us what version and as such what version of python and what standard libraries are going to be present.
Also, just make sure that if they have played around with their settings to much and they double click on python it does not work they will have to choose to open the file in "terminal.app" in the Utilities Applications folder (/Applications/Utilities/terminal.app)
The other idea is borrow a mac and compile it with the py2app program that you already mentioned. Otherwise there is no generic binary file that you will be able to compile in windows and have run on mac.

How should I launch a Portable Python Tkinter application on Windows without ugliness?

I've written a simple GUI program in python using Tkinter. Let's call this program 'gui.py'. My users run 'gui.py' on Windows machines from a USB key using Portable Python; installing anything on the host machine is undesirable.
I'd like my users to run 'gui.py' by double-clicking an icon at the root of the USB key. My users don't care what python is, and they don't want to use a command prompt if they don't have to. I don't want them to have to care what drive letter the USB key is assigned. I'd like this to work on XP, Vista, and 7.
My first ugly solution was to create a shortcut in the root directory of the USB key, and set the "Target" property of the shortcut to something like "(root)\App\pythonw.exe (root)\App\gui.py", but I couldn't figure out how to do a relative path in a windows shortcut, and using an absolute path like "E:" seems fragile.
My next solution was to create a .bat script in the root directory of the USB key, something like this:
#echo off
set basepath=%~dp0
"%basepath%App\pythonw.exe" "%basepath%\App\gui.py"
This doesn't seem to care what drive letter the USB key is assigned, but it does leave a DOS window open while my program runs. Functional, but ugly.
Next I tried a .bat script like this:
#echo off
set basepath=%~dp0
start "" "%basepath%App\pythonw.exe" "%basepath%\App\gui.py"
(See here for an explanation of the funny quoting)
Now, the DOS window briefly flashes on screen before my GUI opens. Less ugly! Still ugly.
How do real men deal with this problem? What's the least ugly way to start a python Tkinter GUI on a Windows machine from a USB stick?
EDIT:
All the answers below were very good (py2exe, pyinstaller, small .exe, .wsf script.) The .wsf solution was the simplest, so I'm using it for now. I'll probably end up switching to one of the other three solutions if I want a prettier icon and the standard .exe extension. Thanks, everyone!
This Windows Scripting Host script (file extension .wsf) can be used instead of the batch file:
<job>
<script language="VBScript">
set WshShell = WScript.CreateObject("WScript.Shell")
CMDFile = "App\\pythonw.exe App\\gui.py"
WshShell.Run CMDFile, 1
</script>
</job>
Update: Alternatively compile this C program and link an icon resource:
#include <windows.h>
#include <process.h>
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPSTR lpCmdLine, int nCmdShow)
{
return _spawnl(_P_NOWAIT, "App/pythonw.exe", " App/gui.py", lpCmdLine, NULL);
}
Update 2: To build an App.exe with icon, save the C code to app.c, create an Windows icon file app.ico, and save the following line to app.rc:
appicon ICON "app.ico"
Using Visual Studio 2008 Express, run these commands:
"C:\Program Files\Microsoft Visual Studio 9.0\VC\vcvarsall.bat"
rc.exe app.rc
cl.exe app.c /FeApp.exe /link app.res
Alternatively use "Visual Studio 2010 Express" or "Microsoft Windows SDK v7.0 for Windows 7 and .NET Framework 3.5 Service Pack 1" and adjust the commands accordingly.
Note that the icon will only be used for the App.exe starter program, not your Python program.
Use pyinstaller to zip up your distribution (the advantage over py2exe is that it knows different third-party libraries and is generally more up-to-date).
You can then create a .exe for your users to click upon to start your application. If you just copy the results of the pyinstaller build onto your USB drive you should be fine.
Make it to a single executable using py2exe.
You could do this in a hacky manner by writing you're own little C application that calls system('start "" "%basepath%App\pythonw.exe" "%basepath%\App\gui.py"'). Next you compile it without console and use it as a "shortcut".
The Short Answer:
This question was asked a few years ago, but I recently found a solution for a program I was working on that may still be useful for others. With this method, you will be able to create a standalone exe program launcher that can be placed anywhere and refer to a file in its same folder or subdirectory, while having a pretty icon of your choice and no DOS screen popping up. In other words, a true good-looking relative-path transportable shortcut file :)
The solution should be easy to follow and do even for non-programmers and goes as follows:
open notepad
write: %windir%\system32\cmd.exe /c start "" "%CD%\optional
subfolder\mainpy2exeGUI.exe"
save as "whatever.bat"
convert the bat-file to an exe file using a program called "BAT to EXE converter"
while checking the "invisible application" option, and selection the
icon file you want under the "versioninformations" tab. You can name
the output exe file to whatever you want. Link to the converter
program can be found at
http://www.freewaregenius.com/how-to-create-shortcuts-with-a-relative-path-for-use-on-usb-drives/
The converter program download contains a 32 and 64-bit version, use the 32-bit version to make the shortcut usable by both older and newer PCs.
(note, this solutions is almost the same as suggested at http://www.freewaregenius.com/how-to-create-shortcuts-with-a-relative-path-for-use-on-usb-drives/. However the current solution is different in terms of the code it uses in step2 which allows the launcher progam to be placed anywhere on a computer and not just on the top directory of a USB-stick, and is new to emphasize that the invisible option should be checked. Those differences are crucial.)
More Details (optional):
The original question was: "What's the least ugly way to start a python Tkinter GUI on a Windows machine from a USB stick?"
What was needed can be broken down to four things:
1. An exe program launcher.
2. That works on any computer and in any directory (i.e. it supports relative paths).
3. That has an icon.
4. That does not open an "ugly" DOS window.
There were several possible solutions suggested but none so far that satisfies all criteria. The original poster went for the ".wsf" option which allowed for relative paths and no ugly DOS window, but did not allow a custom icon or the recognizable exe file.
Part of the problem with the previously suggested solutions include:
you do not have C/VB programming skills or software.
you want an icon to your launcher program. Using a shortcut file that executes "cmd" and uses it to open your GUI file will allow you to set an icon file, BUT the icon file reference is absolute and will fail on any other computer than the one you created the shortcut file on.
you do not want the "ugly" DOS window flash. The cmd shortcut solution mentioned in the previous point creates a DOS window that flashes before opening your GUI.
Making the py2exe main executable file as the program launcher would almost be a perfect solution because it satisfies all criteria, but a backdraw with it is that the py2exe ececutable would require an ugly "tlc" folder to be placed in the same top-directory. It is therefore better to hide the main py2exe launcher in a nicely named subfolder. Also, there are many cases where one would like to keep the program launcher and the program itself as separate exe files, for instance if you are only using your main py2exe program to function as a python-runner that can launch open-ended editable python scripts that you can edit on the go without having to create a new py2exe file for each time you make a change to one of your scripts.
You can also fork Portable Python sources on GitHub and create shortcut in the same way other Portable Python shortcuts are created.
This gives you nice way to start app, icon, you can set custom registry/env variables if you need to, etc etc.
As an example you can take e.g. IDLE shortcut from Portable Python sources.
I've made a batch script (PyRunEXE) which compiles a simple Assembly Language code to make an EXE launcher for you:
https://github.com/SzieberthAdam/pyrunexe

Categories