I developed many years in C and only now discovered that a program can execute code prior to main() function. Here is a code example
int generateNum(){ // Some malicious code here... return 5; } static int someArray[] = {generateNum(),generateNum()} int main(){ // Some code here... } The function generateNum() is called twice before main().
My questions are
- Who calls generateNum()? I know that on Windows it is crtexe()
- Is this behavior standardized on different platforms: Windows/Linux/Android/iOS?
- How can I get more information about this behavior? I want to search in Google, but I don't know how to describe it.
- Can I do anything I want inside the generateNum()? I mean, can I call malloc()? What about fopen() and fwrite()? Can I open a socket and send information over UDP? Eventually I can abuse this function and even call to main() from it :-)
mainis the first user-provided code to be executed.