0

Let's say I have a file "hello.txt" containing the text "Hello, world!" I have application "textreader", that reads the file hello.txt and puts it in stdout. Next, i have encrypted the file "hello.txt". Can decrypt it a special library that can read the file byte by byte.

Ho do i can use application "textreader" to read from encrypted file as a normal file, using my library (or my application)? I can't write some temporary files under the terms of the problem.

It is possible to use named pipes (mkfifo), but prerequisite is the support seeking. File must be read randomly.

Does anyone have any ideas how i can to do it, if i haven't source code of "textreader"?

4 Answers4

1

Write a shared library that replaces the standard Linux I/O routines, and load it with LD_PRELOAD.

This will work provided the application is dynamically linked to the C library - if "textreader" makes system calls directly, then interception won't work.

See also:

Community
  • 1
  • 1
Adrian Cox
  • 6,204
  • 5
  • 41
  • 68
1

How about simply using stdin?

If the textreader application converts hello.txt to stdout, simply pipe the output of textreader to your own application/script. For example:

$ ./textreader | ./myapp

Depending on what language you'll decide to use in your application, you can handle stdin the same way you'd handle most streams.

The following SO answers should give you more information as to how to read from stdin in different languages:

Wikipedia also provides some helpful information regarding stdin.

Edit: Most streams are seekable, as you initially asked. I wrote a small example in C++ that shows how you can seek stdin.

#include <iostream>

int main(void) {

    int i = 0;
    std::string buffer;

    while (not std::cin.eof()) {
        std::getline(std::cin, buffer);
        if (buffer.find("while") != std::string::npos)
            if (i < 10)
                std::cin.seekg(0, std::ios::beg);
            else
                std::cin.seekg(0, std::ios::end);
        std::cout << "Line " << i++ << ": " << buffer << std::endl;
    }

return 0;

}   

You can compile the code by using g++ test.cpp -o test and then run it with ./test < test.cpp. The code will output the beginning of the file twice, up to the beginning of the while loop. No buffering is done other than reading a single line from stdin at a time.

Community
  • 1
  • 1
teotwaki
  • 1,033
  • 7
  • 10
  • The method should support random seek.Reading from standard input can only be serial, as far as I understand. – Denis Bezrukov Mar 01 '12 at 19:31
  • @user1242996 I've edited my answer to add a simple seeking example in C++. Reading from stdin is no different than reading from most streams. – teotwaki Mar 02 '12 at 09:39
1

Normally this is done using filesystem filter driver which will perform decryption on-the-fly. In order to support random seek you would have to encrypt data in independent blocks (64-256Kb large) and cache those blocks in memory in decrypted form. Not a trivial task but can be accomplished. I am not aware of how to implement a filter driver on Linux (we offer a similar product for Windows).

Eugene Mayevski 'Callback
  • 45,135
  • 8
  • 71
  • 121
0

how about load the encrypted file in memory and decrypt from the memory buffer? some script language such as perl, python, etc can read the whole file and assign it to a variable

Qian
  • 1,007
  • 7
  • 6