flat assembler
Message board for the users of flat assembler.

Index > Main > search for files in a directory and encoding

Author
Thread Post new topic Reply to topic
unknown334



Joined: 08 Oct 2021
Posts: 22
unknown334
Hello, FLATASSEMBLER team!

I need to find the first 10 txt files in the directory with the executable file and encode them with the AES algorithm

I'm attaching the library

I would like variations for windows and for linux


Description:
Download
Filename: fasmaes-1.0.tar.gz
Filesize: 10.96 KB
Downloaded: 109 Time(s)

Post 20 Jan 2022, 11:57
View user's profile Send private message Reply with quote
FlierMate



Joined: 21 Jan 2021
Posts: 208
FlierMate
Done! Win32 programs modified from your aesexample1.asm.

It finds first 10 *.txt files in the current directory, encode or decode them all.(WARNING: It overwrites existing .txt files)

But I notice got a bug, the last few bytes of the text file sometimes not decoded properly...... Hmm.. And it won't work with text file shorter than 48 bytes (TEXTSIZE)

Tested on wine (Linux x64).

As a side note, there is AES extension set in x86 CPU instruction that you might want to make use of it.

EDIT: Please add the following line right before "redo:" label if you have downloaded both .asm file below
Code:
        mov     dword [fileptr], 0
        
redo:        


Description: encode win32
Download
Filename: encaes.asm
Filesize: 3.74 KB
Downloaded: 112 Time(s)

Description: decode win32
Download
Filename: decaes.asm
Filesize: 3.74 KB
Downloaded: 107 Time(s)



Last edited by FlierMate on 21 Jan 2022, 09:44; edited 1 time in total
Post 21 Jan 2022, 02:05
View user's profile Send private message Visit poster's website Reply with quote
unknown334



Joined: 08 Oct 2021
Posts: 22
unknown334
FlierMate wrote:
Done! Win32 programs modified from your aesexample1.asm.

It finds first 10 *.txt files in the current directory, encode or decode them all.(WARNING: It overwrites existing .txt files)

But I notice got a bug, the last few bytes of the text file sometimes not decoded properly...... Hmm.. And it won't work with text file shorter than 48 bytes (TEXTSIZE)

Tested on wine (Linux x64).

As a side note, there is AES extension set in x86 CPU instruction that you might want to make use of it.


Thanks
Post 21 Jan 2022, 04:46
View user's profile Send private message Reply with quote
unknown334



Joined: 08 Oct 2021
Posts: 22
unknown334
FlierMate wrote:
Done! Win32 programs modified from your aesexample1.asm.

It finds first 10 *.txt files in the current directory, encode or decode them all.(WARNING: It overwrites existing .txt files)

But I notice got a bug, the last few bytes of the text file sometimes not decoded properly...... Hmm.. And it won't work with text file shorter than 48 bytes (TEXTSIZE)

Tested on wine (Linux x64).

As a side note, there is AES extension set in x86 CPU instruction that you might want to make use of it.


perhaps because the size of the source file is different from the encrypted one?
Post 21 Jan 2022, 05:28
View user's profile Send private message Reply with quote
unknown334



Joined: 08 Oct 2021
Posts: 22
unknown334
FlierMate wrote:
Done! Win32 programs modified from your aesexample1.asm.

It finds first 10 *.txt files in the current directory, encode or decode them all.(WARNING: It overwrites existing .txt files)

But I notice got a bug, the last few bytes of the text file sometimes not decoded properly...... Hmm.. And it won't work with text file shorter than 48 bytes (TEXTSIZE)

Tested on wine (Linux x64).

As a side note, there is AES extension set in x86 CPU instruction that you might want to make use of it.


I found an error
fix
Code:
TEXTSIZE equ BLOCK_SIZE    

replace
Code:
TEXTSIZE equ 3*BLOCK_SIZE    



up: no, it still doesn't work...
Post 21 Jan 2022, 05:40
View user's profile Send private message Reply with quote
FlierMate



Joined: 21 Jan 2021
Posts: 208
FlierMate
unknown334 wrote:


perhaps because the size of the source file is different from the encrypted one?


I have run multiple tests, the file buffer copying is actually correct, size after = size before, content after = content before. You can redo it using this barebone example:

Code:
format PE CONSOLE 4.0
entry start

include 'include/win32a.inc'

TEXTSIZE equ 48
FILELIMIT equ 10

section '.text' code readable executable

  start:
    invoke GetStdHandle, -11
    mov    [stdout], eax
    invoke lstrcat, buf, filepath
    call   find_files
    invoke ExitProcess, 0
    
proc    find_files
    locals
        hfind  dd ?
    endl
        invoke FindFirstFile,buf,wfd
        cmp    eax,INVALID_HANDLE_VALUE
        je     exit
        mov    [hfind], eax
check:
        test   [wfd.dwFileAttributes],FILE_ATTRIBUTE_DIRECTORY
        jz     found
        jmp    next
found:
        invoke WriteConsole, [stdout], wfd.cFileName, 256, 0, 0
        invoke  CreateFile, wfd.cFileName, GENERIC_READ + GENERIC_WRITE, FILE_SHARE_READ, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0
        mov     dword [fd], eax
        cmp     eax, INVALID_HANDLE_VALUE
        je      quit
        mov     dword [fileptr], 0
        
redo:
        invoke  SetFilePointer, dword [fd], dword [fileptr], 0, FILE_BEGIN
        cmp     eax, -1         ;INVALID_SET_FILE_POINTER
        je      quit    
        invoke  ReadFile, dword [fd], buffer, TEXTSIZE, len, 0
        test    eax, eax
        jz      quit
        mov     ecx, dword [len]
        test    ecx, ecx
        jz      close
        
        ;
        ;Do something with [buffer]
        ;
        
        invoke  SetFilePointer, dword [fd], dword [fileptr], 0, FILE_BEGIN
        cmp     eax, -1         ;INVALID_SET_FILE_POINTER
        je      quit
        mov     ecx, dword [len]
        add     dword [fileptr], ecx
        invoke  WriteFile, dword [fd], buffer, dword [len], 0, 0
        jmp     redo
        
close:
        invoke  CloseHandle, dword [fd]
        
quit:     
        add    [filecount],1
        cmp    [filecount],FILELIMIT
        jae    exit
next:
        invoke FindNextFile,[hfind],wfd
        test   eax,eax
        jne    check
        invoke FindClose, [hfind]
exit:
        ret
endp    

section '.data' data readable writeable

  buf       rb 256
  filepath  db '.\*.txt',0
  wfd       WIN32_FIND_DATA
  stdout    dd ?
  filecount dw ?
  fd        dd ?
  buffer    rb TEXTSIZE
  len       dd ?
  fileptr   dd ?

section '.idata' import data readable writeable

  library kernel,'KERNEL32.DLL'

  import kernel,\
         ExitProcess,'ExitProcess',\
         WriteConsole, 'WriteConsoleA',\
         GetStdHandle,'GetStdHandle', \
         FindFirstFile, 'FindFirstFileA',\
         FindNextFile, 'FindNextFileA',\
         FindClose, 'FindClose',\
     CreateFile, 'CreateFileA', \
     ReadFile, 'ReadFile', \
     WriteFile, 'WriteFile', \
     GetLastError, 'GetLastError', \
     SetFilePointer, 'SetFilePointer',\
     CloseHandle, 'CloseHandle',\
     lstrcat,'lstrcatA'
    


Last edited by FlierMate on 21 Jan 2022, 09:37; edited 1 time in total
Post 21 Jan 2022, 08:16
View user's profile Send private message Visit poster's website Reply with quote
unknown334



Joined: 08 Oct 2021
Posts: 22
unknown334
FlierMate wrote:
unknown334 wrote:


perhaps because the size of the source file is different from the encrypted one?


I have run multiple tests, the file buffer copying is actually correct, size after = size before, content after = content before. You can redo it using this barebone example:

Code:
format PE CONSOLE 4.0
entry start

include 'include/win32a.inc'

TEXTSIZE equ 48
FILELIMIT equ 10

section '.text' code readable executable

  start:
    invoke GetStdHandle, -11
    mov    [stdout], eax
    invoke lstrcat, buf, filepath
    call   find_files
    invoke ExitProcess, 0
    
proc    find_files
    locals
        hfind  dd ?
    endl
        invoke FindFirstFile,buf,wfd
        cmp    eax,INVALID_HANDLE_VALUE
        je     exit
        mov    [hfind], eax
check:
        test   [wfd.dwFileAttributes],FILE_ATTRIBUTE_DIRECTORY
        jz     found
        jmp    next
found:
        invoke WriteConsole, [stdout], wfd.cFileName, 256, 0, 0
        invoke  CreateFile, wfd.cFileName, GENERIC_READ + GENERIC_WRITE, FILE_SHARE_READ, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0
        mov     dword [fd], eax
        cmp     eax, INVALID_HANDLE_VALUE
        je      quit
        
redo:
        invoke  SetFilePointer, dword [fd], dword [fileptr], 0, FILE_BEGIN
        cmp     eax, -1         ;INVALID_SET_FILE_POINTER
        je      quit    
        invoke  ReadFile, dword [fd], buffer, TEXTSIZE, len, 0
        test    eax, eax
        jz      quit
        mov     ecx, dword [len]
        test    ecx, ecx
        jz      close
        
        ;
        ;Do something with [buffer]
        ;
        
        invoke  SetFilePointer, dword [fd], dword [fileptr], 0, FILE_BEGIN
        cmp     eax, -1         ;INVALID_SET_FILE_POINTER
        je      quit
        mov     ecx, dword [len]
        add     dword [fileptr], ecx
        invoke  WriteFile, dword [fd], buffer, dword [len], 0, 0
        jmp     redo
        
close:
        invoke  CloseHandle, dword [fd]
        
quit:     
        add    [filecount],1
        cmp    [filecount],FILELIMIT
        jae    exit
next:
        invoke FindNextFile,[hfind],wfd
        test   eax,eax
        jne    check
        invoke FindClose, [hfind]
exit:
        ret
endp    

section '.data' data readable writeable

  buf       rb 256
  filepath  db '.\*.txt',0
  wfd       WIN32_FIND_DATA
  stdout    dd ?
  filecount dw ?
  fd        dd ?
  buffer    rb TEXTSIZE
  len       dd ?
  fileptr   dd ?

section '.idata' import data readable writeable

  library kernel,'KERNEL32.DLL'

  import kernel,\
         ExitProcess,'ExitProcess',\
         WriteConsole, 'WriteConsoleA',\
         GetStdHandle,'GetStdHandle', \
         FindFirstFile, 'FindFirstFileA',\
         FindNextFile, 'FindNextFileA',\
         FindClose, 'FindClose',\
     CreateFile, 'CreateFileA', \
     ReadFile, 'ReadFile', \
     WriteFile, 'WriteFile', \
     GetLastError, 'GetLastError', \
     SetFilePointer, 'SetFilePointer',\
     CloseHandle, 'CloseHandle',\
     lstrcat,'lstrcatA'
    


not work...
Post 21 Jan 2022, 08:54
View user's profile Send private message Reply with quote
FlierMate



Joined: 21 Jan 2021
Posts: 208
FlierMate
I think the read/write of text file must be aligned to BLOCK_SIZE(16 bytes) or TEXTSIZE(48 bytes).... Below is the version which encode with 48-byte alignment, you still use the same decoder, but the decoded text file will have extra characters at the end of file.

The first example of encaes.asm + decaes.asm (text file size doesn't change)
Code:
d, of the Nullsecurity Proj���    


The second example of encaes.asm(V2) + decaes.asm (text file increased to be 48-byte aligned)
Code:
d, of the Nullsecurity Project.pressed or i    


So it is like apple and orange. I give up, maybe someone more skillful should give it a try. Crying or Very sad


Description: encoder V2
Download
Filename: encaes.asm
Filesize: 3.91 KB
Downloaded: 108 Time(s)



Last edited by FlierMate on 21 Jan 2022, 09:10; edited 1 time in total
Post 21 Jan 2022, 08:54
View user's profile Send private message Visit poster's website Reply with quote
FlierMate



Joined: 21 Jan 2021
Posts: 208
FlierMate
unknown334 wrote:


not work...


Yes, this barebone example will write what it read, so you would notice no difference after running it.
Just meant for testing its accuracy of read/write. Smile

Perhaps we should reset the file pointer:

Code:
        mov     dword [fileptr], 0
        
redo:    
Post 21 Jan 2022, 08:57
View user's profile Send private message Visit poster's website Reply with quote
unknown334



Joined: 08 Oct 2021
Posts: 22
unknown334
FlierMate wrote:
unknown334 wrote:


not work...


Yes, this barebone example will write what it read, so you would notice no difference after running it.
Just meant for testing its accuracy of read/write. Smile

Perhaps we should reset the file pointer:

Code:
        mov     dword [fileptr], 0
        
redo:    


Thanks Smile, Work
Post 21 Jan 2022, 09:20
View user's profile Send private message Reply with quote
FlierMate



Joined: 21 Jan 2021
Posts: 208
FlierMate
unknown334 wrote:
FlierMate wrote:
unknown334 wrote:


not work...


Yes, this barebone example will write what it read, so you would notice no difference after running it.
Just meant for testing its accuracy of read/write. Smile

Perhaps we should reset the file pointer:

Code:
        mov     dword [fileptr], 0
        
redo:    


Thanks Smile, Work


You're welcome! Very Happy

But I am shocked, the wine (windows emulator) in Linux did not give any error.
Post 21 Jan 2022, 09:29
View user's profile Send private message Visit poster's website 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-2020, Tomasz Grysztar. Also on GitHub, YouTube, Twitter.

Website powered by rwasa.