Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. ; ###############################################################################;
  2. ; DESCRIPTION: Simple and not optimized code for Base 64 Encode/Decode algorithm.
  3. ;            :
  4. ; -------------------------------------------------------------------------------;
  5. ; COMPILATION: ml /c /coff file.asm
  6. ; LINKING    : link /SUBSYSTEM:CONSOLE file
  7. ; -------------------------------------------------------------------------------;
  8. ; AUTHOR     : Marco 'RootkitNeo' C.
  9. ; -------------------------------------------------------------------------------;
  10. ; LANGUAGE   : MASM32 (CUI Application)
  11. ; -------------------------------------------------------------------------------;
  12. ; VERSION    : v. 1.0
  13. ; -------------------------------------------------------------------------------;
  14. ; LICENSE    : GNU/GPL V.3
  15. ; ###############################################################################;
  16.  
  17.  
  18. include     c:\masm32\include\masm32rt.inc
  19.  
  20.  
  21. ; String length (byte format)
  22. ; #################################################################################-
  23. strLen     MACRO   string
  24.  
  25.   xor      eax, eax
  26.   xor      ecx, ecx
  27.  
  28. mywhile:
  29.   cmp      [string+ecx], 0
  30.   je       _exit
  31.   inc      ecx
  32.   jmp      mywhile
  33.  
  34. _exit:
  35.   mov      eax, ecx
  36.  
  37. ENDM
  38. ; #################################################################################
  39.  
  40. ; Get index of a char in CODES string
  41. ; #################################################################################
  42. indexOf    MACRO   char
  43.   push     ecx
  44.   xor      ecx, ecx
  45.  
  46. .IF char != '='
  47.   .WHILE  [codes+ecx] != char
  48.     inc cl
  49.   .ENDW
  50. .ELSE
  51.   mov      ecx, -1
  52. .ENDIF
  53.  
  54.   mov      dl, cl
  55.   pop      ecx
  56.  
  57. EXITM <dl>          ; return value
  58.  
  59. ENDM
  60. ; #################################################################################
  61.  
  62.  
  63.  
  64. ;
  65. ; Data Section
  66. ; ---------------------------------------------------------------------------------
  67. .data
  68.  
  69.   initMessage1       db     9,9,"#############################################",13,10,
  70.                             9,9,"#                                           #",13,10,
  71.                             9,9,"#    Base64  v.1.0  |   MASM V. 6.14.8444   #",13,10,
  72.                             9,9,"#-------------------------------------------#",13,10,
  73.                             9,9,"#    Developed by:                          #",13,10,0  
  74.                            
  75.                            
  76.   initMessage2       db     9,9,"#                 Marco 'RootkitNeo' C.     #",13,10,
  77.                             9,9,"#-------------------------------------------#",13,10,
  78.                             9,9,"#    Description :                          #",13,10,
  79.                             9,9,"#                 Base64 Encode/Decode      #",13,10,
  80.                             9,9,"#############################################",13,10,13,10,0
  81.  
  82.  
  83.  
  84.   strInputTextM        db   "Type your text: ",0
  85.   strOutputTextME      db   "Base64 Encoded String: ",0
  86.   strOutputTextMD      db   "Base64 Decoded String: ",0
  87.  
  88.   codes                db   "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
  89.  
  90.   CRLF                 db   13,10,0
  91.  
  92.   menu                 db   "-------------------------------------------------------------------------------",13,10,
  93.                             "Select an option:",13,10,
  94.                             "1. Encode",13,10,
  95.                             "2. Decode",13,10,
  96.                             "3. Exit",13,10,13,10,
  97.                             "> ",0
  98.                            
  99.   consoleTitle         db    "Base 64 Encode/Decode ",0
  100. ; ---------------------------------------------------------------------------------
  101.  
  102. ;
  103. ; Stack section
  104. ; ---------------------------------------------------------------------------------
  105. .data?
  106.  
  107.   strBuffer            db 100  dup (?)    ; Plain text buffer
  108.   strEncodedBuffer     db 300  dup (?)    ; Encode text buffer
  109.  
  110.   choice               db           ?     ; Selected option (menu)
  111. ; ---------------------------------------------------------------------------------
  112.  
  113. ;
  114. ; Constant section
  115. ; ---------------------------------------------------------------------------------
  116. .const
  117.  
  118.   max_input_buffer     = 100
  119.   max_encoded_buffer   = 300
  120. ; ---------------------------------------------------------------------------------
  121.  
  122. ;
  123. ; Code segment
  124. ; ---------------------------------------------------------------------------------
  125. .code
  126. start:
  127.   ; Title of the window
  128.   invoke     SetConsoleTitle, addr consoleTitle
  129.  
  130.   ; Show the menu
  131.   .WHILE choice != '3'
  132.     call       ClearScreenAndColor
  133.  
  134.     ; Print init message
  135.     push       offset    initMessage1
  136.     call       StdOut
  137.  
  138.     push       offset    initMessage2
  139.     call       StdOut
  140.  
  141.     ; The menu
  142.     push       offset    menu
  143.     call       StdOut
  144.  
  145.     ; Wait user input
  146.     push       4
  147.     push       offset    choice
  148.     call       StdIn
  149.  
  150.     ; Encode
  151.     .IF choice == '1'
  152.       push       offset    strInputTextM
  153.       call       StdOut
  154.  
  155.       push       [max_input_buffer]
  156.       push       offset    strBuffer
  157.       call       StdIn
  158.  
  159.       push       offset    CRLF
  160.       call       StdOut
  161.  
  162.       call       encodeString
  163.  
  164.       push       offset    strOutputTextME
  165.       call       StdOut
  166.  
  167.       push       offset    strEncodedBuffer
  168.       call       StdOut
  169.      
  170.     ; Decode
  171.     .ELSEIF choice == '2'
  172.       push       offset    strInputTextM
  173.       call       StdOut
  174.  
  175.       push       [max_encoded_buffer]
  176.       push       offset    strEncodedBuffer
  177.       call       StdIn
  178.  
  179.       push       offset    CRLF
  180.       call       StdOut
  181.  
  182.       call       decodeString
  183.  
  184.       push       offset    strOutputTextMD
  185.       call       StdOut
  186.  
  187.       push       offset    strBuffer
  188.       call       StdOut
  189.     .ENDIF
  190.     ; End if
  191.    
  192.     push       offset    CRLF
  193.     call       StdOut
  194.  
  195.     push       offset    CRLF
  196.     call       StdOut
  197.  
  198.     ; Clean buffers
  199.     invoke     RtlZeroMemory, addr strBuffer,max_input_buffer
  200.     invoke     RtlZeroMemory, addr strEncodedBuffer, max_encoded_buffer
  201.  
  202.     inkey      "[...Press any key to continue...]"
  203.   .ENDW  
  204.   ; Exit
  205.   invoke     ExitProcess, 0
  206.  
  207.  
  208.  
  209. ;
  210. ; Encode plain text
  211. ; ---------------------------------------------------------------------------------
  212. encodeString      proc
  213.   pushad
  214.  
  215.   strLen [strBuffer]      ; Length
  216.  
  217.   xor        ecx, ecx
  218.  
  219.   mov        ebx, eax
  220.   xor        eax, eax
  221.   xor        edx, edx
  222.   xor        esi, esi
  223.  
  224.   .while ecx < ebx
  225.     mov        al, [strBuffer+ecx]
  226.     and        al, 0FCh
  227.     shr        al, 2
  228.     mov        al, [codes+eax]
  229.  
  230.     mov        [strEncodedBuffer+esi+0], al
  231.  
  232.     ; First char
  233.     mov       al, [strBuffer+ecx]
  234.     and       al, 03h
  235.     shl       al, 4
  236.  
  237.     inc       ecx
  238.     dec       ebx
  239.    
  240.     .IF ecx > ebx
  241.       dec       ecx
  242.       inc       ebx
  243.      
  244.       ; Second char
  245.       mov        al, [codes+eax]
  246.       mov       [strEncodedBuffer+esi+1], al
  247.       mov       [strEncodedBuffer+esi+2], '='
  248.       mov       [strEncodedBuffer+esi+3], '='
  249.     .ELSE
  250.       inc ebx
  251.       dec ecx
  252.      
  253.       ; Second char
  254.       mov        dl, [strBuffer+ecx+1]
  255.       and        dl, 0F0h
  256.       shr        dl, 4
  257.       or         al, dl
  258.       mov        al, [codes+eax]
  259.       mov        [strEncodedBuffer+esi+1], al
  260.  
  261.       mov        al, [strBuffer+ecx+1]
  262.       and        al, 0Fh
  263.       shl        al, 2
  264.  
  265.       add        ecx, 2
  266.       dec        ebx
  267.      
  268.       .IF ecx > ebx
  269.         sub        ecx, 2
  270.         inc        ebx
  271.        
  272.         ; Third char and four
  273.         mov        al, [codes+eax]
  274.         mov        [strEncodedBuffer+esi+2], al
  275.         mov        [strEncodedBuffer+esi+3], '='
  276.       .ELSE
  277.         sub        ecx, 2
  278.         inc        ebx
  279.  
  280.         ; Third char
  281.         mov        dl, [strBuffer+ecx+2]
  282.         and        dl, 0C0h
  283.         shr        dl, 6
  284.         or         al, dl
  285.         mov        al, [codes+eax]
  286.         mov        [strEncodedBuffer+esi+2], al
  287.  
  288.         ; Four char
  289.         mov       al, [strBuffer+ecx+2]
  290.         and       al, 3Fh
  291.         mov       al, [codes+eax]
  292.         mov       [strEncodedBuffer+esi+3], al
  293.       .ENDIF
  294.     .ENDIF
  295.  
  296.     add        ecx, 3
  297.     add        esi, 4
  298.  
  299.   .endw
  300.  
  301.   ; End string character
  302.   mov      [strEncodedBuffer+esi+4], 0
  303.  
  304.   popad
  305.  
  306.   ret
  307. encodeString  endp
  308.  
  309.  
  310. ;
  311. ; Decode String
  312. ; ---------------------------------------------------------------------------------
  313. decodeString  proc
  314.   pushad
  315.  
  316.   strLen [strEncodedBuffer]
  317.  
  318.   mov    ebx, eax
  319.  
  320.   xor    ecx, ecx
  321.   xor    eax, eax
  322.   xor    edx, edx
  323.   xor    esi, esi
  324.  
  325.   .while ecx < ebx
  326.     mov    al, [strEncodedBuffer+ecx+0]
  327.     mov    al, indexOf(al)
  328.  
  329.     mov    dl, [strEncodedBuffer+ecx+1]
  330.     mov    dl, indexOf(dl)
  331.  
  332.     shl    al, 2
  333.     shr    dl, 4
  334.     or     al, dl
  335.     mov    [strBuffer+esi], al       ; First
  336.     inc    esi
  337.  
  338.     mov    al, [strEncodedBuffer+ecx+1]
  339.     mov    al, indexOf(al)
  340.     mov    dl, [strEncodedBuffer+ecx+2]
  341.  
  342.     .IF dl == '='
  343.       jmp    _end
  344.     .ENDIF
  345.  
  346.     mov    dl, indexOf(dl)
  347.  
  348.     shl    al, 4
  349.     shr    dl, 2
  350.     or     al, dl
  351.     mov    [strBuffer+esi], al       ; Second
  352.     inc    esi
  353.  
  354.     mov    al, [strEncodedBuffer+ecx+2]
  355.     mov    al, indexOf(al)
  356.     mov    dl, [strEncodedBuffer+ecx+3]
  357.     mov    dl, indexOf(dl)
  358.  
  359.     shl    al, 6
  360.     or     al, dl
  361.     mov    [strBuffer+esi], al       ; Third
  362.     inc    esi
  363.  
  364.     add    ecx, 4
  365.  
  366.   .endw
  367. _end:
  368.   mov    [strBuffer+esi], 0
  369.  
  370.   popad
  371.  
  372.   ret
  373.  
  374. decodeString endp
  375. ; ---------------------------------------------------------------------------------
  376.  
  377.  
  378.  
  379.  
  380.   ; #############################################
  381.   ; #           UTILITY FUNCTION                #
  382.   ; #############################################
  383.  
  384.  
  385. ;
  386. ; Clear command prompt window and set the colors
  387. ; ---------------------------------------------------------------------------------
  388. ClearScreenAndColor      proc
  389.  
  390.   LOCAL          coordScreen:COORD
  391.   LOCAL          cCharsWritten:DWORD
  392.   LOCAL          csbi:CONSOLE_SCREEN_BUFFER_INFO
  393.   LOCAL          dwConSize:DWORD
  394.   LOCAL          hStdout:HANDLE
  395.  
  396.   pushad
  397.  
  398.  
  399.   invoke         GetStdHandle, STD_OUTPUT_HANDLE
  400.   mov            hStdout, eax
  401.  
  402.   invoke         GetConsoleScreenBufferInfo, hStdout, addr csbi
  403.  
  404.   .IF eax == 0
  405.     print       "Error 1"
  406.     jmp          _exit_clear
  407.   .ENDIF
  408.  
  409.   movzx          ebx, word ptr csbi.dwSize.y
  410.   movzx          eax, word ptr csbi.dwSize.x
  411.   mul            ebx
  412.  
  413.   mov            dwConSize, eax
  414.  
  415.   invoke         FillConsoleOutputCharacter, hStdout, ' ', dwConSize, 0, addr cCharsWritten
  416.  
  417.   .IF eax == 0
  418.     print       "Error 2"
  419.     jmp          _exit_clear
  420.   .ENDIF
  421.  
  422.  
  423.   invoke         GetConsoleScreenBufferInfo, hStdout, addr csbi
  424.  
  425.   .IF eax == 0
  426.     print       "Error 3"
  427.     jmp          _exit_clear
  428.   .ENDIF
  429.  
  430.  
  431.   ; Set Background and Foreground colors
  432.   invoke         FillConsoleOutputAttribute, hStdout, (BACKGROUND_BLUE or BACKGROUND_INTENSITY or FOREGROUND_BLUE or FOREGROUND_GREEN or FOREGROUND_RED or FOREGROUND_INTENSITY), 65535, 0, addr cCharsWritten
  433.   invoke         SetConsoleTextAttribute, hStdout, (BACKGROUND_BLUE or BACKGROUND_INTENSITY or FOREGROUND_BLUE or FOREGROUND_GREEN or FOREGROUND_RED or FOREGROUND_INTENSITY)
  434.  
  435.   .IF eax == 0
  436.     print       "Error 4"
  437.     invoke       GetLastError
  438.     jmp          _exit_clear
  439.   .ENDIF
  440.  
  441.   invoke         SetConsoleCursorPosition, hStdout, 0
  442.  
  443. _exit_clear:
  444.   popad
  445.  
  446.   ret
  447. ClearScreenAndColor      endp
  448.  
  449. end start