0

Using the browser if I visit a certain direct download URL, it automatically downloads the file. However, when I use Java code to download the file, I get the HTML code instead of the file contents:

<html>
<body>
<script type="text/javascript" src="/aes.js"></script>
<script>function toNumbers(d) {
    var e = [];
    d.replace(/(..)/g, function (d) {
        e.push(parseInt(d, 16))
    });
    return e
}

function toHex() {
    for (var d = [], d = 1 == arguments.length && arguments[0].constructor == Array ? arguments[0] : arguments, e = "", f = 0; f < d.length; f++) e += (16 > d[f] ? "0" : "") + d[f].toString(16);
    return e.toLowerCase()
}

var a = toNumbers("f655ba9d09a112d4968c63579db590b4"), b = toNumbers("98344c2eee86c3994890592585b49f80"),
    c = toNumbers("b5eb8dc5c53e5107faa7ec1c1f3e3dc7");
document.cookie = "__test=" + toHex(slowAES.decrypt(c, 2, a, b)) + "; expires=Thu, 31-Dec-37 23:55:55 GMT; path=/";
location.href = "http://example.com/Test.txt?i=1";</script>
<noscript>This site requires Javascript to work, please enable Javascript in your browser or use a browser with
    Javascript support
</noscript>
</body>
</html>

My file downloading code is e.g. the following:

URL url = new URL("...");
try (InputStream inputStream = url.openStream())
{
    Files.copy(inputStream, downloadedFilePath, REPLACE_EXISTING);
}

How would it be possible to download the file programmatically in Java? There are ways to execute JavaScript but how is it supposed to work exactly? It seems like the document.cookie has to be set (correctly) to download.

BullyWiiPlaza
  • 17,329
  • 10
  • 113
  • 185

1 Answers1

0

Downloading works by passing the correct cookie value alongside of the request. The cookie value can be retrieved using e.g. the Google Chrome DevTools -> Application -> Cookies. Since in my example the cookie value does not change, a code like the following would do the trick:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

public class DownloadWithCookieExample
{
    public static void main(String[] arguments)
    {
        try
        {
            URL url = new URL("...");
            HttpURLConnection con = (HttpURLConnection) url.openConnection();
            con.setRequestProperty("Cookie", "blabla"); // Hard-coded correct cookie value
            String readStream = readStream(con.getInputStream());
            System.out.println(readStream);
        } catch (Exception exception)
        {
            exception.printStackTrace();
        }
    }

    private static String readStream(InputStream inputStream) throws IOException
    {
        StringBuilder stringBuilder = new StringBuilder();
        try (BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream)))
        {
            String line;
            while ((line = reader.readLine()) != null)
            {
                stringBuilder.append(line);
                stringBuilder.append(System.lineSeparator());
            }
        }
        return stringBuilder.toString().trim();
    }
}

An even better solution would be to execute the JavaScript to receive the cookie value and to pass the result. This task is left as an exercise for another answer for whoever knows how to do this elegantly.

BullyWiiPlaza
  • 17,329
  • 10
  • 113
  • 185