Send Command to Java Program 01-03-2016, 07:05 AM
#1
Lets say, for example, that we have 20 computers with our Java program running, and we want to send a command (or data) over the internet to all of them. That is what we will be learning about today.
DISCLAIMER: Security is outside of this tutorial's scope.
We will need a web server with simple PHP capabilities (you can find that for free just about anywhere). Our PHP script will get an input from a text box we fill out in our browser. It will then save that data into a file. Our Java app(s) will retrieve that file from the internet and extract our command. It's as simple as that.
This is the PHP script (index.php) that will be on our web server:
This is our Java code:
Everything is pretty self-explanatory.
Enjoy.
DISCLAIMER: Security is outside of this tutorial's scope.
We will need a web server with simple PHP capabilities (you can find that for free just about anywhere). Our PHP script will get an input from a text box we fill out in our browser. It will then save that data into a file. Our Java app(s) will retrieve that file from the internet and extract our command. It's as simple as that.
This is the PHP script (index.php) that will be on our web server:
Code:
<?php
if ($_POST["cmd"]) {
if (!file_put_contents("cmd", $_POST["cmd"])) {
echo "ERROR: Command not saved to file.";
}
}
?>
<html>
<body>
<form action="index.php" method="post">
Command: <input type="text" name="cmd"><br>
<input type="submit">
</form>
</body>
</html>This is our Java code:
Code:
package bot;
import java.net.*;
import java.io.*;
public class BOT {
public static String HOST = "http://yourwebserver.com/cmd";
private static String readRemoteFile(String url) {
URL u;
BufferedReader in;
String data = null;
try {
u = new URL(url);
in = new BufferedReader(
new InputStreamReader(u.openStream())
);
data = in.readLine();
} catch (MalformedURLException mue) {
System.out.println("ERROR: MalformedURLException occured.");
mue.printStackTrace();
System.exit(1);
} catch (IOException ioe) {
System.out.println("ERROR: IOException occured.");
ioe.printStackTrace();
System.exit(1);
} finally {
return data;
}
}
public static void main(String[] args) {
String cmd = readRemoteFile(HOST);
System.out.println(cmd);
}
}Everything is pretty self-explanatory.
Enjoy.






![[+]](https://sinister.li/images/modern/collapse_collapsed.png)










