10.1 Java

Team-Fly    

 
Malicious Mobile Code: Virus Protection for Windows
By Roger A. Grimes
Slots : 1
Table of Contents
Chapter 10.  Malicious Java Applets


Java , developed by Sun Microsystems (http://www.sun.com), is a programming language just like any other you might be familiar with. Although it is not C++, it was intentionally written with a similar syntax to decrease the learning curves of the many C++ programmers today. It is easier to learn than C++, has better memory management, and has been optimized for network distribution. Today, when you hear Java, it can mean the Java programming language or the whole platform of programming tools designed to support the core language.

Sun Microsystems's Java Software Division, now known as the freestanding JavaSoft figs/u2122.gif (http://www.javasoft.com)company, started developing Java in the early 1990s as a programming language to interact with common consumer appliances and devices. The vision of the IP-connected toaster is probably not as far- fetched as people used to believe. Imagine your air conditioning thermostat automatically adjusting itself for the expected heat output from your dinner cooking or your microwave contacting the local authorized service dealer for periodic maintenance. Sun has not lost this encompassing vision and continues to push its dream with its Jini figs/u2122.gif (http://www.jini.org) architecture and Java2 Micro Edition (J2ME).

Best of all, Java is free. Anyone can download the latest Java Development Kit (JDK) from Sun Microsystems at http://java.sun.com and start writing Java programs. Be forewarned that software developer kits can be difficult to understand for nonprogrammers. That said, Sun's web site contains all the tools you would need to learn, write, compile, and start publishing Java programs. Of course, you can buy a variety of commercial products to optimize your Java experience.

The Java programming language is a complete, feature-rich product that can be written to do almost anything. Java's biggest goal is the ability to "Write Once, Run Anywhere". Supposedly, a programmer can write a Java application and it will run anywhere the Java environment is supported -- which includes most of the popular computer platforms like Windows 9x/NT, OS/2, Irix, OpenVMS, FreeBSD, Linux, Netware, OS/400, and Macintosh.

In many companies, Java has replaced C++ as the programming language of choice. Java can be written to create network applications, database interfaces, telephony projects and graphical user tools. Despite popular belief, Java applications do not need a browser to run, just a Java runtime environment . Sun has developed a free Java runtime environment to help entrench Java within corporate shops . Most experts agree that Java is a very capable language, but it tends to run a little slower than C or C++. Sun is helping to fix the speed problem by providing faster Java compilers and hardware-based solutions.

10.1.1 Java Virtual Machine

A Java Virtual Machine (JVM) is a software-based virtual environment where Java applications can exist and manipulate computer resources. The JVM is specifically written for each computer platform (usually by the operating system or browser vendor), as it handles and translates requests from Java programs to the operating system for computer resources. The JVM allows Java programmers to forget about the intricate details of how a particular operating system platform accesses memory or files. They can write programs in Java, and the JVM will translate the commands into requests the operating system can understand.

Java is able to undertake the goal of "Write Once, Run Anywhere" by using a pseudo-interpreted process. Interpreted is a key word. Java programs are interpreted, just as Basic is, and must be eventually converted (a process that slows down running programs) to its final machine language form just prior to execution in the CPU. A non-interpreted language, like C++, is completely compiled into native machine language prior to distribution and runs very quickly without having to wait for runtime translation. Java has a pseudo-compilation process that does an intermediate conversion, but it still needs interpretation at runtime.

Many browsers, including Internet Explorer, contain a separate JVM that has to be installed in order for Java applets to work. Internet Explorer comes with a Microsoft version. Sun has a JVM plug-in that can be used in many different browsers. This chapter and the next discusses Internet Explorer running Microsoft's own JVM.

10.1.2 Java Byte Code

A Java applet is written by typing the Java language program into any ASCII editor and saving it as a text file with a .JAVA extension. The source code text file is then processed by a Java compiler into intermediate byte code . The compiled byte code is saved with a .CLASS extension. Most of the Java language, itself, is stored in class files, with the exception of supporting files that hook it into the operating system. Related class files are grouped together and stored as a package . Most class files and packages for Internet Explorer can be found at C:\%windir%\Java\Packages or in the folder specified under the following registry key: HKLM\Software\Microsoft\Code Store Database.

The byte code output is what is downloaded into Internet Explorer when you surf across a Java-enabled HTML page. The JVM downloads the byte code, verifies it, and then executes it. The JVM has the daunting task of interpreting the byte code into platform-specific instructions that can be executed. Example 10-1 shows the Java source to a sample applet.

Example 10-1. Java applet source code -- Sample.java
 /Sample.java /Draws small square on browser screen import java.awt.Graphics; public class Sample extends java.applet.Applet {         public void paint(Graphics g) {         g.drawRect(0,0,100,100);         } } 

Next, the text source code is run through the Java compiler to produce byte code, as shown in Example 10-2.

Example 10-2. Java applet byte code representation -- Sample.class
 Compiled from Sample.java public synchronized class Sample extends java.applet.Applet      /* ACC_SUPER bit set */ {     public void paint(java.awt.Graphics);     public Sample(  ); } Method void paint(java.awt.Graphics)    0 aload_1    1 iconst_0    2 iconst_0    3 bipush 100    5 bipush 100    7 invokevirtual #5 <Method void drawRect(int, int, int, int)>   10 return Method Sample(  )    0 aload_0    1 invokespecial #4 <Method java.applet.Applet(  )>    4 return 

A Java-enabled HTML page must include special tag indicators to link the Java applet into the HTML page. When Internet Explorer reads the HTML code, the referenced Java applet is automatically downloaded and executed. The following three HTML tags can be used to load a Java applet, depending on your browser and version:

  • <APPLET CODE=> and </APPLET>

  • <OBJECT> and </OBJECT>

  • <EMBED> and </EMBED>

If your browser doesn't support Java or a particular type of Java tag, the tags are simply ignored and the Java applet is not executed. I give examples of all three in Example 10-3, although most web pages would not contain all three.

Example 10-3. Example HTML page loading sample applet
 <HTML> <HEAD> <TITLE> Draw a Square </TITLE> </HEAD> <BODY> Here is a sample square: <APPLET CODE="Sample.class" HEIGHT=101 WIDTH=101 </APPLET> <OBJECT codetype="application/java" classid="java:Sample.class"         width="101" height="101"> </OBJECT> <EMBED SRC="sample.class" WIDTH="101" HEIGHT="101"> </EMBED> </BODY> </HTML> 
Figure 10-1. Example of sample Java program
figs/mmc_1001.gif

The gray rectangle area where the applet runs was defined by the HTML code when loading the applet. The applet, Sample , only drew the square outline (see Figure 10-1).

10.1.3 Java Applet Versus Java Application

Java applet programs use a special subset of the Java programming language customized to run within compatible web browsers. There are lots of browsers that currently support Java including Microsoft Internet Explorer figs/u2122.gif , Netscape Navigator figs/u2122.gif , Opera figs/u2122.gif , and Sun's own HotJava figs/u2122.gif . Java-enabled browsers began appearing with the 2.x versions of Netscape Navigator and 3.x versions of Internet Explorer, although it was an earlier version of Java, JDK 1.0. You need Internet Explorer version 4.0 or later to support the extended feature set of the most popular Java versions, JDK 1.1.X. Javasoft significantly updated the Java Development Kit in version 1.2, or Java 2 figs/u2122.gif , especially where security was concerned . The latest version, Java 1.3, is currently only supported by Netscape 6 and Opera 5. Applets written to the earlier standards are usually backward compatible in the newer releases.

Java applets can be used to add all kinds of excitement and interactivity to a web page. Java applets add multimedia effects, animations, music, interactive games ; they also respond to mouse cursor movement, and to make sophisticated web forms. While Java applications can do anything a normal program can do, remotely loaded applets are greatly restricted. Java's creators knew malicious code writers would jump all over a language that automatically downloads and executes without the user's explicit permission. With that in mind, Java's security team limited what an untrusted applet could do.

10.1.4 Java's Just-In-Time Compiler

Java's speed was suffering in comparison to its closest rivals. In some cases, a purely compiled C++ program could outperform Java 50 to 1. The software industry responded by making several Just-In-Time (JIT) compilers. JavaSoft included Symantec's 32-bit Windows JIT compiler with JDK 1.1, which ended up in 3.x releases of Internet Explorer. Although you won't see any outwardly noticeable clues, the JIT complier substantially increased the performance of Java programs so that they now near C++ speeds. Internet Explorer allows you to turn on and off the JIT component by choosing Tools figs/u2192.gif Internet Options figs/u2192.gif Advanced and un-checking the JIT selection.

JIT compilers work by converting the entire Java class subroutine (called a method ) into native machine language and placing it into memory instead of allowing the JVM to slowly interpret the byte codes individually. With a JIT compiler, if the method gets called again, it is already compiled into machine language and waiting for immediate execution at the CPU; and it no longer has to undergo many of the repetitive normal boundary checks that interpreted byte codes do.

For example, Java applets frequently use symbolic language references to fetch data stored somewhere in memory. An applet might tell the JVM to grab the data area called DATA_STORE. In byte code, the symbolic memory address is not converted to its real physical memory address location until runtime. With a JIT compiler installed, once the symbolic memory location is resolved to a physical location, future calls to the same memory location by the same method are made with the faster physical address.

The initial loading of the JIT compiler and its initial conversion process slows down the first-time execution of any Java method. But as long as a Java program calls the method more than once, it will usually result in faster code execution. While this does speed up the code, the lack of continuous boundary checking and further complexity might be a future security hole.


Team-Fly    
Top


Malicious Mobile Code. Virus Protection for Windows
Malicious Mobile Code: Virus Protection for Windows (OReilly Computer Security)
ISBN: 156592682X
EAN: 2147483647
Year: 2001
Pages: 176

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net