Challenge Name
Problem
Exploit the function pointers in this program. It is also found in /problems/pointy_4_3b3533bd4e08119669feda53e8cb0502 on the shell server. Source.
Solution
The provided program allows us to enter students and professors. Students can then rate the professors.
The bug in the program is that we can select professors as students and students as professors. We can write the
lastScore
of aProfessor
and then treat it as aStudent
to control thescoreProfessor
field.The
Student
andProfessor
struct
s are as follows:scoreProfessor
is a function pointer that is in the same position relative to thestruct
aslastScore
is inProfessor
. We can create aProfessor
and set thelastScore
variable to the address ofwin()
. Then, we can convert that professor to a student, which will override thescoreProfessor
pointer and point it to thewin()
function. (scoreProfessor
is pointed togiveScoreToProfessor()
on line 79:student->scoreProfessor=&giveScoreToProfessor;
.) Thus, when thescoreProfessor
pointer is called (close to the end of the file:student->scoreProfessor(professor, value);
), thewin()
function will be executed and we will get the flag.Script (script.py) Walkthrough:
Allocate a student
s1
Allocate a professor
p1
Use student
s1
to give the scoreGive the score to professor
p1
Set the
lastScore
attribute of professorp1
to the address ofwin()
The loop in vuln.c restarts
Allocate another student
s2
(only necessary to proceed to next inputs in program)Allocate another professor
p2
(only necessary to proceed to next inputs in program)Use professor
p1
(which has the address ofwin()
) to give a score. This converts professorp1
to a student, thus overwriting thescoreProfessor
function pointer towin()
.Give the score to professor
p1
. This does not matter since we only care about the last line (student->scoreProfessor(professor, value);
) wherescoreProfessor
(akawin()
) is called. You could usep2
here as well since it is the extra professor we created earlier.Give a score of
0
to professorp1
, but not really since thescoreProfessor
function pointer points towin()
and notgiveScoreToProfessor()
since we changed it by converting aProfessor
to aStudent
.
Run the script.py
python script.py USER=<username> PASSWORD=<password>
:
Flag
picoCTF{g1v1ng_d1R3Ct10n5_c7465fbf}
Last updated