2016-05-04 52 views
-1

私はクライアントブラウザからHttpRequestを取得し、サーバに転送するjavaでプロキシサーバを実装しようとしています。 サーバーからHttpResponseを受信した後、Cookieを抽出して変更した後、修正されたCookieを持つHttpResponse をクライアントブラウザに転送します。これは、セッションハイジャックのための不正使用のCookieを防ぐために行っています。JavaのHttpResponseのプロキシサーバーでクライアントIPアドレスを取得する方法は?

次回は、クライアントが同じサーバーに接続しようとしたときに、私のプロキシサーバーは、この修正されたクッキーでのHttpRequestを取得します。 私のプロキシサーバーは、HttpRequestをサーバー に転送する前に、この変更されたCookieを元のものに置き換え、サーバーは正しいCookieを検出するので応答します。同じLANの他のクライアントになる場合は、このapprochで

一つの欠点は、攻撃者は、ANSクライアントの修正クッキーを盗むし、プロキシサーバへのHttpRequest を送って、プロキシサーバは、元のことで、この変更されたクッキーを交換し、サーバに転送し、攻撃者ができますされますセッションハイジャックを 実行します。

この問題を解決するために、私はこれを次のように実装しようとしています。代わりの

(クッキー元の値=変更された値)、Iは(IP、クッキー元の値)=変更された値を格納します。だからいつでも HttpReqestはクライアントブラウザから来ます。この修正されたCookieの元のCookieと共に保存された送信者とIPアドレスのIPアドレスをチェックします。

このコードを単一マシンで実行しようとしましたが、ローカルポート1111へのすべてのブラウザー要求にリダイレクトして、クライアントを取得することができました。 IPアドレス。

public static void runServer(String host, int remoteport, int localport) 
     throws IOException { 

// Create a ServerSocket to listen for connections with 
ServerSocket ss = new ServerSocket(1111); 

final byte[] request = new byte[1024]; 
byte[] reply = new byte[4096]; 

while (true) { 
    Socket client = null, server = null; 
    try { 
    // Wait for a connection on the local port 
    client = ss.accept(); 

    final String SenderIp=client.getInetAddress().getHostAddress(); 
    System.out.println("Client Ip address "+ SenderIp); 

..... 

// cookie convertion moodified to original is done here 
    (modified cookie ---> original cookie value) 
..... 



// forward HttpRequest to server 
byte[] requestBytes = requestString.getBytes("ISO-8859-1"); 
streamToServer.write(requestBytes,0,requestBytes.length); 
streamToServer.flush(); 

} 



// Read the server's responses 
     // and pass them back to the client. 
     int bytesRead; 
     try { 
      while ((bytesRead = streamFromServer.read(reply)) != -1) { 

     String responseString = new String(reply, 0, bytesRead, "ISO-8859-1"); 
     System.out.println("Reply string "+responseString); 


     // cookie modification done here 

     System.out.println("Forwarded to IP "+SenderIp); 
     byte[] responseBytes = responseString.getBytes("ISO-8859-1"); 
     streamToClient.write(responseBytes, 0, responseBytes.length); 
     streamToClient.flush(); 

     HashMap(original_cookie, modified cookie) 
} 
} 

は、どのように私は誰にこのHttpResponseにはに送信されますIPアドレスを知ることができますか?私は、クライアントのIPアドレスを私が送信するアドレスを変更します。

外部サーバーからHttpResponseを取得した後、HashMap(original_cookie、IPアドレス、変更されたCookie)を使用します。

ありがとうございます。

+0

あなたは 'request'の[' X_Forwarded_For'](https://en.wikipedia.org/wiki/X-Forwarded-For)(以降 'Forwarded')ヘッダからそれを得ます。 –

+0

@ElliottFrisch、すぐにお返事ありがとうございます。私はコアのJavaを使用しています。 X_Forwarded_Forはサーブレットで利用可能です。任意のオプションのコアJavaソケットプログラミング? – bholanath

+0

私はあなたが何を求めているのか分かりません。 'X_Forwarded_For'は通常あなたが書いているプロキシ*によって追加されます。 –

答えて

関連する問題