Introduction The Server The Echo Client References
REMOTE COMMAND EXECUTION
Muhammad Adil Raja
Roaming Researchers, R .
November 18, 2015
Introduction The Server The Echo Client References
OUTLINE
1 INTRODUCTION
2 THE SERVER
3 THE ECHO CLIENT
4 REFERENCES
Introduction The Server The Echo Client References
OUTLINE
1 INTRODUCTION
2 THE SERVER
3 THE ECHO CLIENT
4 REFERENCES
Introduction The Server The Echo Client References
OUTLINE
1 INTRODUCTION
2 THE SERVER
3 THE ECHO CLIENT
4 REFERENCES
Introduction The Server The Echo Client References
OUTLINE
1 INTRODUCTION
2 THE SERVER
3 THE ECHO CLIENT
4 REFERENCES
Introduction The Server The Echo Client References
INTRODUCTION
Introduction The Server The Echo Client References
THE SERVER I
/∗
∗ Copyright ( c ) 2013, Oracle and / or i t s a f f i l i a t e s . A l l r i g h t s reserved .
∗
∗ R e d i s t r i b u t i o n and use in source and binary forms , with or without
∗ modification , are permitted provided that the f o l l o w i n g conditions
∗ are met :
∗
∗ − Redistributions of source code must r e t a i n the above copyright
∗ notice , t h i s l i s t of conditions and the f o l l o w i n g disclaimer .
∗
∗ − Redistributions in binary form must reproduce the above copyright
∗ notice , t h i s l i s t of conditions and the f o l l o w i n g disclaimer in the
∗ documentation and / or other materials provided with the d i s t r i b u t i o n .
∗
∗ − Neither the name of Oracle or the names of i t s
∗ c o n t r i b u t o r s may be used to endorse or promote products derived
∗ from t h i s software without s p e c i f i c p r i o r w r i t t e n permission .
∗
∗ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
∗ IS " AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
∗ THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
∗ PURPOSE ARE DISCLAIMED . IN NO EVENT SHALL THE COPYRIGHT OWNER OR
∗ CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT , INCIDENTAL , SPECIAL ,
∗ EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
∗ PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
∗ PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
∗ LIABILITY , WHETHER IN CONTRACT, STRICT LIABILITY , OR TORT (INCLUDING
∗ NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
∗ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Introduction The Server The Echo Client References
THE SERVER II
∗/
import java . net . ∗ ;
import java . io . ∗ ;
public class TelnetServer {
public static void main ( String [ ] args ) throws IOException {
i f ( args . length != 1) {
System . err . p r i n t l n ( "Usage : java EchoServer <port number>" ) ;
System . e x i t ( 1 ) ;
}
int portNumber = Integer . parseInt ( args [ 0 ] ) ;
try (
ServerSocket serverSocket =
new ServerSocket ( Integer . parseInt ( args [ 0 ] ) ) ;
Socket clientSocket = serverSocket . accept ( ) ;
P r i n t W r i t e r out =
new P r i n t W r i t e r ( clientSocket . getOutputStream ( ) , true ) ;
BufferedReader in = new BufferedReader (
new InputStreamReader ( clientSocket . getInputStream ( ) ) ) ;
) {
String inputLine ;
String n u l l S t r =null ;
while ( ( inputLine = in . readLine ( ) ) != null ) {
ProcessBuilder processB=new ProcessBuilder ( " bash " , "−c " , inputLine ) ;
processB . redirectErrorStream ( true ) ;
Introduction The Server The Echo Client References
THE SERVER III
Process process = processB . s t a r t ( ) ;
BufferedReader br = new BufferedReader (
new InputStreamReader ( process . getInputStream ( ) ) ) ;
/ / BufferedReader errorReader = new BufferedReader (
/ / new InputStreamReader ( process . getErrorStream ( ) ) ) ;
while ( ( inputLine=br . readLine ( ) ) ! = null ) {
out . p r i n t l n ( inputLine ) ;
System . out . p r i n t l n ( " Log : "+ inputLine ) ;
}
out . p r i n t l n ( " bye " ) ;
out . flush ( ) ;
System . out . p r i n t l n ( "Bye Input ! " ) ;
System . out . p r i n t l n ( "Bye Error ! " ) ;
br . close ( ) ;
process . destroy ( ) ;
/ / out . p r i n t l n ( " Bye : " ) ;
}
} catch ( IOException e ) {
System . out . p r i n t l n ( " Exception caught when t r y i n g to l i s t e n on port "
+ portNumber + " or l i s t e n i n g f o r a connection " ) ;
System . out . p r i n t l n ( e . getMessage ( ) ) ;
}
}
}
Introduction The Server The Echo Client References
THE CLIENT I
/∗
∗ Copyright ( c ) 1995, 2013, Oracle and / or i t s a f f i l i a t e s . A l l r i g h t s reserved .
∗
∗ R e d i s t r i b u t i o n and use in source and binary forms , with or without
∗ modification , are permitted provided that the f o l l o w i n g conditions
∗ are met :
∗
∗ − Redistributions of source code must r e t a i n the above copyright
∗ notice , t h i s l i s t of conditions and the f o l l o w i n g disclaimer .
∗
∗ − Redistributions in binary form must reproduce the above copyright
∗ notice , t h i s l i s t of conditions and the f o l l o w i n g disclaimer in the
∗ documentation and / or other materials provided with the d i s t r i b u t i o n .
∗
∗ − Neither the name of Oracle or the names of i t s
∗ c o n t r i b u t o r s may be used to endorse or promote products derived
∗ from t h i s software without s p e c i f i c p r i o r w r i t t e n permission .
∗
∗ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
∗ IS " AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
∗ THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
∗ PURPOSE ARE DISCLAIMED . IN NO EVENT SHALL THE COPYRIGHT OWNER OR
∗ CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT , INCIDENTAL , SPECIAL ,
∗ EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
∗ PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
∗ PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
∗ LIABILITY , WHETHER IN CONTRACT, STRICT LIABILITY , OR TORT (INCLUDING
∗ NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
∗ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Introduction The Server The Echo Client References
THE CLIENT II
∗/
import java . io . ∗ ;
import java . net . ∗ ;
public class TelnetClient {
public static void main ( String [ ] args ) throws IOException {
i f ( args . length != 2) {
System . err . p r i n t l n (
"Usage : java TelnetClient <host name> <port number>" ) ;
System . e x i t ( 1 ) ;
}
String hostName = args [ 0 ] ;
int portNumber = Integer . parseInt ( args [ 1 ] ) ;
try (
Socket echoSocket = new Socket ( hostName , portNumber ) ;
P r i n t W r i t e r out =
new P r i n t W r i t e r ( echoSocket . getOutputStream ( ) , true ) ;
BufferedReader in =
new BufferedReader (
new InputStreamReader ( echoSocket . getInputStream ( ) ) ) ;
BufferedReader stdIn =
new BufferedReader (
new InputStreamReader ( System . in ) )
) {
String userInput , serverOutput ;
Introduction The Server The Echo Client References
THE CLIENT III
while ( ( userInput = stdIn . readLine ( ) ) != null ) {
out . p r i n t l n ( userInput ) ;
System . out . p r i n t l n ( " t e l n e t : " ) ;
while ( ! ( serverOutput=in . readLine ( ) ) . equals ( " bye " ) )
System . out . p r i n t l n ( serverOutput ) ;
System . out . p r i n t l n ( "Bye Client " ) ;
}
} catch ( UnknownHostException e ) {
System . err . p r i n t l n ( "Don ’ t know about host " + hostName ) ;
System . e x i t ( 1 ) ;
} catch ( IOException e ) {
System . err . p r i n t l n ( " Couldn ’ t get I /O f o r the connection to " +
hostName ) ;
System . e x i t ( 1 ) ;
}
}
}
Introduction The Server The Echo Client References
REFERENCES
The source code has been taken from
here.
This presentation has been produced with LATEX.
Frankfurt, monarcha.

Remote Command Execution

  • 1.
    Introduction The ServerThe Echo Client References REMOTE COMMAND EXECUTION Muhammad Adil Raja Roaming Researchers, R . November 18, 2015
  • 2.
    Introduction The ServerThe Echo Client References OUTLINE 1 INTRODUCTION 2 THE SERVER 3 THE ECHO CLIENT 4 REFERENCES
  • 3.
    Introduction The ServerThe Echo Client References OUTLINE 1 INTRODUCTION 2 THE SERVER 3 THE ECHO CLIENT 4 REFERENCES
  • 4.
    Introduction The ServerThe Echo Client References OUTLINE 1 INTRODUCTION 2 THE SERVER 3 THE ECHO CLIENT 4 REFERENCES
  • 5.
    Introduction The ServerThe Echo Client References OUTLINE 1 INTRODUCTION 2 THE SERVER 3 THE ECHO CLIENT 4 REFERENCES
  • 6.
    Introduction The ServerThe Echo Client References INTRODUCTION
  • 7.
    Introduction The ServerThe Echo Client References THE SERVER I /∗ ∗ Copyright ( c ) 2013, Oracle and / or i t s a f f i l i a t e s . A l l r i g h t s reserved . ∗ ∗ R e d i s t r i b u t i o n and use in source and binary forms , with or without ∗ modification , are permitted provided that the f o l l o w i n g conditions ∗ are met : ∗ ∗ − Redistributions of source code must r e t a i n the above copyright ∗ notice , t h i s l i s t of conditions and the f o l l o w i n g disclaimer . ∗ ∗ − Redistributions in binary form must reproduce the above copyright ∗ notice , t h i s l i s t of conditions and the f o l l o w i n g disclaimer in the ∗ documentation and / or other materials provided with the d i s t r i b u t i o n . ∗ ∗ − Neither the name of Oracle or the names of i t s ∗ c o n t r i b u t o r s may be used to endorse or promote products derived ∗ from t h i s software without s p e c i f i c p r i o r w r i t t e n permission . ∗ ∗ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS ∗ IS " AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, ∗ THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR ∗ PURPOSE ARE DISCLAIMED . IN NO EVENT SHALL THE COPYRIGHT OWNER OR ∗ CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT , INCIDENTAL , SPECIAL , ∗ EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, ∗ PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR ∗ PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF ∗ LIABILITY , WHETHER IN CONTRACT, STRICT LIABILITY , OR TORT (INCLUDING ∗ NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS ∗ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  • 8.
    Introduction The ServerThe Echo Client References THE SERVER II ∗/ import java . net . ∗ ; import java . io . ∗ ; public class TelnetServer { public static void main ( String [ ] args ) throws IOException { i f ( args . length != 1) { System . err . p r i n t l n ( "Usage : java EchoServer <port number>" ) ; System . e x i t ( 1 ) ; } int portNumber = Integer . parseInt ( args [ 0 ] ) ; try ( ServerSocket serverSocket = new ServerSocket ( Integer . parseInt ( args [ 0 ] ) ) ; Socket clientSocket = serverSocket . accept ( ) ; P r i n t W r i t e r out = new P r i n t W r i t e r ( clientSocket . getOutputStream ( ) , true ) ; BufferedReader in = new BufferedReader ( new InputStreamReader ( clientSocket . getInputStream ( ) ) ) ; ) { String inputLine ; String n u l l S t r =null ; while ( ( inputLine = in . readLine ( ) ) != null ) { ProcessBuilder processB=new ProcessBuilder ( " bash " , "−c " , inputLine ) ; processB . redirectErrorStream ( true ) ;
  • 9.
    Introduction The ServerThe Echo Client References THE SERVER III Process process = processB . s t a r t ( ) ; BufferedReader br = new BufferedReader ( new InputStreamReader ( process . getInputStream ( ) ) ) ; / / BufferedReader errorReader = new BufferedReader ( / / new InputStreamReader ( process . getErrorStream ( ) ) ) ; while ( ( inputLine=br . readLine ( ) ) ! = null ) { out . p r i n t l n ( inputLine ) ; System . out . p r i n t l n ( " Log : "+ inputLine ) ; } out . p r i n t l n ( " bye " ) ; out . flush ( ) ; System . out . p r i n t l n ( "Bye Input ! " ) ; System . out . p r i n t l n ( "Bye Error ! " ) ; br . close ( ) ; process . destroy ( ) ; / / out . p r i n t l n ( " Bye : " ) ; } } catch ( IOException e ) { System . out . p r i n t l n ( " Exception caught when t r y i n g to l i s t e n on port " + portNumber + " or l i s t e n i n g f o r a connection " ) ; System . out . p r i n t l n ( e . getMessage ( ) ) ; } } }
  • 10.
    Introduction The ServerThe Echo Client References THE CLIENT I /∗ ∗ Copyright ( c ) 1995, 2013, Oracle and / or i t s a f f i l i a t e s . A l l r i g h t s reserved . ∗ ∗ R e d i s t r i b u t i o n and use in source and binary forms , with or without ∗ modification , are permitted provided that the f o l l o w i n g conditions ∗ are met : ∗ ∗ − Redistributions of source code must r e t a i n the above copyright ∗ notice , t h i s l i s t of conditions and the f o l l o w i n g disclaimer . ∗ ∗ − Redistributions in binary form must reproduce the above copyright ∗ notice , t h i s l i s t of conditions and the f o l l o w i n g disclaimer in the ∗ documentation and / or other materials provided with the d i s t r i b u t i o n . ∗ ∗ − Neither the name of Oracle or the names of i t s ∗ c o n t r i b u t o r s may be used to endorse or promote products derived ∗ from t h i s software without s p e c i f i c p r i o r w r i t t e n permission . ∗ ∗ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS ∗ IS " AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, ∗ THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR ∗ PURPOSE ARE DISCLAIMED . IN NO EVENT SHALL THE COPYRIGHT OWNER OR ∗ CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT , INCIDENTAL , SPECIAL , ∗ EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, ∗ PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR ∗ PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF ∗ LIABILITY , WHETHER IN CONTRACT, STRICT LIABILITY , OR TORT (INCLUDING ∗ NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS ∗ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  • 11.
    Introduction The ServerThe Echo Client References THE CLIENT II ∗/ import java . io . ∗ ; import java . net . ∗ ; public class TelnetClient { public static void main ( String [ ] args ) throws IOException { i f ( args . length != 2) { System . err . p r i n t l n ( "Usage : java TelnetClient <host name> <port number>" ) ; System . e x i t ( 1 ) ; } String hostName = args [ 0 ] ; int portNumber = Integer . parseInt ( args [ 1 ] ) ; try ( Socket echoSocket = new Socket ( hostName , portNumber ) ; P r i n t W r i t e r out = new P r i n t W r i t e r ( echoSocket . getOutputStream ( ) , true ) ; BufferedReader in = new BufferedReader ( new InputStreamReader ( echoSocket . getInputStream ( ) ) ) ; BufferedReader stdIn = new BufferedReader ( new InputStreamReader ( System . in ) ) ) { String userInput , serverOutput ;
  • 12.
    Introduction The ServerThe Echo Client References THE CLIENT III while ( ( userInput = stdIn . readLine ( ) ) != null ) { out . p r i n t l n ( userInput ) ; System . out . p r i n t l n ( " t e l n e t : " ) ; while ( ! ( serverOutput=in . readLine ( ) ) . equals ( " bye " ) ) System . out . p r i n t l n ( serverOutput ) ; System . out . p r i n t l n ( "Bye Client " ) ; } } catch ( UnknownHostException e ) { System . err . p r i n t l n ( "Don ’ t know about host " + hostName ) ; System . e x i t ( 1 ) ; } catch ( IOException e ) { System . err . p r i n t l n ( " Couldn ’ t get I /O f o r the connection to " + hostName ) ; System . e x i t ( 1 ) ; } } }
  • 13.
    Introduction The ServerThe Echo Client References REFERENCES The source code has been taken from here. This presentation has been produced with LATEX. Frankfurt, monarcha.