Opening files with Pydbg while application is running - python

Using pydbg I'm opening files(ex. c:\\myfile.mnp) within a win32 application(Ex. c:\\myprog.exe) in this way.
dbg = pydbg()
dbg.load("c:\\myprog.exe", "c:\\myfile1.mnp")
If the target application is already running then, is it possible to open a another file(For example c:\myfile2.mnp ) within the same application which is already running without closing that process/apps, using pydbg?

From personal experience, its better to have python start the application, or attach to it while its running.
import pydbg
from pydbg import *
from pydbg.defines import *
import struct
import utils
dbg = pydbg()
pid = ''
name = ''
found_program = False
for (pid, name) in dbg.enumerate_processes():
if name.lower() == "program.exe":
found_program = True
dbg.attach(pid)
if found_program:
dbg.run()
To make python start it:
from os import system
system('start "c:\program.exe"')

Related

How to read url on active tab of browser

I am using python 2.7 but am not able to get anything how to read url which are active in tab of browsers.
So far i tried this
import win32gui
import win32process
import psutil
#w=win32gui
#value=w.GetWindowText(w.GetForegroundWindow())
#print(value)
appwindow = win32gui.GetForegroundWindow()
ProcessID = win32process.GetWindowThreadProcessId(appwindow)
#procname = psutil.Process(ProcessID)
#applicname = procname.name()
print("hello")
Check out pybrinf package, is Windows supported and allows you to get some info about a browser.

Python 3 script crash on lru_cache

I have raspberry pi acting as MFRC522 card reader. So far I have a script which worked on python 2.7, but when I tried to move to python 3, bizarre thing happened.
I used updated library for it: https://github.com/pimylifeup/MFRC522-python and adjusted my script accordingly
I installed pip3 and spidev
Here is where strange thing happens. If I run demo script from repo above in new folder it works, cool.
But if I put my previous script in the same folder it returns error as below. And after that if I try again to run demo script, same error occurs.
Moving both script to different folder, has the same effect. I can run demo only before my script. Doing that after, is impossible.
Error:
File "/usr/lib/python3.7/re.py", line 297, in <module>
#functools.lru_cache(_MAXCACHE)
AttributeError: module 'functools' has no attribute 'lru_cache'
My script:
import RPi.GPIO as GPIO
from mfrc522 import SimpleMFRC522
import time
import requests
import signal
import sys
import os
# some functions non related to MFRC522
reader = SimpleMFRC522()
while continue_reading:
try:
id, text = reader.read()
#print id
#print "---"
ver = query_server(id)
if ver == 1:
open_relay(5)
else:
#do something
finally:
time.sleep(0.5)
It's like folder is somehow tainted after firing my script, what am I missing?

Python or LibreOffice Save xlsx file encrypted with password

I am trying to save an Excel file encrypted with password. I have tried following the guide on https://help.libreoffice.org/Common/Protecting_Content_in - and works perfectly. However, this is in the GUI, but I am looking for a solution using the command line interface in headless mode.
I have looked at the man libreoffice, but I could not find anything in there.
Likewise I have looked at the documentation of the Python 3 library openpyxl, but I did not find anything useful there either.
Is it possible to save an Excel 2007+ file encrypted with a password on Ubuntu 14.04/16.04 using the command line (or Python library) that do not require any user interaction or X session?
There is solution using Jython and Apache POI. If you want like to use it from CPython/PyPy, you can use subprocess module to call external Jython script.
I assume that you have Java JRE/JDK installed
Create non-encrypted xlsx file with Excel/Calc or use xlsxwriter or openpyxl and save it as test1.xlsx
Download standalone Jython
Download Apache POI
Extract Apache POI in same dir where is standalone Jython jar
Save following Jython script as encrypt.py:
import os
import sys
from java.io import BufferedInputStream
from java.io import FileInputStream
from java.io import FileOutputStream
from java.io import File
from java.io import IOException
from org.apache.poi.poifs.crypt import EncryptionInfo, EncryptionMode
from org.apache.poi.poifs.crypt import CipherAlgorithm, HashAlgorithm
from org.apache.poi.poifs.crypt.agile import AgileEncryptionInfoBuilder
from org.apache.poi.openxml4j.opc import OPCPackage, PackageAccess
from org.apache.poi.poifs.filesystem import POIFSFileSystem
from org.apache.poi.ss.usermodel import WorkbookFactory
def encrypt_xlsx(in_fname, out_fname, password):
# read
in_f = File(in_fname)
in_wb = WorkbookFactory.create(in_f, password)
in_fis = FileInputStream(in_fname)
in_wb.close()
# encryption
out_poi_fs = POIFSFileSystem()
info = EncryptionInfo(EncryptionMode.agile)
enc = info.getEncryptor()
enc.confirmPassword(password)
opc = OPCPackage.open(in_f, PackageAccess.READ_WRITE)
out_os = enc.getDataStream(out_poi_fs)
opc.save(out_os)
opc.close()
# write
out_fos = FileOutputStream(out_fname)
out_poi_fs.writeFilesystem(out_fos)
out_fos.close()
if __name__ == '__main__':
in_fname = sys.argv[1]
out_fname = sys.argv[2]
password = sys.argv[3]
encrypt_xlsx(in_fname, out_fname, password)
Call it from console:
java -cp "jython-standalone-2.7.0.jar:poi-3.15/lib/commons-codec-1.10.jar:poi-3.15/lib/commons-collections4-4.1.jar:poi-3.15/poi-3.15.jar:poi-3.15/poi-ooxml-3.15.jar:poi-3.15/poi-ooxml-schemas-3.15.jar:poi-3.15/ooxml-lib/curvesapi-1.04.jar:poi-3.15/ooxml-lib/xmlbeans-2.6.0.jar" org.python.util.jython -B encrypt.py test1.xlsx test1enc.xlsx 12345678
Where:
encrypt.py - name of script
test1.xlsx - input filename
test1enc.xlsx - output filename
12345678 - password
Final encrypted xslx should be in test1enc.xlsx.

Import modules using RPYC

I'm trying to remote into an interactive shell and import modules within python 2.7. I'm getting hung up. So far this is what I've got:
import rpyc
import socket
hostname = socket.gethostname()
port = 12345
connections = rpyc.connect(hostname,port)
session = connections.root.getSession()
session exists
>>>session
<blah object at 0xMore-Goop>
I want to issue an import sys so I can add another module to the path. However when I try to see if modules exist in the path I get the following:
>>>connections.modules
AttributeError: 'Connection' object has no attribute 'modules'
What I need to execute remotely is the following:
import sys
sys.path.append(path/to/import)
import file
log = file.logger(session, path/to/log)
Is it possible to have rpyc issue the above content? Thanks in advance
You can add the following methods to the service:
import sys, importlib, rpyc
...
class MyService(rpyc.Service):
...
def exposed_import_module(self, mod):
return importlib.import_module(mod)
def exposed_add_to_syspath(self, path):
return sys.path.append(path)
and access it like this:
connections.root.add_to_syspath('path/to/import')
file = connections.root.import_module('file')
file.logger(session, 'path/to/log')

Import custom modules on IPython.parallel engines with sync_imports()

I've been playing around with IPython.parallel and I wanted to use some custom modules of my own, but haven't been able to do it as explained on the cookbook using dview.sync_imports(). The only thing that has worked for me was something like
def my_parallel_func(args):
import sys
sys.path.append('/path/to/my/module')
import my_module
#and all the rest
and then in the main just to
if __name__=='__main__':
#set up dview...
dview.map( my_parallel_func, my_args )
The correct way to do this would in my opinion be something like
with dview.sync_imports():
import sys
sys.path.append('/path/to/my/module')
import my_module
but this throws an error saying there is no module named my_module.
So, what is the right way of doing it using dview.sync_imports()??
The problem is that you're changing the PYTHONPATH just in the local process running the Client, and not in the remote processes running in the ipcluster.
You can observe this behaviour if you run the next piece of code:
from IPython.parallel import Client
rc = Client()
dview = rc[:]
with dview.sync_imports():
import sys
sys.path[:] = ['something']
def parallel(x):
import sys
return sys.path
print 'Local: ', sys.path
print 'Remote: ', dview.map_sync(parallel, range(1))
Basically all the modules that you want to use with sync_imports must already be in the PYTHONPATH.
If it's not in the PYTHONPATH then you must add it to the path in the function that you execute remotely, and then import the module in the function.

Categories

Resources