So I recently asked this question
I had to create an environment variable MYENV and store something in it such that I can successfully run this code.
#include <stdio.h>
#include <stdlib.h>
int main(){
int (*func)();
func = getenv("MYENV");
func();
}
Earlier I was doing something like export MYENV=ls
.
Which a user pointed out is incorrect as when the func() is called it basically tells C to run the instructions stored in the variable func
which would be the string ls
and is not a correct machine code. So I should pass some shellcode instead.
Now I want to know if this how it works for functions in general. As in when I declare a function let's say myFunction()
which does let's say multiply 100
and 99
and returns the value, then the variable myFunction
will point towards a set of machine instructions stored somewhere which multiplies 100
and 99
and returns the value.
And if I were to figure out those machine instructions and store them in a string and make myFunction
point towards it, and then if I call myFunction()
we'll have 9900
returned?
This is what I mean :
int (*myFunc)();
char *var = <machine_instructions_in_string_format>
int returnVar = myFunc();
Will the returnVar have 9900?
And if yes, how do I figure out what that string is?
I am having a hard time wrapping my head around this.