![]() |
|
Tutorial Send Command to Java Program - Printable Version +- Sinisterly (https://sinister.li) +-- Forum: Coding (https://sinister.li/Forum-Coding) +--- Forum: Java, JVM, & JRE (https://sinister.li/Forum-Java-JVM-JRE) +--- Thread: Tutorial Send Command to Java Program (/Thread-Tutorial-Send-Command-to-Java-Program) |
Send Command to Java Program - m0dem - 01-03-2016 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: 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.
RE: Send Command to Java Program - meow - 01-03-2016 thanks for the nice representation of how to have 0 security whatsoever how to pwn all 20 of those computers - send post request with malicious command as the parameter value RE: Send Command to Java Program - Inori - 01-03-2016 (01-03-2016, 07:56 AM)meow Wrote: thanks for the nice representation of how to have 0 security whatsoever Did he ever say it was secure? And really, it's Java, nobody bothers to learn how to do malicious shit with it. RE: Send Command to Java Program - meow - 01-03-2016 (01-03-2016, 08:12 AM)Chitoge Wrote: Did he ever say it was secure? No, does that automatically mean I'm not allowed to have input on the security of it? RE: Send Command to Java Program - m0dem - 01-03-2016 (01-03-2016, 07:56 AM)meow Wrote: thanks for the nice representation of how to have 0 security whatsoever You are right ... A PHP script that saves abritrary text (from anyone on the internet) into a fil e on the server ... Not a secure solution. But, like @'Chitoge' hinted, security is not in the scope of this tutorial. This tutorial is not for those who like to copy n' paste and have it all done. I have put a disclaimer in the thread. (01-03-2016, 08:12 AM)Chitoge Wrote: Did he ever say it was secure? And really, it's Java, nobody bothers to learn how to do malicious shit with it. (01-03-2016, 08:18 AM)Lust Wrote: No, you have all the right to say it's insecure. @"Chitoge" also has the right to critisize you for it. Thanks. |