Advertisement
Guest User

GCHQ Stage 1 Solution

a guest
Dec 4th, 2011
3,316
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ; GCHQ canyoucrackit.co.uk stage 1 solution
  2.  
  3. ; Dr Gareth Owen, School of Engineering, University of Greenwich, England
  4.  
  5. ; Explanation: http://gchqchallenge.blogspot.com
  6. ; To run:
  7. ; nasm -f elf p1-complete.asm
  8. ; gcc -o p1 p1-complete.asm
  9. ; ./p1
  10.  
  11. global main
  12. main:
  13.  
  14. jmp start
  15.  
  16. ; unused - code for part 3
  17. scasd
  18. ret 0xa3bf
  19. ; unused - end code for part 3
  20.  
  21. start:
  22. sub esp,0x100    ; 4096 bytes
  23. xor ecx,ecx
  24.  
  25. ; loop through 256 bytes of memory and set values to equal offset (e.g. pos 1 = val 1)
  26. ; RC4 KSA initialisation
  27. ksa_part1_loop:
  28. mov [esp+ecx],cl
  29. inc cl
  30. jnz ksa_part1_loop
  31.  
  32. xor eax,eax
  33.  
  34. ; key for RC4 algorithm
  35. mov edx,0xdeadbeef
  36.  
  37. ksa_key_loop:   ; cl used as j (loop counter)
  38. add al,[esp+ecx];
  39. add al,dl   ; take cur byte from key
  40. ror edx,0x8     ; next byte in key (rotate 8 bits)
  41. mov bl,[esp+ecx]
  42. mov bh,[esp+eax]
  43. mov [esp+eax],bl
  44. mov [esp+ecx],bh
  45. inc cl
  46. jnz ksa_key_loop
  47.  
  48. jmp label1
  49.  
  50. decrypt:       
  51. mov ebx,esp
  52. add ebx,0x4 ; keystream location
  53.  
  54. pop esp     ; loads end of program address into ESP (using location from earlier call which placed it on stack )
  55.  
  56. ; sanity check
  57. pop eax         ; ensure we're at end of program (see last four instructions)
  58. cmp eax,0x41414141
  59. jnz myexit
  60.  
  61. ; esp = beginning of data from image
  62. ; check valid data
  63. pop eax
  64. cmp eax,0x42424242
  65. jnz myexit
  66.  
  67. ; begin decryption
  68. pop edx     ; get num bytes ( = 50/32h)
  69. mov ecx,edx
  70. mov esi,esp ; stack pointer into esi
  71. mov edi,ebx ; destination is old esp + 4
  72. sub edi,ecx ; move back down stack 32 bytes
  73. rep movsb   ; move ECX bytes from ESI to EDI
  74. mov esi,ebx
  75. mov ecx,edx
  76. mov edi,ebx
  77. sub edi,ecx
  78. xor eax,eax
  79. xor ebx,ebx
  80. xor edx,edx
  81.  
  82. decrypt_loop:           ; main decrypt loop
  83. inc al
  84. add bl,[esi+eax]
  85. mov dl,[esi+eax]
  86. mov dh,[esi+ebx]
  87. mov [esi+eax],dh
  88. mov [esi+ebx],dl
  89. add dl,dh
  90. xor dh,dh
  91. mov bl,[esi+edx]
  92. mov dl,[edi]
  93. xor dl,bl           ; xor with keystraem
  94. mov [edi],dl            ; store result
  95. inc edi
  96. dec ecx
  97. jnz decrypt_loop
  98.  
  99. myexit:
  100. mov eax, 4 ; sys_write
  101. mov ebx, 1 ; stdout
  102. mov edx, 0x32 ; length
  103. lea ecx, [edi-0x32] ; location
  104. int 0x80
  105.  
  106. xor ebx,ebx  ; clear ebx
  107. mov eax,ebx  ; clear eax
  108. inc al       ; set eax to 1 (syscall for exit)
  109. int 0x80     ; make syscall
  110.  
  111. label1:
  112. nop
  113. nop
  114. call decrypt    ; CALL SO THAT WE GET THIS LOCATION ONTO THE STACK (e.g. end of file for later for decryption)
  115.  
  116. ; 0x41414141 (to check we're in right place later)
  117. dd 0x41414141
  118. ;inc ecx
  119. ;inc ecx
  120. ;inc ecx
  121. ;inc ecx
  122.  
  123. ; INSERT STUFF TO BE DECRYPTED HERE
  124. db  042h
  125. db  042h
  126. db  042h
  127. db  042h
  128. db  032h
  129. db  00h
  130. db  00h
  131. db  00h
  132. db  091h
  133. db  0d8h
  134. db  0f1h
  135. db  06dh
  136. db  070h
  137. db  020h
  138. db  03ah
  139. db  0abh
  140. db  067h
  141. db  09ah
  142. db  0bh
  143. db  0c4h
  144. db  091h
  145. db  0fbh
  146. db  0c7h
  147. db  066h
  148. db  0fh
  149. db  0fch
  150. db  0cdh
  151. db  0cch
  152. db  0b4h
  153. db  02h
  154. db  0fah
  155. db  0d7h
  156. db  077h
  157. db  0b4h
  158. db  054h
  159. db  038h
  160. db  0abh
  161. db  01fh
  162. db  0eh
  163. db  0e3h
  164. db  08eh
  165. db  0d3h
  166. db  0dh
  167. db  0ebh
  168. db  099h
  169. db  0c3h
  170. db  093h
  171. db  0feh
  172. db  0d1h
  173. db  02bh
  174. db  01bh
  175. db  011h
  176. db  0c6h
  177. db  011h
  178. db  0efh
  179. db  0c8h
  180. db  0cah
  181. db  02fh
  182.  
  183.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement