First of all, how to import the module.
howToImport.py
from ctypes import *
Next, how to use C library dynamic linking and functions.
howToLoadLib.py
msvcrt=CDLL("C\\WINDOWS\\System32\\msvcrt.dll")
str="On Python!!"
msvcrt.printf("Hello World : %s",str)
By the way, there are several methods of dynamic linking, such as CDLL (), windll (), oledll, etc. I'm a complete beginner, so it's a bit confusing, but maybe I'll use it properly depending on the OS ...?
By the way, input functions such as gets () can also be used normally. Also, for dynamic linking,
howToLoad_2.py
msvcrt=cdll.msvcrt
But it seems that it can be done. (I wonder if this is more common ...?)
I don't know this in detail either, but maybe it can be used if it is environment-dependent and is in the standard library directory ...? You can do it normally without writing an absolute path here.
In Python, you generally don't have to specify a type for a variable, but in C you have to specify a type for a variable. And since there is no C type in Python, ctypes has it for you! For example
howToHensu.py
str=c_char_p("AAA")
hensu.c
char *str="AAA"
Two programs like this are probably the same. I don't know the details, but maybe what I'm doing is the same. This is just one example, char, int, short, long, double, float, void * and many more.
By the way, if you want to pass by reference in C language, use a method called byref (). For example, if you want to pass a pointer to a function argument, you can use it like myfunc (byref (pointer)). In this case pointer is just a variable.
You can also use structures and unions as well as variables.
howToStruct.py
class Test(Structure):
_fields_=[
("testInt",c_int),
("testChar",c_char),
]
You can also make a structure like this. (I will omit the union because I haven't studied enough and I don't know how to use it yet.)
By the way, I bought a book called "Reverse Engineering-Binary Analysis Techniques with Python-" and started reading this story, so I wrote it. I've just started reading, so I'd like to upload it again if I read a little more! URL: Reverse Engineering-Binary Analysis Techniques with Python-
By the way, this is Qiita's first time, so I hope you can see it with warm eyes ... w I'm a complete beginner high school student, so it would be very helpful if you could point out any mistakes!
Recommended Posts