dgets / RecScan

Utility to search, modify, and/or extract contents from potentially recursive archived messes
1 stars 0 forks source link

Read tar stream to EOF #12

Closed dgets closed 6 years ago

dgets commented 7 years ago

Not sure how to read the TarInputStream to EOF just yet. Seems like it should be a ridiculously simple problem to fix, but I'm having trouble locating anything about it in the api. :| I'll have to look into whether or not it has something to do with watching the byte count manually... Seems like it should be handled a lot more professionally than that, though.

Then again, it looks like there may be other issues that need correcting along the way. I just attempted testing with an archive other than my normal test archive. The different runs crashed in different places, with different exceptions. :(

Here is the output from the new archive:

Verbose:
Initializing tgt
Executing tgt.getArcType()
Archive is tar.gz
Opening fis . . .
Opening zis . . .
Opening tarInput . . .
Fucked: tarListDir() java.io.IOException: Stream Closed
Borkage: java.io.IOException: Stream Closed

Here's the output from the original test archive:

Verbose:
Initializing tgt
Executing tgt.getArcType()
Archive is tar.gz
Opening fis . . .
Opening zis . . .
Opening tarInput . . .
Read entry #1
Pulling entry: pax_global_header
-Found Global Pax Header-
Read entry #2
Pulling entry: ossec-hids-2.9.1/
-Valid Directory Found-
Read entry #3
Pulling entry: ossec-hids-2.9.1/.gitignore
-Valid File Found-
NP Fucked: tarListDir() java.lang.NullPointerException
dgets commented 7 years ago

I have no idea why the following code is doing so terribly on some archives, and then slightly better on others... Been working on this for awhile now without making an inch of headway. Here's where things are borked (Tar.tarListDir()):

    public static String[] tarListDir(InputStream incoming) 
            throws Exception {
        if (RecScan.verbose) {
            System.out.println("Opening tarInput . . .");
            System.out.println("incoming.available(): " +
                    incoming.available());
        }

        TarArchiveInputStream tarInput = new TarArchiveInputStream(incoming);
        TarArchiveEntry entry = null;
        String[] directory = null;

        int cntr = 0;

        if (RecScan.verbose) {
            System.out.println("tarInput.getCount(): " + tarInput.getCount());
            System.out.println(tarInput.available() + " total available");
        }

        do {
            try {
                entry = tarInput.getNextTarEntry();
            } catch (Exception e) {
                throw new Exception("Looping .getNextTarEntry() borked");
            }

            if (RecScan.verbose) {
                System.out.println("Read entry #" + (cntr + 1));
            }

            if (RecScan.verbose) {
                System.out.println("Pulling entry: " + entry.getName());
            }

            //other conditions?
            if (entry.isGlobalPaxHeader()) {
                if (RecScan.verbose) {
                    System.out.println("-Found Global Pax Header-");
                }
                continue;
            } else if (entry.isFile()) {
                if (RecScan.verbose) {
                    System.out.println("-Valid File Found-");
                }

                directory[cntr] = entry.getName();
                if (RecScan.verbose) {
                    System.out.println(directory[cntr]);
                }
            } else if (entry.isDirectory()) {
                if (RecScan.verbose) {
                    System.out.println("-Valid Directory Found-");;
                }

                //bug?
                /*for (TarArchiveEntry ouah : 
                    entry.getDirectoryEntries()) {
                    //not going to test them out just yet
                    if (RecScan.verbose) {
                        System.out.println(entry.getName());
                    }

                    directory[cntr++] = entry.getName();
                }*/
            } else {
                if (RecScan.verbose) {
                    System.out.println("-No Idea-");
                }
            }
        } while (tarInput.available() > 0);

        if (RecScan.verbose) {
            System.out.println("Exited tarInput while");
        }
                tarInput.close();

        return directory;
    }

Here's the usual result (2/3 of the archives, at this point):

Verbose: Initializing tgt Executing tgt.getArcType() Archive is tar.gz Opening fis . . . Opening zis . . . Opening tarInput . . . incoming.available(): 1 tarInput.getCount(): 0 0 total available Borkage: Looping .getNextTarEntry() borked

Here's the result when working with ossec-*.tar.gz (the only one showing this much progress):

Verbose: Initializing tgt Executing tgt.getArcType() Archive is tar.gz Opening fis . . . Opening zis . . . Opening tarInput . . . incoming.available(): 1 tarInput.getCount(): 0 0 total available Read entry #1 Pulling entry: pax_global_header -Found Global Pax Header- Read entry #1 Pulling entry: ossec-hids-2.9.1/ -Valid Directory Found- Exited tarInput while