How to view internet-passwords from Keychain using Java (mac) - java

I would like to create a simple java app (for mac) that takes in a website and outputs my password associated with the website in Keychain.
My problem is that my app can't read the password from the output. If I manually write:
security find-internet-password -gs www.google.com
into the Terminal I get a few lines of info and then -> password: "mypassword". But in my application I only see the lines of info but the last line which should be the password is not there or null.
My code:
public static void command(){
try{
String command = "security find-internet-password -gs www.google.com";
Process child = Runtime.getRuntime().exec(command);
BufferedReader r = new BufferedReader(new InputStreamReader(child.getInputStream()));
String s;
while ((s = r.readLine()) != null) {
System.out.println(s);
}
System.out.println(r.readLine());
r.close();
}
catch (IOException e) {
e.printStackTrace();
}
}
I'm not getting any errors but simply the code above is not showing the password. I'm using Eclipse if that matters. Thanks
The application prints:
keychain: "/Users/*username*/Library/Keychains/login.keychain"
class: "inet"
attributes:
0x00000007 <blob>="www.google.com"
0x00000008 <blob>=<NULL>
"acct"<blob>="*username*"
"atyp"<blob>="http"
"cdat"<timedate>=0x323031*numbers*3333325A00 "2011*numbers*132Z\000"
"crtr"<uint32>="rimZ"
"cusi"<sint32>=<NULL>
"desc"<blob>=<NULL>
"icmt"<blob>=<NULL>
"invi"<sint32>=<NULL>
"mdat"<timedate>=0x3230331*numbers*33834375A00 "20115*numbers*3847Z\000"
"nega"<sint32>=<NULL>
"path"<blob>="/"
"port"<uint32>=0x00000000
"prot"<blob>=<NULL>
"ptcl"<uint32>="htps"
"scrp"<sint32>=<NULL>
"sdmn"<blob>="www.google.com"
"srvr"<blob>="www.google.com"
"type"<uint32>=<NULL>
but the last line is missing and should be
password: "mypassword"
Edited
Is it the InputStreamReader that does not read the password? Is there another way to get the password?

The reason why you do not get the password in the output is that it is not printed to the stdout but to the stderr instead.
If you replace child.getInputStream() with child.getErrorStream() in your code will get you righ that Password:secret! line.
You could use e.g. the following modified version of your code:
public static boolean command(String host) {
try {
String command = "security find-internet-password -gs " + host;
Process child = Runtime.getRuntime().exec(command);
try (BufferedReader out = new BufferedReader(new InputStreamReader(
child.getInputStream()));
BufferedReader err = new BufferedReader(
new InputStreamReader(child.getErrorStream()))) {
String user = null;
String password = null;
String s;
while ((s = out.readLine()) != null) {
if (s.matches(" *\"acct\".*")) {
user = s.replaceAll("^.*=\"", "").replace("\"", "");
}
}
s = err.readLine();
password = s.replaceAll("^.*: *\"", "").replace("\"", "");
System.out.println("user: " + user);
System.out.println("pwd: " + password);
return true;
}
} catch (IOException e) {
return false;
}
}

Related

how to extract specific lines from msinfo32 file using java code

i am writing a code to extract system info details i.e ram, processor speed and put them on a text file.
public void getSpecs(){
//run a cmd command to convert msinfo32 to .txt file
String[] command = {
"cmd",
};
Process p;
try{
p= Runtime.getRuntime().exec(command);
new Thread(new Sec(p.getErrorStream(), System.err)).start();
new Thread(new Sec(p.getInputStream(), System.out)).start();
PrintWriter pw= new PrintWriter(p.getOutputStream());
pw.println("msinfo32 /report .\\specs.txt");
pw.close();
p.waitFor();
}catch(Exception e){
e.printStackTrace();
}
}
}
This process is taking long and its converting the whole file.
msinfo32 exports the computer info into a file. It is expected to take some time as it retrieves a huge export for each computer/windows component.
I have done something similar using powershell
public static void main(String[] args) throws IOException {
//Set the commands
String cmd = "powershell.exe get-WmiObject ";
String[] win32CmdA = {"win32_processor", "win32_computerSystem", "win32_logicaldisk"};
for (String win32Cmd : win32CmdA) {
String info = runCmd(cmd + win32Cmd,
"MaxClockSpeed",
"TotalPhysicalMemory",
"DeviceID",
"FreeSpace");//Add as many atributes you want to return from powershell output
System.out.println(info); // You can use a file writer here
}
// //You can handle ErrorStream here
// String line;
// BufferedReader stderr = new BufferedReader(new InputStreamReader(
// powerShellProcess.getErrorStream()));
// while ((line = stderr.readLine()) != null) {
// System.out.println(line);
// }
}
private static String runCmd(String cmd, String... attrs) throws IOException {
Process powerShellProcess = Runtime.getRuntime().exec(cmd);
powerShellProcess.getOutputStream().close();
String line;
String result="";
BufferedReader stdout = new BufferedReader(new InputStreamReader(powerShellProcess.getInputStream()));
while ((line = stdout.readLine()) != null) {
if (line != null && line.contains(":")) {
String nameValue[] = line.split(":");
if (Arrays.asList(attrs).contains(nameValue[0].trim())) {
result+=nameValue[0] + " - " + nameValue[1] + "\n";
}
}
}
stdout.close();
return result;
}
The above code invokes the Windows Management Instrumentation (WMI) classes in powershell for specific components (processor, computerSystem and logicaldisk).
Then you define which values should be taken from the powershell output, like MaxClockSpeed, TotalPhysicalMemory, etc.
If you change the System.out.println(info); with a file writer you will have this info in a file.
Sample output (took ~3 seconds to run)
DeviceID - CPU0
MaxClockSpeed - 3401
TotalPhysicalMemory - 17053949952
DeviceID - C
FreeSpace - 56341774336
DeviceID - D
FreeSpace -
DeviceID - F
FreeSpace - 373687742464

Executing "$ aarch64-linux-objdump -d a.out' in terminal throug java

I have made a cross compiler using gcc. Now I want the compile and run commands to be executed in a terminal through java program. Here is the code that I am using for this :
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class Runterminal {
public static void main(String[] args) {
Process proc;
Process procRun;
String compileCommand = "aarch64-linux-g++ -std=c++14 test.cpp";
String runCommand = "aarch64-linux-objdump -d a.out";
try{
proc = Runtime.getRuntime().exec(compileCommand);
procRun = Runtime.getRuntime().exec(runCommand);
// Read the output
BufferedReader reader =
new BufferedReader(new InputStreamReader(proc.getInputStream()));
String line = "";
while((line = reader.readLine()) != null) {
System.out.print(line + "\n");
}
proc.waitFor();
BufferedReader readero =
new BufferedReader(new InputStreamReader(procRun.getInputStream()));
String lineo = "";
while((lineo = readero.readLine()) != null) {
System.out.print(lineo + "\n");
}
procRun.waitFor();
}
catch(Exception e)
{
System.out.println("Exception occurred "+e);
}
}
}
Now my first command is executing since I could see the a.out file being generated. The second command should dump the memory contents of file and it should print in in terminal but I am not seeing any output. Can anyone tell where I am going wrong?

How to execute code from a String in Java

I'm trying to work on a security system which needs remote debugging.So what I'm searching is a way to execute a code which is in a String,like the example below but with java.
try {
String Code = "rundll32 powrprof.dll, SetSuspendState";// the code we need to excecute
Runtime.getRuntime().exec(Code);
} catch (IOException e) {
}
String Code = "rundll32 powrprof.dll, SetSuspendState";
StringBuffer output = new StringBuffer();
Process p;
try {
p = Runtime.getRuntime().exec(Code);
p.waitFor();
BufferedReader reader =
new BufferedReader(new InputStreamReader(p.getInputStream()));
String line = "";
while ((line = reader.readLine())!= null) {
output.append(line + "\n");
}
} catch (Exception e) {
e.printStackTrace();
}
System.out.println(output.toString());
Please refer the following URL for further information http://www.mkyong.com/java/how-to-execute-shell-command-from-java/
No friend you got it wrong.I really don't want to execute cmd codes.what i really want is to execute java commands.as a string which is passed as shown below.
example :
String code = "System.out.println("Test code")";
Runtime.getRuntime().exec(Code);
something like this.

code runs in command prompt but does not run in eclipse

The code given below is the code that i try to run in eclipse which returns stdInput.readLine() as null when i try to run the command through command prompt it runs successfully what am i doing wrong?
public class Recognize {
public String Recog(String name)
{ try {
String command="java -cp .;C:\\mywork\\Speaker\\marf-0.3.0-devel-20070108-fat.jar SpeakerIdentApp --ident C:\\mywork\\Speaker\\testing-samples\\"+name+".wav";
Process proc = Runtime.getRuntime().exec(command);
BufferedReader stdInput = new BufferedReader(new InputStreamReader(proc.getInputStream()));
BufferedReader stdError = new BufferedReader(new InputStreamReader(proc.getErrorStream()));
name = "";
String s ;
System.out.println(stdInput.readLine());
// read the output from the command
// System.out.println("Here is the standard output of the command:\n");
while ((s=stdInput.readLine()) != null){
// System.out.println(s);
String recog = s;
// System.out.println(recog);
String ex = stdInput.readLine();
// System.out.println(ex);
String sb = stdInput.readLine();
// System.out.println(sb);
if ( recog.equalsIgnoreCase(ex))
{//System.out.println("ACCESS GRANTED");
name = recog;
// System.out.print(recog);
}
else if (ex.equalsIgnoreCase(sb))
{//System.out.println("ACCESS GRANTED");
name = ex;
// System.out.println(ex);
}
else {//System.out.println("ACCESS DENIED");
name = "";
}
it must be because eclipse default path is not set by you so try setting eclipse default path according to your requirement

Ping value in java

I know that this question has been approached under different ways, but I have checked stackoverflow and I didn't found the answer I was looking for.
To make it simple : Is there a way to get the Time ping value to an IP server under Windows ?
I know how to check if some servers are reachable, but I would like to have precise values, like we can read on terminal.
Thank you for your help and understanding.
You can do something like this :
//The command to execute
String pingCmd = "ping " + ip + " -t";
//get the runtime to execute the command
Runtime runtime = Runtime.getRuntime();
Process process = runtime.exec(pingCmd);
//Gets the inputstream to read the output of the command
BufferedReader in = new BufferedReader(new InputStreamReader(process.getInputStream()));
//reads the outputs
String inputLine = in.readLine();
while ((inputLine != null)) {
if (inputLine.length() > 0) {
........
}
inputLine = in.readLine();
}
reference
UPDATE: As per your need
public class PingDemo {
public static void main(String[] args) {
String ip = "localhost";
String time = "";
//The command to execute
String pingCmd = "ping " + ip;
//get the runtime to execute the command
Runtime runtime = Runtime.getRuntime();
try {
Process process = runtime.exec(pingCmd);
//Gets the inputstream to read the output of the command
BufferedReader in = new BufferedReader(new InputStreamReader(process.getInputStream()));
//reads the outputs
String inputLine = in.readLine();
while ((inputLine != null)) {
if (inputLine.length() > 0 && inputLine.contains("time")) {
time = inputLine.substring(inputLine.indexOf("time"));
break;
}
inputLine = in.readLine();
}
System.out.println("time --> " + time);
} catch (Exception ex) {
System.out.println(ex);
}
}
}
Written in little haste.
You can invoke the ping command and read the output (as explained in the previous answer), or if you need a lower lever access (like you can do with RAW sockets), you can have a look at the jpcap java library.
As shown here, you'll want to make use of the Runtime class to shell out a ping. All that's required of you is to parse the input stream (possibly using regex to get the time ping value).
public static void main(String[] args) {
System.out.println("Enter the host to be pinged : ");
Scanner sc = new Scanner(in);
String str = sc.next();
System.out.println("Enter the no. of packets to be sent : ");
int packets = sc.nextByte();
String pingResult;
int count=0;
try{
String command = "ping -c "+ packets +" -w 10 " + str;
Process process = Runtime.getRuntime().exec(command);
BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
String inputLine;
while ((inputLine = reader.readLine()) != null) {
if(count==packets+4) {
pingResult = (inputLine.substring(inputLine.indexOf("=")));
pingResult = (pingResult.substring
(pingResult.indexOf("/")+1,pingResult.indexOf("/")+7));
System.out.println(pingResult + " ms");
}
count++;
}
in.close();
if(count==0)System.out.println("Wrong host entered.");
}catch(Exception e){
System.out.println("Exception caught: " + e.toString());
}
}
}
Output:
Enter the host to be pinged :
8.8.8.8
Enter the no. of packets to be sent :
5
31.406 ms
Process finished with exit code 0

Categories

Resources