Showing posts with label Hacking. Show all posts
Showing posts with label Hacking. Show all posts

Thursday, July 31, 2008

.WAR Games - .WAR ゲームズ

So lets talk about .WARs, if you see my previous post you will know they are simply web application archive files. They are zip files that contain all the necessary components (jsp/images/html/java classes) that are required for the web application to work. Lets go over the structure, ascii-style. And by ascii, I mean a bitmap of ascii art.


Yeah that looks Terrible. Anyways, so thats the structure of the .WAR file. How do you extract it? I mentioned earlier it is simply a zip file, you can exact it via an unzip program, or with java provided you installed the latest JDK. If the java directory is in your path, you can just do "jar -xf .war" without the quotes. Let's do a trial run here and download Some Random Java Application. Just download the ginp.war file. Once that is completed, unzip the file, and you should see a directory structure similiar to the one I described above. Always go directly to the WEB-INF/ directory and open the web.xml first. Let's pull out a servlet's details...
<servlet-name
>ginp Controller Servlet</servlet-name>
...
<servlet-class>net.sf.ginp.GinpServlet</servlet-class>
...
<servlet-mapping>
<servlet-name>ginp Controller Servlet</servlet-name>
<url-pattern>/styles/backinblack/ginpservlet</url-pattern>
</servlet-mapping>

So the ginp Controller Servlet is mapped to a url /styles/backinblack/ginpservlet. This means we can access it via: http://thehost/appname/styles/backinblack/ginpservlet. Easy as that.
It may take some parameters, but we don't know, so lets look at the class.
Under servlet-class we see net.sf.ginp.GinpServlet, so if we go into the WEB-INF directory, then classes/net/sf/ginp/ directory we can see none other than... GinpServlet.class!
Lets decompile the class with Jad. From the /ginp/ directory run the command (provided jad is in your path):
C:\Documents and Settings\eff.bee.eye\Desktop\kougeki\ginp\WEB-INF\classes\net\s
f\ginp>jad -8 -s .java GinpServlet.class

Parsing GinpServlet.class... Generating GinpServlet.java
Couldn't resolve all exception handlers in method doHttpMethod
Couldn't fully decompile method _mthclass$
Couldn't resolve all exception handlers in method _mthclass$

Most servlets process GET requests and forward them to a POST (doPost) method. This one just forwards it to another method doHttpMethod. We're starting to see a number of classes being referenced outs
ide our initial servlet. When this happens, it's usually a good idea to just decompile the entire package:
C:\Documents and Settings\eff.bee.eye\Desktop\kougeki\ginp\WEB-INF\classes>jad -8 -s .java -r */**/*.class
Parsing net/sf\ginp/CommandParameter.class... Generating net\sf\ginp\CommandParameter.java

....
From here we would begin to do a code-review to identify areas of potential vulnerability but, I think we will leave code-reviewing for the next installment! Until then!

.WARの話しましょう。前の記事を見たらWARファイルはWebアプリケーションアーカイブを知っています。アプリケーションの実行するためにWARは 必要なファイル(JSP/画像/HTML/Javaクラス)を含んでいるZipファイルです。 ASCIIスタイルでWARのディレクトリ構成をレビュー します。実はASCIIスタイルというのはbitmap画像です。


↑見た目が本当に悪いですよね。上記の画像はWARファイルの構成の説明です。どの方法でアーカイブから解凍しますか。前の時、zipファイルだと言ったので、Unzipプログラムで解凍するか、JavaのJDKをインストールしたらJarアプリでも解凍できます。 Javaパースを環境値に含んでいる場合、”jar -xf [appname].war"のコマンドを実行すると、自動的に解凍します。試しに適当なJavaアプリケーションを探して、サイトからginp.warファイルをダウンロードします。そのあと、解凍すると、上記構成のように見えます。いつも、初めにWEB-INFディレクトリのweb.xml設定ファイルを開きます。最初のJavaサーブレットを見ましょう。
<servlet-name>ginp Controller Servlet</servlet-name>
...
<servlet-class>net.sf.ginp.GinpServlet</servlet-class>
...
<servlet-mapping>
<servlet-name>ginp Controller Servlet</servlet-name>
<url-pattern>/styles/backinblack/ginpservlet</url-pattern>
</servlet-mapping>

ginp Controller Servletは/styles/backinblack/ginpservletに参照します。そのurl-patternの値が存在するので、直接ブラウザーでhttp://thehost/appname/styles/backinblack/ginpservletへアクセスできます。
それは簡単です。そのサーブレットはパラメータが必要かもしれないので、Javaクラスを見ましょう。 servlet-classの値でnet.sf.ginp.GinpServletがあるので、WEB-INF/classes/net/sf/ginpのディレクトリに入って、GinpServlet.classファイルを見ます。このクラスファイルをJadでディコンパイルしましょう。「/ginp」ディレクトリで(Jadプログラムを環境値にパース入っている場合)以下のコマンドを実行します。

C:\Documents and Settings\eff.bee.eye\Desktop\kougeki\ginp\WEB-INF\classes\net\s
f\ginp>jad -8 -s .java GinpServlet.class
Parsing GinpServlet.class... Generating GinpServlet.java
Couldn't resolve all exception handlers in method doHttpMethod
Couldn't fully decompile method _mthclass$
Couldn't resolve all exception handlers in method _mthclass$

殆どのサーブレットはHTTPのGETメソッドを受付すると、POSTメソッド(JavaのdoPostメソッド)に転送します。このアプリは両方のメソッドを他のJavaメソッドdoHttpMethodに転送します。GinpServletは複数のクラスに
このGinpServletクラスを理解するには、クラスファイル内の複数の参照を見る必要があるので、全てのアプリケーション名前空間ファイルをディコンパイルしたら良いです。
C:\Documents and Settings\eff.bee.eye\Desktop\kougeki\ginp\WEB-INF\classes>
jad -8 -s .java -r */**/*.class
Parsing net/sf\ginp/CommandParameter.class... Generating net\sf\ginp\CommandParameter.java
ここから可能な脆弱性を発見するために、コードレビューをしますが、今度の記事でコードレビュー方法を説明します。それまで!

Saturday, July 12, 2008

J2EE Hacking Server First Steps - J2EEサーバーのハッキング紹介

So, you just downloaded and installed some j2ee server and you want to hack it. What are the first steps you should take? You should be aware of the application servers architecture, usually you can read the manuals to get a high level understanding of how it works. You'll probably see a bunch of ridiculous java terms being thrown around, so lets do a quick run down of what they are;
  • JSP - Java Server Pages, a form of scripting that allows the developer to inter-mix HTML and Java code in the same file. Much the same as ASP, PHP and other Web Programming Languages.
  • Web Services - Basically an API that is accessible over the HTTP protocol using standard HTTP methods. Client sends a request, the server processes it, runs some code and returns the results back to the client. Messages are commonly transfered in XML messages that follow the SOAP standard.
  • SOAP - [Simple Object Access Protocol] is a protocol for exchanging XML messages to a Web Service.
  • WSDL - Web Services Definition Language, yet another xml format for determining how a client should call a web service.
  • JAR - Java ARchive, basically a zip file with all html, images, java classes, jsp pages required for an application.
  • WAR - Web ARchive, Web Application Archive, same format as a JAR but specifically for web applications. Contains the web.xml configuration file along with classes, other jars and anything else required by the web application.
  • EAR - Enterprise ARchive, basically an application that contains a bunch of web 'modules' that work together to form an enterprise application. Think of it as a container for multiple web application(s) and their components.
  • EJB- Enterprise Java Bean, used for housing the 'business' or 'application' logic. It's a rather advanced architecture that is run inside of a J2EE server. It has its own specifications and API, please see sun's website: http://java.sun.com/products/ejb/.
  • RMI - Remote Method Invocation, allows programmers to write objects in a distributed environment. These objects can be called remotely.
  • ORB-IIOP - Object Request Broker Internet Inter-Orb Protocol, a communication protocol for two systems to access objects. This is a language neutral communication protocol. These are accessible via remote clients through RMI-IIOP. For more information on RMI-IIOP see: http://en.wikipedia.org/wiki/RMI-IIOP.
  • JRMP - Java Remote Method Protocol, like ORB-IIOP but for one java application to access the objects of a different java applications.
  • JMS - Java Message Service, an API for sending messages between two or more clients.
  • JMX-RMI - Java Management eXtensions Remote Method Invocation is used for managing and monitoring the servers applications.
  • JNI - Java Native Interface, Allows java code to integrated with code in other langauges such as C/C++. (Good for legacy systems).
  • JAX-WS - Java API for XML Web Services, basically an API that utilizes XML messages for communication between the web service and the client.
  • JSF - Java Server Faces, a framework for building MVC (Model View Controller) web applications. Competes with Apache Struts.
  • JBI - Java Business Integration, a specification for implementing a SOA (Service Oriented Archeticture). JBI is built upon the web services model.
  • And many more...
This is one reason I hate java so much, too many acronyms and specifications, most of which are quite useless.

Tools.
I'm not much of a 'tools' person, I prefer simple methods and if required, I write my own scripts. But there are a few I simply can not live without and as they relate to hacking J2EE apps and servers here's what I usually use.
  • Burpsuite - A HTTP Intercepter Proxy tool. I've been using it for almost 6 years now and I still love it.
  • Jad - Java Decompiler. A *must* for decompiling java bytecode.
  • cygwin - I'm a windows guy, but I also know when Unix tools are better served ;>.
  • SysInternals Procexp and Tcpview. Good to see which files and ports are in use by a process.
  • More to come later.
Thats it for my quick introduction, later I'll be posting what to actually /do/ to figure out what vulnerabilities may exist in J2EE servers.

新しいJ2EEサーバーをダウンロード、インストールするとハッキングをしたくなりますよね。最初はどうするか。はじめに、サーバーのアーキテクチャの説明書を読んで、一般的な知識を得ればいいと思います。
確かに、おかしいJava言葉をばっかり見るのでちょっとレビューしますよ。

* JSP - (Java Server Pages) 開発者のためにHTMLとJavaスクリプトコード同じファイルに入れるWebスクリプティング言語です。これはASPやPHPやほかのWebプログラミング言語と同じです。
* Web Services - 基本的に、HTTPプロトコールでプログラミングAPI。クライアントはリクエストを送信し、サーバー側でコードを実行し、レスポンスを送ります。一般的に送信データの構成がSOAP標準構成で実装します。
* SOAP - (Simple Object Access Protocol)シンプルオブジェクトアクセスプロトコールはXMLメッセージをクライアントからサーバーに切り替えるプロトコールです。
* WSDL - (Web Services Definition Language) WebサービスデフィニションランゲージはXMLドキュメントでWebServiceの定義ファイルです。このファイルを理解すれば、クライアントはサーバーのWebサービスを使えます。
* JAR - (Java ARchive) アプリケーションのために、HTML、画像、Javaクラス、JSPファイルなど、必要なファイルをジップファイルに含むアーカイブです。
* WAR - (Web ARchive) WebアプリケーションアーカイブはJARと同じ構成ですが、Webアプリケーションのだけです。このアーカイブはweb.xmlコンフィグレーションファイル、Javaクラスファイル、JARファイルなどの必要ファイルを含むアーカイブです。
* EAR - (Enterprise ARchive)企業アプリケーションの関連Webモジュールを含むアプリケーションアーカイブです。複数のWebアプリケーションためのコンテナーとして用いられる。
* EJB- (Enterprise Java Bean)EJBは事務ロジックかアプリケーションロジックのための実装です。 結構上級なJ2EEサーバーでJavaBean実装です。 EJBのAPI説明書を見てください:http://java.sun.com/products/ejb/.
* RMI - (Remote Method Invocation)分散型環境のためJavaオブジェクトを作れます。 このオブジェクトはリモートから呼び出します。
* ORB-IIOP - (オブジェクトリクエストブローカーインターネットインターオアブプロトコール)このプロトコールは二つのシステムの間でオブジェクトを交換する。ORB-IIOPがプログラミング言語に依存していません。 IIOPの説明書を見てください:http://ja.wikipedia.org/wiki/IIOP.
* JRMP - (Java リモートメソッドプロトコール)ORB-IIOPのようですが、JRMPは他のJavaアプリケーションオブジェクトへアクセスするJavaアプリケーションのために使用される。
* JMS - (Java メッセージサービス) 二つ(もしくはそれ以上)のクライアントの間でメッセージを送信する。
* JMX-RMI - (Java Management eXtensions Remote Method Invocation)JMX-RMIはサーバーのアプリケーションを管理や監視のために使用される。
* JNI - (Java Native Interface) Javaコードは他の言語(C/C++)でインテグレートできるインタフェースです。 (古いシステムに良いです)。
* JAX-WS - (Java API for XML Web Services) クライアントとサーバーの送信に使用するためのXMLメッセージAPI.
* JSF - (Java Server Faces)Sun MicrosystemsのMVC(モデル、ビュー、コントローラー)のWebアプリケーションフレームワーク。JSFはApacheStrutsに対抗する。
* JBI - (Java Business Integration)SOA(サービス向けアーキテクチャ)の実装です。JBIはWebサービスモデルで作成された。
* その他

略語と仕様がたくさんあるので僕はJavaが嫌いです。殆どの仕様は役に立ちません。

ツール。
僕はツールばかり使う人ではありません。僕の意見は簡単な方法でハッキングすることが一番良いと思います。もし、必要ならば、自分でツールやスクリプトを作ります。でも、J2EEアプリケーションとサーバーをハッキングするため、必要なツールをもちろん使います。
以下参照:
  • Burpsuite - HTTPのインターセプタープロクシツール。 6年間使用中、かなり気にいってます。
  • Jad - Javaデコンパイラ。Javaバイトコードをデコンパイルするために使う必要があります。
  • cygwin - 僕はWindowsの方が好きですが、Unixツールを使う時も多いです。
  • SysInternals - ProcexpとTcpview。このツールの目的はどのファイルやどのポートをプロセスに使うか検索します。
  • 随時追加。
以上ですが、次回はJ2EEのハッキングやり方と脆弱性の見つけ方を説明します。