Cannot access one .txt file with two scripts - python

Here is my problem :
I got a python program that writes into a file, and a batch one that reads the file then. So I got my python code (simplified) :
import os
file = open("test.txt", "w")
file.write(Chooseentry.get()+"\n")
for i in hal:
file.write(i+"\n")
file.close()
os.startfile (r"C:/Hack/Project/Read.bat")
So I start my batch after having closed the file. It is supposed not to be used anymore by python, right ?
But then my batch file that reads it tells me :
The system cannot find the file test.txt
The process cannot access to the file because this file is already used by another process
Soooo... How can I close properly my file in python so that I can use it with my batch then ?
Thanks by advance for any help ! ^^

Related

Decoding a Payload using GitHub Decoder Script

Abstract:
I am analysing a pcap file, with live malware (for educational purposes), and using Wireshark - I managed to extract few objects from the HTTP stream and some executables.
During my Analysis, I found instances hinting Fiestka Exploit Kit used.
Having Googled a ton, I came across a GitHub Rep: https://github.com/0x3a/tools/blob/master/fiesta-payload-decrypter.py
What am I trying to achieve?
I am trying to run the python fiesta-payload-decrypter.py against the malicious executable (extracted from the pcap).
What have I done so far?
I've copied the code onto a plain text and saved it as malwaredecoder.py. - This script is saved in the same Folder (/Download/Investigation/) as the malware.exe that I want to run it against.
What's the Problem?
Traceback (most recent call last):
File "malwaredecoder.py", line 51, in <module>
sys.exit(DecryptFiestaPyload(sys.argv[1], sys.argv[2]))
File "malwaredecoder.py", line 27, in DecryptFiestaPyload
fdata = open(inputfile, "rb").read()
IOError: [Errno 2] No such file or directory: '-'
I am running this python script in Kali Linux, and any help would be much appreciated. Thank you.
The script expects two args... What are you passing it?
Looks like it expects the args to be files and it sees a -, (dash), as the input file.
https://github.com/0x3a/tools/blob/master/fiesta-payload-decrypter.py#L44 Here it looks like the first arg is the input file and second is the output file.
Try running it like this:
python malewaredecoder.py /Download/Investigation/fileImInvestigating.pcap /Download/Investigation/out.pcap
All that said, good luck, that script looks pretty old and was last modified in 2015.

How to execute a python file using txt file as input (to parse data)

I tried looking inside stackoverflow and other sources, but could not find the solution.
I am trying to run/execute a Python script (that parses the data) using a text file as input.
How do I go about doing it?
Thanks in advance.
These basics can be found using google :)
http://pythoncentral.io/execute-python-script-file-shell/
http://www.python-course.eu/python3_execute_script.php
Since you are new to Python make sure that you read Python For Beginners
Sample code Read.py:
import sys
with open(sys.argv[1], 'r') as f:
contents = f.read()
print contents
To execute this program in Windows:
C:\Users\Username\Desktop>python Read.py sample.txt
You can try saving the input in the desired format (line-wise) file, and then using it as an alternative to STDIN (standard input file) using the file subcommand with Python
python source.py file input.txt
Note: To use it with input or source files in any other directory, use complete file location instead of file names.

Python script not writing to txt file

I have a Python script that runs properly on my laptop, but when running on my raspberry pi, the following code does not seem to be working properly. Specifically, "TextFile.txt" is not being updated and/or saved.
openfile = open('/PATH/TextFile.txt','w')
for line in lines:
if line.startswith(start):
openfile.write(keep+'\n')
print ("test 1")
else:
openfile.write(line)
print ("test 2")
openfile.close()
I am seeing "test 1" and "test 2" in my output, so I know that the code is being reached, paths are correct, etc
It may be due to a permissions problem. I am running the script from the terminal by using:
usr/bin/python PATH/script.py
Python is owned by "root" and script.py is owned by "Michael".
My first guess:
Does the file exist? If it does not exist then you cannot write to it. Try this to create the file if it does not exist: file = open('myfile.dat', 'w+')
Additionally manually opening and closing file handles is bad practice in python. The with statement handles the opening and closing of the resource automatically for you:
with open("myfile.dat", "w+") as f:
#doyourcalculations with the file object here
for line in f:
print line
All, thank you for your input. I was able to figure out that it was writing to the new file, but it was overwriting with the same text. The reason was because ".startswith" was returning false when I expected true. The misconception was due to the difference between how Windows and Unix treat new line characters (/n /r).
Since your code is running, there should be a file somewhere.
You call "PATH/script.py", but there is "/PATH/TextFile.txt" in your program. Is the slash before PATH a mistake? Have you checked the path in your program is really where you are looking for the output file?

How to run a bat file from Python 3.3.2 [duplicate]

This question already has answers here:
Run a .bat file using python code
(9 answers)
Closed 8 years ago.
I am developing a program in python, however I have a little problem because the data that my program needs, it is from the result of a bat file, so I would like to create something that I help me to run just my script in python and not use the bat file directly.
Well, right now, I have to execute the following steps:
I create a txt file where I enter the input data (For example: A.txt)
I run the BAT file using the file A.txt
It creates a new file txt which has the result of running the BAT file (For example: B.txt)
I open the file B.txt and I copy all data and then I paste it in a new txt file, where it will serve as input data to my program in python
I run my program in python
it creates a new file txt where has the final results
End
How can I improve this, I mean, Which scripts I need to run my program in Python without to go to the Bat file and to do the steps that I describe above?
I'm pretty sure whatever you're doing in the bat file you can doit in a python module. So:
Write a python module sustitute for your *.bat
Write a python module containing a function for read the file A.txt and return the results. Then use that module from your program.
For instance:
# calculations.py, the bat substitute.
def calculate_from_input(file_name):
input_ = open(file_name, 'rb') # The mode you read the fike depends on your purposes.
# .... Do your calculations
return result # Return a list of velocities, for example.
Then in your main program
from calculations import calculate_from_input
calculated_data = calculate_from_input("A.txt") # Then, you have the data ready to use in your program.

Python file to open a text file and run other python files in the text file

I have a text file inside which I have paths to a few python files and the arguments that I would specify when I run them in a command prompt.
I am looking for a python script that opens up the text file and runs the python programs specified in the text file along with the provided arguments.
The text file will look something like
`C:\hello.py world
C:\square.py 5`
i think you should refer to below :
Calling an external command in Python
Step One
read all lines in your command file get a list of python script file name and arguments
like: " C:\hello.py and argument: word "
Step Two
call them in below code style
from subprocess import call
call(["python C:\hello.py", "word"])
......
I don't think this post deserves down voting. But from now on I would suggest to OP to look for a solution yourself, and then if you can't find the answer post on stack overflow!
from subprocess import call
with open("somefile.txt", 'r') as f:
some_files_to_run = [line.split('\n')[0] for line in f.readlines()]
for file_to_run in some_files_to_run:
call(["python", file_to_run])

Categories

Resources