Advertisement
Guest User

Untitled

a guest
May 9th, 2016
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. BITS 32
  2.               org     0x08048000
  3. ;Mini LEGAL ELF header
  4. ;http://www.muppetlabs.com/~breadbox/software/tiny/teensy.html
  5. ehdr:                                                 ; Elf32_Ehdr
  6.               db      0x7F, "ELF", 1, 1, 1, 0         ;   e_ident
  7.       times 8 db      0
  8.               dw      2                               ;   e_type
  9.               dw      3                               ;   e_machine
  10.               dd      1                               ;   e_version
  11.               dd      _start                          ;   e_entry
  12.               dd      phdr - $$                       ;   e_phoff
  13.               dd      0                               ;   e_shoff
  14.               dd      0                               ;   e_flags
  15.               dw      ehdrsize                        ;   e_ehsize
  16.               dw      phdrsize                        ;   e_phentsize
  17.               dw      1                               ;   e_phnum
  18.               dw      0                               ;   e_shentsize
  19.               dw      0                               ;   e_shnum
  20.               dw      0                               ;   e_shstrndx
  21.  
  22. ehdrsize      equ     $ - ehdr
  23.  
  24. phdr:                                                 ; Elf32_Phdr
  25.               dd      1                               ;   p_type
  26.               dd      0                               ;   p_offset
  27.               dd      $$                              ;   p_vaddr
  28.               dd      $$                              ;   p_paddr
  29.               dd      filesize                        ;   p_filesz
  30.               dd      filesize                        ;   p_memsz
  31.               dd      5                               ;   p_flags
  32.               dd      0x1000                          ;   p_align
  33.  
  34. phdrsize      equ     $ - phdr
  35.  
  36. _start:
  37.     pop esi         ; Get the number of arguments
  38.  
  39.     dec esi      ; If there are no arguments just exit instantly
  40.     jz _noArgsExit
  41.  
  42.     pop ecx         ; Pop the program name, we don't need this so we'll just overwrite it
  43.  
  44.     pop ecx           ; Get first argument
  45.     mov ebx, 0x80808080 ;Store the himagic in ebx so we can speed things up a little
  46.     ;Listen: I don't give a damn if you don't like the fact that I'm using a non
  47.     ;general purpose register for this, it doesn't get used and it's much faster
  48.     mov ebp, 0x7F7F7F7F
  49.     ;compare ecx with '-n' to see if they're the same
  50.     mov ah,`\n`     ;The newline character
  51.     mov edx, [ecx]
  52.     and edx, 0xFFFFFF ; Mask the bits we don't need
  53.     cmp edx,`-n\0`;Check for '-n'
  54.     jne _main
  55. _removenl:
  56.     xor ah,ah ;Removes the newline character from memory
  57.     xor edx,edx ;Set edx back to zero from before to prevent segfaults
  58.     dec esi
  59.     jz _exit
  60.     pop ecx
  61. _main:
  62.     ;strlen(edx)
  63.     ;Here we have an assembly implementation of glibc's strlen.c
  64.     ;Yes, that's right, I'm using *that* method because it's REALLY fast
  65.     mov edx, ecx
  66.     ;Get the string length for string edx and put it in eax
  67. _s:
  68.     mov edi,[edx]
  69.     add edx,4     ;Move to the next 'double word' (because we'll be decreasing from it)
  70.     ; Wooo magical numbers!
  71.     and edi, ebp
  72.     sub edi, 0x01010101
  73.     and edi, ebx
  74.     xor edi, 0  ;compare edi with 0
  75.     jz _s ;If none of them were zeros loops back to s
  76.     ;otherwise let's track down the one that was zero which will be represented a 0x80
  77.     sub edx, 4      ;Remove the 'add edx, 2' that we did before
  78.     ; mov edx again, so that we can test the actual value, since our cool magic
  79.     ; number stuff that we did before destroys edx, which means that character
  80.     ; 128 will cause misfires
  81.     mov edi,[edx]
  82.     test edi, 0xFF
  83.     jz _cont
  84.  
  85.  
  86.     inc edx
  87.     test edi, 0xFF00
  88.     jz _cont
  89.  
  90.  
  91.     inc edx
  92.     test edi, 0xFF0000
  93.     jz _cont
  94.  
  95.     inc edx
  96.     test edi, 0xFF000000
  97.     jnz _s ;If it was a misfire, go back and continue
  98.  
  99. _cont:
  100.     mov edi,ecx ;Save the original starting point in edi, we don't want to modify ecx
  101.     sub edx, edi  ;Get the difference
  102.     mov byte [ecx+edx],32 ; Put a space in between each argument to replace the string terminator
  103.     dec esi          ; Decrease arg count
  104.     jnz _main        ; If this is the last argument exit
  105.  
  106.  
  107. _exit:
  108.     ;Append a newline to the end if we have a newline
  109.     mov [ecx+edx],ah
  110.     inc edx ;Increase the length by one
  111.     ; Print the string
  112.     ;String length should already be in edx
  113.     ;String should already be in ecx
  114.     ;mov edx,edx     ; String length
  115.     ;mov ecx,ecx     ; String
  116.     xor ebx, ebx
  117.     inc ebx
  118. ;    mov ebx,1       ; stdout
  119.     ;mov eax,4       ; sys_write
  120.     xor eax, eax
  121.     mov al, 4
  122.     ;push _sysentercont
  123.     ;push ecx
  124.     ;push edx
  125.     ;push ebp
  126.     ;mov ebp, esp
  127.     int 0x80
  128.     ;sysenter        ; Kernel interrupt
  129. _sysentercont:
  130.     ;Exit with code 0
  131.     ;xor ebx, ebx
  132.     dec ebx
  133. _noArgsExit:
  134.     xor eax, eax
  135.     inc eax
  136. ;    mov eax, 1
  137. ;    mov ebp, esp
  138.     int 0x80
  139.  
  140. filesize      equ     $ - $$
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement