I want python to click Enter. However, I don't want to install any outside extensions. I want it to click enter without using them. I found many resources like win32 but I want to do it without using external resources. I don't mind which version of python it is on.
Is this possible?
If so, how?
I have looked on the web but couldn't find anything. Please, can someone help?
Thank you in advance.
Yes.
It should definitely be possible using the ctypes library to talk directly to the Windows dlls
If you don't want to install a helper module that has done all this for you, then you pretty much have to do it old school by going after the really low level code.
In short, call the SendInput function of user32.dll by using ctypes.windll.user32.SendInput. Good luck with getting the parameters correct - I don't have the patience to figure it out for you.
Here are the docs for the user32.dll SendInput API
Here is a helpful resource for figuring out the data types. This is actually part of ctypes and can be imported by import ctypes.wintypes but I find it instructional to read the actual code.
So for example, to create a WORD with the value of the Virtual Key Code VK_RETURN, I think it would be
>>> ctypes.wintypes.WORD(0x0D)
c_ushort(13)
But there is a lot more you have to piece together from there. Just build up your parameters and call the function.
Last hint, use the examples in the second link for how to build "C" type structures. Then build one yourself using ctypes.Structure
Related
So I'm trying to open a ppt application, and switch slides, and end the ppt using python.
I don't know how to call a macro from within python either. I've seen people use win32.client for it, but I'm on a mac. Is there any way for me to do these things and get a reference of the Presentation object within Python?
I've figured out the VBA macros (or so I think):
//starting a slideshow
Sub run()
ActivePresentation.SlideShowSettings.Run
End Sub
//for next slide
Sub next()
ActivePresentation.SlideShowWindow.View.Next
End Sub
Any advice would be much appreciated. I'm relatively new to coding and really grasping at straws at this point, Thanks a ton!
I tried looking up a bunch of places:
http://www.java2s.com/Code/VBA-Excel-Access-Word/PowerPoint/StartingaSlideShow.htm
https://www.youtube.com/watch?v=d0PknGechXI
They both use VBA or the win32 client. I don't mind creating the macros' code for VBA, but I dont know how to call a piece of macro code on a running PPT.
Can I introduce you to python-pptx?
It's a Python 3 library that I think will do what you want. (I am a fellow Mac user and have done a lot of work with this package.)
begin TLDR;
I want to write a python3 script to scan through the memory of a running windows process and find strings.
end TLDR;
This is for a CTF binary. It's a typical Windows x86 PE file. The goal is simply to get a flag from the processes memory as it runs. This is easy with ProcessHacker you can search through the strings in the memory of the running application and find the flag with a regex. Now because I'm a masochistic geek I strive to script out solutions for CTFs (for everything really). Specifically I want to use python3, C# is also an option but would really like to keep all of the solution scripts in python.
Thought this would be a very simple task. You know... pip install some library written by someone that's already solved the problem and use it. Couldn't find anything that would let me do what I need for this task. Here are the libraries I tried out already.
ctypes - This was the first one I used, specifically ReadProcessMemory. Kept getting 299 errors which was because the buffer I was passing in was larger than that section of memory so I made a recursive function that would catch that exception, divide the buffer length by 2 until it got something THEN would read one byte at a time until it hit a 299 error. May have been on the right track there but I wasn't able to get the flag. I WAS able to find the flag only if I knew the exact address of the flag (which I'd get from process hacker). I may make a separate question on SO to address that, this one is really just me asking the community if something already exists before diving into this.
pymem - A nice wrapper for ctypes but had the same issues as above.
winappdbg - python2.x only. I don't want to use python 2.x.
haystack - Looks like this depends on winappdbg which depends on python 2.x.
angr - This is a possibility, Only scratched the surface with it so far. Looks complicated and it's on the to learn list but don't want to dive into something right now that's not going to solve the issue.
volatility - Looks like this is meant for working with full RAM dumps not for hooking into currently running processes and reading the memory.
My plan at the moment is to dive a bit more into angr to see if that will work, go back to pymem/ctypes and try more things. If all else fails ProcessHacker IS opensource. I'm not fluent in C so it'll take time to figure out how they're doing it. Really hoping there's some python3 library I'm missing or maybe I'm going about this the wrong way.
Ended up writing the script using the frida library. Also have to give soutz to rootbsd because his or her code in the fridump3 project helped greatly.
I just installed this library that scrapes twitter data: https://github.com/kennethreitz/twitter-scraper
I wanted to find out the library's functions and methods so I can start interacting with the library. I have looked around StackOverflow on this topic and tried the following:
pydoc twitter_scraper
help(twitter_scraper)
dir(twitter_scraper)
imported inspect and ran functions = inspect.getmembers(module, inspect.isfunction)
Of the four things I have tried, I have only gotten an output from the inspect option so far. I am also unsure (excluding inspect) whether these codes should go in the terminal or a scratch file.
Still quite new at this. Thank you so much for reading everybody!
Excellent question! There are a few options in trying to grok (fully understand) a new library. In your specific case, twitter-scraper, the only function is get-tweets() and the entire library is under 80 lines long.
For the general case, in decreasing order of usefulness.
Carefully read the project's description on GitHub. The ReadMe is usually the most carefully written piece of documentation.
Larger libraries have formatted documentation at http://(package-name).readthedocs.org.
pydoc module_name works when the module is installed. ``help(module_name)works in an interactive Python session after you have done animport module_name. These both work from the "docstrings" or strategically placed comments in the source code. This is also whatmodule_name?` does in iPython.
dir(module_name) also requires an import. It lists all the entrypoints to the module, including lots of odd "dunder", or double underscore, you would not normally call or change.
Read the source code. Often, this is easier and more complete than the documentation. If you can bring up the code in an IDE, then jumping around works quickly.
Also, you asked about what can be used within a script:
import os
print("Welcome, human.")
print("dir() is a function, returning a list.")
print("This has no output")
a_list = dir(os)
print("but this does", dir(os))
print("The help() command uses pydoc to print to stdout")
help(os)
print("This program is gratified to be of use.")
It seems like this library lacks proper documentation, but the GitHub page provides some usage examples to help you get started.
>>> from twitter_scraper import get_tweets
>>> for tweet in get_tweets('kennethreitz', pages=1):
>>> print(tweet['text'])
P.S. your API is a user interface
s3monkey just hit 100 github stars! Thanks, y’all!
I’m not sure what this /dev/fd/5 business is, but it’s driving me up the wall.
…
To get more information, simply look at the source code at https://github.com/kennethreitz/twitter-scraper/blob/master/twitter_scraper.py. It seems like the only function is get_tweets, which, looking at the source code, takes in two arguments, the username and the number of pages (optional, defaults to 25).
How does one specify --null-audio option working with python API (http://trac.pjsip.org/repos/wiki/Python_SIP_Tutorial)?
Thanks.
Somehow you're wasting hours of time searching and then, desperate, you post a question to Stackoverflow, and the exact moment after it you find an answer by yourself. This place is the most useful problem solver ever.
After you've created your lib, call lib.set_null_snd_dev(). That must be done after making lib.init (here's my example:)
lib = pj.Lib()
lib.init(log_cfg=pj.LogConfig(level=3, callback=log_cb))
lib.set_null_snd_dev()
What happens behind the scenes when we invoke the python prompt?
I am more interested in understanding how do the methods/functions like print, import and the likes load up in the interpreter?
EDIT: Some clarification on my question:
When we type python on our unix prompt/windows console and Hit enter, what are the libraries that get loaded. My specific interest is as to how the keywords like print and import are made available to the user.
Like you , I am very interested by the underlying mechanisms of Python.
I think you'll love this series:
http://tech.blog.aknin.name/category/my-projects/pythons-innards/
There are too many levels to that question. Here's a very rough sketch.
There's the whole C-level interpreter initialization, a bunch of in-the-interpreter tasks, reading the environment and options, customization of the interpreter session. All that defines what you see when you run python.
I know there's a good description of the whole process somewhere.