How delete a file in system32 folder with python? - python

for the thesis work on forensic analysis I should try to automatically delete a file created and placed in the "system32" folder.
This is the code I ran:
os.system("C://Windows//System32//update.exe")
os.chmod("C://Windows//System32//update.exe", stat.S_IRWXU)
os.remove("C://Windows//System32//update.exe")
The error is as follows:
Traceback (most recent call last): File "C:\Users\xxxx\PycharmProjects\Tesi\main.py", line 5, in <module> os.chmod("C://Windows//System32//update.exe", stat.S_IRWXU) PermissionError: [WinError 5] Accesso negato: 'C://Windows//System32//update.exe'
How can I run it with the right permissions?

Depends on how you are running the Python interpreter. If you are using Powershell, you can Right-click the Powershell icon and run it as Administrator. After this, any Python script you run from that shell will also run with admin rights.

Related

Python Executable, PermissionError: [WinError 5]

I'm trying to write a chatting app that when run the first time deletes itself from the current position. Now the code is:
import os
import sys
file=sys.argv[0]
os.remove(file)
if i run it as .py file, it works just fine. Anyway if compile it with pyinstaller, when run by terminal it raises:
Traceback (most recent call last):
File "tests.py", line 6, in <module>
PermissionError: [WinError 5] Denied access: 'tests.exe'
[14320] Failed to execute script 'tests' due to unhandled exception!
nothing changes if i run it as administrator or assigning the file permissions with os.chmod. I have python 3.10, i tried both with python 3.9 and 3.10 and it does not work. I even tried running the command del with the subprocess module with the same result.
This works as .py file because the actual executable is the compiled version of the source code - a separate file/memory space. While the compilation is running the source code .py file is closed and can be removed.
When the program is compiled and run as a .exe file it will not remove itself because it is still open and running and therefore has a permission access denied error.
It is trying to remove itself because file = sys.argv[0]
(If you want to remove your running exe maybe start another program that does the removing from the exe and exit exe so that it is closed before removal - kind of awkward.)

Error when running program through virtual machine

We have written a python script to run within a forced speech alignment program. The speech alignment program was written with python but is run as an image through a Virtual Machine. The script instructs the program to align all of the files in a designated folder that is shared between the Virtual Machine and the computer's hard drive.
The command line of the VM image points to the shared folder where the files and the python script are located. It successfully opens the python script but once we try to open the directory specified in the script it says that there is no such file found. Would this have anything to do with the fact that it is being run in the virtual machine?
Here is the part of the script that points to the directory:
import sys
import os
def main():
direct=r'/Desktop/Shared/sf_VM_Shared/faseAlign/F3'
for file in os.listdir(direct):
and here is the error message:
ubuntu#BCE:~/Desktop/Shared/sf_VM_Shared/faseAlign/F3$ python3 FASE.py
Traceback (most recent call last):
File "FASE.py", line 19, in <module>
main()
File "FASE.py", line 9, in main
for file in os.listdir(direct):
FileNotFoundError: [Errno 2] No such file or directory: '~/Desktop/Shared/sf_VM_Shared/faseAlign/F3'

How to run a Python script in VSCode using the same path of the script? (otherwise it can't find an input file located in the same folder)

I have this Python code: https://github.com/andreagrandi/aoc_2019/tree/master/aoc
which runs perfectly from the terminal (Python 3 required) if I do for example: python aoc_03.py
But if I try to run it from VSCode, taking advantage of the Python extension and integration, I get this error:
(aoc) ➜ advent_of_code_2019 git:(master) /Users/andrea/.virtualenvs/aoc/bin/python /Users/andrea/Documents/advent_of_code_2019/aoc/aoc_03.py
Traceback (most recent call last):
File "/Users/andrea/Documents/advent_of_code_2019/aoc/aoc_03.py", line 70, in <module>
with open('aoc_03_input.txt', 'r') as file:
FileNotFoundError: [Errno 2] No such file or directory: 'aoc_03_input.txt'
My guess is that when invoked from VSCode, the script is run from a different path, so it cannot find the file aoc_03_input.txt which is located in the same folder of the script.
How do I tell VSCode to run my script from the /Users/andrea/Documents/advent_of_code_2019/aoc/ folder, so that it will be able to find my input file?
Thanks
Actually, I should have tried more before asking this question, because I just found the solution, but at this point I will write it here in case it can be useful to anyone:
If I change the path in this way:
with open('./aoc_03_input.txt', 'r') as file:
The file is being open correctly when I run the code in VSCode and when I run it from the terminal. Tested under OSX (but it should work under Linux too. Not sure about Windows).

Python open and "permission denied" on file with ugo+rw?

I have a script on a RHEL 7.x machine written in Python3. In testing this script I created a function which will append to a text file in the same directory.
If I execute the script from the local directory ie - ./pyscript.py everything works as expected.
But I am trying to execute this from a Bash script a couple directories higher and it doesn't seem to work right. The other functions in the script will execute, but this very last one which appends to a text file will not.
Now, if I run the script as the user which owns it(and the txt file) from my home dir, the script errors out with a permission error. BUT if I run the script with sudo it finishes with NO error, However it does NOT write to the text file.
My user has RW privileges on every dir between the bash script and the python script.
Any thoughts on why a sudo or local user run doesn't seem to let me write to the text file??
Edit
Traceback (most recent call last):
File "ace/ppod/my_venv/emergingThreats/et_pro_watchlists.py", line 165, in <module>
with open('etProLog.txt', 'a') as outlog:
PermissionError: [Errno 13] Permission denied: 'etProLog.txt'
If you use open("filename.txt", 'mode'), it will open that file in the directory from which the script is executed, not relative to the current directory of the script. If you want the path to directory where the script exists, import the os module and use open(os.path.dirname(os.path.abspath(__file__))+"filename.txt"). The permission error is because the file doesn't exist; sudo overrides that but does nothing because the file doesn't exist.

Permission Denied even after chmod 777

I have a python script that needs to access 3 files in a folder. When I run the script I get the following:
$ python mqtt_pub_test.py
Traceback (most recent call last):
File "mqtt_pub_test.py", line 10, in <module>
mqttc.tls_set(my_ca_cert, certfile=my_pri_cert, keyfile=my_key_cert)
File "C:\Python27\lib\site-packages\paho\mqtt\client.py", line 557, in tls_set
raise IOError(ca_certs+": "+err.strerror)
IOError: C:\Users\<user>\my_phone_certs: Permission denied
In an attempt to fix this I ran chmod 777 on everything in that path, even down to the files themselves!
The variables in mqttc.tls_set are pointing to the folder locations.
I have no idea why I am still getting permission denied at this point.
Any suggestions?
You are trying to access files which can not be edited:
mqttc.tls_set(my_ca_cert, certfile=my_pri_cert, keyfile=my_key_cert)
You could run the script as superuser (sudo python mqtt_pub_test.py), if in Linux.
If you are running in a cygwin like environment (which you described in the comments) you should use as file descriptor or let's say file pointer something like /cygdrive/c/Users/yourusername/phone_certs (or for git bash for instance /c/Users/youruser/phone_certs).
I would really suggest that you would move to cygwin entirely as it gives you a more complete linux-like environment on Windows which is very convenient to work with.

Categories

Resources