flat assembler
Message board for the users of flat assembler.

Index > Main > base32

Author
Thread Post new topic Reply to topic
Ali.Z



Joined: 08 Jan 2018
Posts: 716
Ali.Z 12 Oct 2019, 20:20
hey,

i hard-coded base32 encoding, currently it runs under ollydbg because i didnt write any gui code. (but i will once i write the decoder)

however before writing the reversed code, i wanna improve this code and make it faster.

many of you know i cant do math, and a lot of you helped me a lot already .. so thank you again in advance. (spent hours hard-coding the encoder Sad )

if you dont know the specification and standard of base32 encoding, then please visit this link.

rfc 4648

Code:
include 'win32a.inc'

format pe gui 4.0

stack 1000h,1000h
heap 1000h,1000h



section '.text' code readable executable



              invoke    GetModuleHandle,0

              xor       eax,eax
              or        ecx,-1
              mov       edi,input
              repnz     scasb
              not       ecx
              dec       ecx
              mov       [input_length],ecx

              mov       ebx,base_32_array

              mov       esi,input
              mov       edi,buffer
              cld
    _8bit:
              lodsb
              mov       edx,eax
              and       edx,7 ; extract first 3 bits
              mov       [extracted_bits],edx ; store them
              shr       eax,3 ; shift right by the number of extracted bits
              mov       al,byte [ebx+eax]
              stosb

   _16bit:    ; 3 bits left + 8 new bits = 11 bits - 5 bits(encode every 5bits) = 6 bits to shift
              dec       ecx
              lodsb
              mov       edx,eax
              and       edx,3Fh ; extract first 6 bits
              xchg      [extracted_bits],edx ; load previous bits, and store the new one
              shr       eax,6 ; 2 bits left
              shl       edx,2 ; shift by the number of bits left in eax
              or        eax,edx ; combine bits 2,3,4 from edx with bits 0,1 in eax
              mov       al,byte [ebx+eax]
              stosb
              or        ecx,ecx
              jz        _end

    .special: ; 6 bits left - 5 (encoding) = 1 bit to shift
              mov       eax,[extracted_bits]
              mov       edx,eax
              and       edx,1
              mov       [extracted_bits],edx
              shr       eax,1
              mov       al,byte [ebx+eax]
              stosb
              dec       ecx
              or        ecx,ecx
              jz        _end

    _24bit:   ; 1 bit left + 8 new bits = 9 bits - 5 = 4 bits to shift
              dec       ecx
              lodsb
              mov       edx,eax
              and       edx,0Fh ; extract first 4
              xchg      [extracted_bits],edx ; edx = 1 bit
              shr       eax,4 ; 4 bits left
              shl       edx,4 ; shift by the number of bits left in eax
              or        eax,edx ; combine bit 4 from edx with bits 0,1,2,3 in eax
              mov       al,byte [ebx+eax]
              stosb
              or        ecx,ecx
              jz        _end

    _32bit:   ; 4 bits left + 8 new bits = 12 - 5 = 7 bits to shift
              lodsb
              mov       edx,eax
              and       edx,7Fh ; extract first 7
              xchg      [extracted_bits],edx ; edx have 4 bits now
              shr       eax,7 ; 1 bit left
              shl       edx,1 ; shl by number of bits left in eax
              or        eax,edx ; combine bits 1,2,3,4 with bit 0 in eax
              mov       al,byte [ebx+eax]
              stosb

    .special: ; 7 bits left - 5 = 2 bits to shift
              mov       eax,[extracted_bits]
              mov       edx,eax
              and       edx,3
              mov       [extracted_bits],edx
              shr       eax,2
              mov       al,byte [ebx+eax]
              stosb
              dec       ecx
              or        ecx,ecx
              jz        _end

    _40bit:   ; 2 bits left + 8 new bits = 10 - 5 = 5 bits to shift
              lodsb
              mov       edx,eax
              and       edx,1Fh
              xchg      [extracted_bits],edx
              shr       eax,5
              shl       edx,3
              or        eax,edx
              mov       al,byte [ebx+eax]
              stosb

    ..special:
              mov       eax,[extracted_bits]
              mov       al,byte [ebx+eax]
              stosb
              dec       ecx
              or        ecx,ecx
              jnz       _8bit

    _end:
              mov       ecx,[input_length]
    subtract:
              cmp       ecx,5
              jbe       skip
              sub       ecx,5
              jmp       subtract
    skip:
              cmp       ecx,1
              jz        _6padding
              cmp       ecx,2
              jz        _4padding
              cmp       ecx,3
              jz        _3padding
              cmp       ecx,4
              jz        _1padding
              int3

    _6padding:
              mov       ecx,6
              mov       eax,'='
              rep       stosb
              int3
    _4padding:
              mov       eax,[extracted_bits]
              shl       eax,4
              mov       al,byte [ebx+eax]
              stosb
              mov       ecx,4
              mov       eax,'='
              rep       stosb
              int3
    _3padding:
              mov       eax,[extracted_bits]
              shl       eax,1
              mov       al,byte [ebx+eax]
              stosb
              mov       ecx,3
              mov       eax,'='
              rep       stosb
              int3
    _1padding:
              mov       eax,[extracted_bits]
              shl       eax,3
              mov       al,byte [ebx+eax]
              stosb
              mov       byte [edi],'='
              int3

section '.data' data readable writeable

  base_32_array db 'ABCDEFGHIJKLMNOPQRSTUVWXYZ234567',0

  input db 'flat assembler',0 ; MZWGC5BAMFZXGZLNMJWGK4Q=
  buffer rb 1024

  input_length dd 0
  extracted_bits dd 0

section '.idata' import data readable

  library kernel32,'kernel32.dll'

  include 'api\kernel32.inc'

section '.reloc' fixups data readable discardable
    

_________________
Asm For Wise Humans
Post 12 Oct 2019, 20:20
View user's profile Send private message Reply with quote
Ali.Z



Joined: 08 Jan 2018
Posts: 716
Ali.Z 13 Oct 2019, 08:24
i wanted someone to help me to dynamically calculate how many bits i should extract and shift, but thats okay for now im still happy.



https://board.flatassembler.net/topic.php?p=209476#209476

revolution wrote:
Division can be emulated by using repeated subtraction. Multiplication can be emulated by repeated addition.

4 * 5 ===> 5 + 5 + 5 + 5

17 / 3 ===> 17 - 3 - 3 - 3 - 3 - 3

The basic operation for decimal conversion is division by powers of 10. So you can make code to go the "long way around" and just keep subtracting powers of 10 instead of using DIV (or the equivalent MUL by 1/10).


here im taking the total length of the input, and keep subtracting 5 to get the number of required padding.

assuming the input was 1000 chars long, then i have to subtract 5 a lot .. im sure this can be done using div but im not sure what numbers i should put in there.

Code:
    _end:
              mov       ecx,[input_length]
    subtract:
              cmp       ecx,5
              jbe       skip
              sub       ecx,5
              jmp       subtract
    skip:
              cmp       ecx,1
              jz        _6padding
              cmp       ecx,2
              jz        _4padding
              cmp       ecx,3
              jz        _3padding
              cmp       ecx,4
              jz        _1padding
              int3    

_________________
Asm For Wise Humans
Post 13 Oct 2019, 08:24
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20300
Location: In your JS exploiting you and your system
revolution 13 Oct 2019, 13:12
Ali.Z wrote:
i wanted someone to help me to dynamically calculate how many bits i should extract and shift, but thats okay for now im still happy.
It wasn't clear from your post that you were asking for help. I thought you were posting your base-32 code with the plan that it might help others.
Post 13 Oct 2019, 13:12
View user's profile Send private message Visit poster's website Reply with quote
Ali.Z



Joined: 08 Jan 2018
Posts: 716
Ali.Z 13 Oct 2019, 14:12
yes, but ignore the encoder part for now.

i want to avoid subtracting by 5, and the solution is using DIV but i dont know what numbers i should put in would you mind fixing this thing for me.

_________________
Asm For Wise Humans
Post 13 Oct 2019, 14:12
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20300
Location: In your JS exploiting you and your system
revolution 13 Oct 2019, 14:31
You can divide by five with this:
Code:
mov eax,[length]
xor edx,edx ;edx:eax is the numerator
mov ecx,5 ;ecx is the denominator
div ecx
;eax=floor(length/5)
;edx=remainder(length/5)    
Post 13 Oct 2019, 14:31
View user's profile Send private message Visit poster's website Reply with quote
Ali.Z



Joined: 08 Jan 2018
Posts: 716
Ali.Z 13 Oct 2019, 15:08
thanks rev, i didnt understand it .. but after running it under olly found that i should compare using edx after the div and ignore eax.

currently im hard-coding the decoder.

_________________
Asm For Wise Humans
Post 13 Oct 2019, 15:08
View user's profile Send private message Reply with quote
Ali.Z



Joined: 08 Jan 2018
Posts: 716
Ali.Z 13 Oct 2019, 19:47
decoder is done, now the most boring thing is to design a gui window.

p.s. did some minor tweaks and got rid of extra code.

_________________
Asm For Wise Humans
Post 13 Oct 2019, 19:47
View user's profile Send private message Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2024, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.