Buffer overflow Part(2)



က်ေနာ္တို့ part(1) မွာ stack နဲ့ registers ေတြဘယ္လိုအလုပ္လုပ္လဲဆိုတာကိုနားလည္ခဲ့ျပီ။ အခု windows ေပၚမွာ freefloat FTP server ကိုသံုးျပီးေတာ့ buffer overflow ကိုစမ္းၾကည္​ၾကမယ္။စမ္းတဲ့ေနရာမွာ က်ေနာ္က freefloat FTP server Version (1) ကိုအသံုးျပဳျပီးစမ္းျပမယ္။ဘာလို့လဲဆိုေတာ့  freefloat FTP server က username ေနရာမွာ buffer overflow vulnerability ရွိတယ္။ ျပီးေတာ့ အဲ့ vulnerability ကိုအသံုးခ်ျပီး target system ရဲ့ shell ကို ရေအာင္ယူၾကည့္မယ္။ က်ေနာ္တို့ ဒီ lab မွာ
freefloat FTP server run ထားမဲ့ windows XP (32 bits) VM တစ္လံုး၊ kali linux VM တစ္လံုးကိုသံုးမယ္။ debugger အေနနဲ့ကေတာ့ immunity debugger သံုးမယ္။
debugger ဘာလို့သံုူးရတာလဲဆိုရင္ stack ရဲ့အေျခအေနနဲ့ register values ေတြကိုသိခ်င္လို့ပါ။  freefloat FTP server ကိုေတာ့ https://www.exploit-db.com/exploits/23243/ link ကေန Vulnerable App ကိုနွိပ္ျပီးေတာ့ download ျပဳလုပ္နိုင္ပါတယ္။အဲ့ေတာ့ requirement ေတြလည္းသိျပီဆိုေတာ့ စလိုက္​ၾကရေအာင္ဗ်ာ။

 အရင္ဆံုး freefloat FTP server ကို windows XP ‌ေပၚမွာ run မယ္​။ debugger မွာ attach လုပ္မယ္။
attach လုပ္ျပီးရင္ x button ညာဘက္ေဘးက run button ေလးကိုနွိပ္ေပးရပါမယ္။



ျပီးရင္ က်ေနာ္တို့ က server ရဲ့ username ေနရာကို A အလံုး 1000 ပို႔ၾကည့္မယ္။
အဲ့လို ပို႔ဖို႔အတြက္ ေအာက္က python နဲ့ေရးထားတဲ့ code ကိုအသံုးျပဳမယ္။



ျပီးရင္ exploit.py အေနနဲ့ save လုပ္ျပီး python exploit.py <server IP> ဆိုျပီး run လိုက္မယ္။
debugger မွာ ဘာျဖစ္သြားမလဲ တစ္ခ်က္ျကည့္ျကည့္မယ္။

EIP value ကို A ေတြ overwrite ျဖစ္သြားတာကိုေတြ့ရမယ္။အဲ့ေတာ့ က်ေနာ္တို့ EIP ကို ထည့္လိုက္တဲ့ buffer က overwrite ျဖစ္သြားတယ္​ဆိုတာေတာ့သိျပီ။ part(1) မွာေျပာခဲ့သလိုပဲ ဘယ္နွစ္လံုးေျမာက္မွာ overwrite ျဖစ္လဲဆိုတာကိုသိရင္ EIP ကို control လုပ္လို့ရျပီေပါ့။ က်ေနာ္တို့  EIP ရဲ့ offset ကို ရွာဖို့အတြက္ kali linux မွာ ပါတဲ့ pattern_create နဲ့ pattern_offset ဆိုတဲ့ ruby script နွစ္ခုကိုသံုးမယ္။
အဲ့ script နွစ္ခုကိုသံုးဖို့ဆိုရင္ အရင္ဆံုး kali မွာ
ပံုထဲကအတိုင္း script ရွိတဲ့ directory ကိုသြား၊ output ထုတ္ခ်င္တဲ့ pattern အေရအတြက္နဲ့အတူ run ေပးလိုက္ရံုပါပဲ။က်ေနာ္ကေတာ့ အလံုး 1000  ထုတ္ထားတယ္။ျပီးရင္ေတာ့ က်ေနာ္တို့ python code ထဲက A ေနရာမွာထြက္လာတဲ့ pattern ကိုထည့္ျပီး server ကိုတစ္ခ်က္ျပန္ပို့ျကည့္မယ္။အဲ့ေတာ့ python code က

(က်ေနာ္ တို့ code ကိုတစ္ခါ run ျပီသြားတိုင္း Server ကိုျပန္ run ျပီး debugger နဲ့ ျပန္ attach လုပ္ေပးဖို့လိုပါတယ္)အေပၚက code ကို run ျပီးတဲ့အခ်ိန္မွာျဖစ္ေနတဲ့ debugger ရဲ့ အေျခအေနေလးတစ္ခ်က္ေလာက္ျကည့္ျကည့္ရေအာင္။
EIP ထဲကို 69413269 ဆိုတဲ့ ဂဏန္းေတြေရာက္ေနတာေတြ့ရပါမယ္။ ဘယ္နွစ္လံုးေျမာက္လဲသိခ်င္ရင္ေတာ့ ခုနက kali linux က pattern_offfset ဆိုတဲ့ script ကိုသံုးရပါမယ္။
Ok?? က်ေနာ္တို့ကို script က 247 လံုးမွာ EIP ကို overwrite ျဖစ္သြားတယ္လို့ေတာ့ေျပာေနျပီ။က်ေနာ္တို့ကတကယ္ဟုတ္မဟုတ္ေသခ်ာေအာင္ A 247 လံုးနဲ့ B ေလးလံုးပို့ျကည့္မယ္။ သူရွာေပးထာတဲ့ offset ကသာအမွန္ဆိုရင္ EIP ေနရာကို B ေတြ၀င္ေနရမယ္။အဲ့ေတာ့ python code ကိုျပန္ျပင္မယ္။
code ကိုျပင္ျပီးရင္ run ျပီးေတာ့ debugger မွာ EIP အေျခအေနကိုတစ္ခ်က္ျကည့္ျကည့္မယ္။

EIP ထဲကို 42424242 ဆိုျပီး B ရဲ့ value ေတြ၀င္ေနတာကိုျမင္ရမွာပါ။အဲ့ေတာ့ EIP ကိုလည္း control လုပ္နိုင္ျပီဆိုေတာ့ shellcode ကို stack ေပၚတင္ျပီးေတာ့ အဲ့ shellcode ရွိတဲ့ address ကို EIP ကိုခုန္ခုိင္းလိုက္ရင္ ok ျပီေပါ.။ ဒီေနရာမွာ တစ္ခုစဥ္းစားရမယ္ဗ်။ EIP ကိုခုန္ခိုင္တာေတာ့ဟုတ္ပါျပီ။ shellcode ကိုဘယ္နားသြားထည့္ျကမလဲ။
ပံုမွာ ESP ကိုျကည့္ျကည့္ပါ။ ESP က က်ေနာ္တို့ EIP အတြက္ B 4 လံုးျပီးေတာ့ ေနာက္ထပ္ထည့္ခဲ့တဲ့ C ေတြရွိတဲ့ addres ကုိ point လုပ္ေနတာကိုေတြ့ရမွာပါ။ အဲ့ C ​ေတြ​ေနရာမွာသာ shellcode ကိုထည္​့ၿပီးအဲ့​ေန႔ရာကိုခုန္​ခိုင္​းလိုက္ရင္​ ok ၿပီ​​​ေပါ့။ ဒါ​ေပမဲ့ က်​ေနာ္​ part(1) မွာ​ေျပာခဲ့သလိုပဲ ပံုကိုတစ္​ခ်က္
​ၾကည္​့ၾကည္​့ပါ။ ESP က  C ​ရဲ႕အစက​ေနစမလုပ္​ဘဲ ​8 လံုး​ေျမာက္​က​ေနမွ စလုပ္​တာကို​သတိထားမိပါလိမ့္မယ္။ အဲ့ဒါ​ေၾကာင္​့ က်​ေနာ္​တို႔ NOP ကိုသံုးတာပါ။ NOP 8 bytes သာထည္​့ၿပီး shellcode ထည္​့လိုက္​ရင္​တန္​းၿပီးအလုပ္​လုပ္​ၿပီ​။ အဲ့အတြက္​ စဥ္​းစားရမွာတစ္​ခု႐ွိ​ေသးတယ္​။ အဲ့ ESP ကိုခုန္​ဖို႔အတြက္​ EIP ​ေနရာမွာ ဘာသြားထည္​့ၾကမလဲ။ ႐ွင္​းပါတယ္။ ESP ကိုခုန္ခိုင္​းတဲ့ operation ျဖစ္​တဲ့ jmp esp ရဲ႕ address ကို႐ွာထည္​့​ေပးလိုက္​ရင္အလုပ္လုပ္ျပီ...။ shellcode generate မလုပ္​ခင္​​မွာ jmp esp ​ရဲ႕ address ကို အ႐ွင္​႐ွာၾကရ​ေအာင္​။ အဲ့လို႐ွာဖို႔အတြက္​ mona ဆိုတဲ့ python script ကိုသံုးပါမယ္​။ mona.py ဆိုတဲ့ python script ကို download ျပဳလုပ္​ၿပီး immunity debugger ကို install လုပ္​ထားတဲ့ C:/Program Files/Immunity Inc/Immunity debugger/PyCommands ​ေအာက္​ကို copy ကူးထည္​့​ေပးလိုက္​ပါ။ ၿပီးရင္​​ေတာ့ immunity debugger မွာ
ဆိုတဲ့ command ေလးကိုေအာက္နားမွာရွိတဲ့့ command box ေလးမွာရိုက္လိုက္ပါ။ "FFE4" ဆိုတာက jmp esp instructuon ရဲ့ opcode ျဖစ္ပါတယ္။ command အလုပ္လုပ္ပံုကိုေျပာရရင္ jmp esp instructions ပါတဲ့ address ေတြကို user32.dll ဆိုတဲ့ထဲမွာသြားရွာတာပါ။
ပထမဆံုး address "0x7E429353" ကိုယူလိုက္မယ္။ အဲ့ေကာင္ကို EIP ေနရာမွာ ထည့္မယ္။ ေရးတဲ့ေနရာမွာ intel x86 cpu architecture  က little endian format ကိုသံုးတဲ့အတြက္ ေျပာင္းျပန္ထည့္ရပါမယ္။အခုထည့္မဲ့ address က "0x7E429353" ဆုိေတာ့ code မွာေရးရင္ "\x53\x93\x42\x7E" ဆိုျပီးေရးရပါမယ္။အဲ့ေတာ့ code ကိုတစ္ခ်က္ျကည့္ျကည့္ရေအာင္။
nop ျပီးေတာ့ "\xCC" 4  bytes ထည့္ထားပါတယ္။ အဲ့ဒါက program ကို interrupt ျဖစ္ေစတဲ့ INT ဆိုတဲ့ instruction ရဲ့ opcode ပါ။ အဲ့ေတာ့ code ကို run လိုက္ရင္ EIP က "\xCC" ရွိတဲ့ေနရာကိုခုန္သြားျပီး sever က interrupt ျဖစ္သြားမယ္။ ဟုတ္မဟုတ္သီရေအာင္ run ျကည့္ျကည့္မယ္။
ဟုတ္ျပီ။ "\xCC" ဆီကိုလည္းေရာက္တယ္။ program လည္း INT command ကို လုပ္ေနတယ္ဆိုတာကို  debugger ရဲ့ ဘယ္ဘက္ေထာင့္နားမွာျပေနတယ္။ အခုက်ေနာ္တို့ shellcode generate လုပ္ျပီး စမ္းျကည့္ဖို့ပဲလိုေတာ့တယ္။ shellcode ကို reverse_shell အတြက္ မထုတ္ခင္မွာ calculater အတြက္ အရင္ထုတ္​မယ္။ run ျကည့္လို့ target machine မွာ calculator run ျပီဆိုမွ reverse_shell အတြက္​ shellcode ထုတ္ျပီး ထပ္စမ္းမယ္။
shellcode generate command for calculator
buf[] = ေနာက္ကစျပီးအကုန္ copy ကူးျပီး က်ေနာ္တို့ရဲ့ exploit code မွာသြားထည့္မယ္။အဲ့ေတာ့ အခု code တစ္ခ်က္ျကည့္မယ္။
​​​(do not forget to add `+´ signs in shellcode)
ဒီ exploit code ကို run လိုက္ရင္ target machine မွာ calculator က auto run ရမယ္။ ဒီ code မွာဆိုအရင္ကနဲ့မတူတာတစ္ခုကိုကိုသတိထားမလားဗ်။ nop ကိုအရင္က 8 bytes  ပဲထည့္ခဲ့တယ္။ ခုက်ေနာ္ 24 bytes ေတာင္ထည့္ထားတယ္။ က်ေနာ္ debugger နဲ့  attach မလုပ္ဘဲ run တဲ့အခါ shellcode အလုပ္မလုပ္တဲ့အတြက္​အတိုးအေလွ်ာ့လုပ္ျကည့္တာပါ။က်ေနာ္ေတာ့ nop 24 bytes ထည့္ျပီးတဲ့အခါမွာ အလုပ္လုပ္သြားတယ္။ က်ေနာ္ကေတာ့ nop ကိုအတိအက်သံုးတာထက္ nop မ်ားမ်ားသံုးရတာကိုပိုသေဘာက်တယ္ဗ် XD။ ပို safe ျဖစ္တယ္လို့ခံစားရလို့ပါ။ code ကို run ျကည့္လိုက္မယ္။
Yeah!! calculator run သြားျပီ။ reverse_shell အတြက္ shellcode ထုတ္ျပီး calculator အတြက္ shellcode ေနရာမွာ အစားထိုးလိုက္ရင္ ok ျပီေပါ့။
reverse_shell shellcode generate command
LHOST​​ေနရာမွာ kali machine ရဲ႕ ip ကိုထည္​့ပါ။
final exploit.py
မ run ခင္ က်ေနာ္တို့  reverse_shell ျပန္လာမဲ့ port မွာ listen လုပ္ေနမယ္။
windows မွာ freefloat ကို run ျပီး kali ကေန exploit code ကို run လိုက္မယ္။
Done! reverse_shell ရပါျပီ။

notes**
ဒီ lab မွာအေပၚမွာေျပာခဲ့သလိုပဲ exploit code ကိုတစ္ခါ run ျပီးတာနဲ့ windows မွာ freefloat ကို ျပန္ run ျပီး debugger နဲ့ ျပန္ attach လုပ္ရပါမယ္။ တစ္ခါ run ျပီးတိုင္ တစ္ခါ attach လုပ္ရပါမယ္။
ေနာက္တစ္ခုက "C" ျပီးေတာ့ 1000 ထဲက နုတ္သြားတဲ့ value ေတြ၊ အဲ့ဒါ​ေတြက အေရွ့မွာ ထည့္ခဲ့တဲ့ bytes အေရအတြက္ကိုျပန္နွုတ္တာပါ။
ဥပမာဗ်ာ final exploit.py မွာဆိုရင္ A က 247 ၊ EIP အတြက္ address က 4 ၊ nop က 24 ၊ shellcode ရဲ့ space က 351 bytes ပါ။ နုတ္သြားတဲ့ တန္ဖိုးေလးေတြကို သတိထားျကည့္ေစခ်င္ပါတယ္။
ျပီးေတာ့ shellcode generate လုပ္တဲ့အခ်ိန္မွာ -b ေနာက္က "\x00\x0A\x0D" ေတြဟာ bad character ေတြပါ။ shellcode ထဲ အဲ့ character ေတြပါေနရင္ program execution ကိုရပ္သြားေစနိုင္ပါတယ္။ အဲ့ဒါေျကာင့္ shellcode generate လုပ္တဲ့ေနရာမွာ အဲ့ဒါေတြကိုမပါေအာင္ -b options နဲ့ဖယ္ထားခဲ့တာပါ။


****Thanks*****

Popular posts from this blog

Docker containers ေတြအျမဲတမ္း up and running ျဖစ္ေနေအာင္ docker's restart policy ေတြကိုဘယ္လိုသံုးသင့္လဲ။

Ubuntu 18.10 Release Date and New Features