Java究竟怎么玩?

天地程序已定棋,人间大数待变局

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理
 

关于本例:


 首先声明一下,这是一个副产品,暂定名叫LocalOS.写它的起因在于放假时去亲戚家串门,脑袋一热答应了下星期帮亲戚孩子做个游戏外挂,谁让国人都喜欢认为[IT人士]就是举凡和计算机有关的都会的人(而且不分软硬|||)。因为没写过外挂,手里没有相关类库,所以用什么写都一样,嫌分析封包麻烦并且也没时间,本想用Java写个汇编类,然后调用游戏本身指令进行挂机.目的有二:一是为了巩固相关的Java和汇编知识,二是强调下在软件世界中,Java能做什么并不是问题,Java不能做什么才是问题。
 
 但事实上看,今天我回家后发觉一边写Java汇编的基础类库一边分析游戏做外挂似乎不赶趟|||,所以暂时放弃Java开发,直接用VB做界面,C++写核心了. 

 暂时把写了一部分的Java执行汇编指令例子丢出来,等有时间再继续,顺便希望有人能帮我把类库补全.

 以下是刚写的这个类库的演示用例,发完了我就准备睡觉了~~~一不留神都这时候了~~~~明天上班还有事呢||||||||

目前提供的系统接口类:

  1. package org.loon.framework.os;
  2. /**
  3.  * Copyright 2008
  4.  * 
  5.  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  6.  * use this file except in compliance with the License. You may obtain a copy of
  7.  * the License at
  8.  * 
  9.  * http://www.apache.org/licenses/LICENSE-2.0
  10.  * 
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  13.  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  14.  * License for the specific language governing permissions and limitations under
  15.  * the License.
  16.  * 
  17.  * @project loonframework
  18.  * @author chenpeng
  19.  * @email:ceponline@yahoo.com.cn
  20.  * @version 0.1
  21.  */
  22. class Kernel {
  23.     
  24.     static {
  25.         System.loadLibrary("localos");
  26.     }
  27.     /**
  28.      * 获得内联汇编执行结果
  29.      * 
  30.      * @param asmBytes
  31.      * @return
  32.      */
  33.     public native static long doResult(final byte[] asmBytes);
  34.     /**
  35.      * 将内联汇编动态注入指定进程
  36.      * 
  37.      * @param pid
  38.      * @param asmBytes
  39.      * @return
  40.      */
  41.     public native static boolean doInject(final int pid, byte[] asmBytes);
  42.     /**
  43.      * 写指定线程内存
  44.      * 
  45.      * @param pid
  46.      * @param address
  47.      * @param buffer
  48.      * @param size
  49.      * @param numberOfBytesWrite
  50.      * @return
  51.      */
  52.     public native static boolean writeProcessMemory(final int pid,
  53.             final int address, final byte[] buffer, final int size,
  54.             final int[] numberOfBytesWrite);
  55.     /**
  56.      * 读指定线程内存
  57.      * 
  58.      * @param pid
  59.      * @param address
  60.      * @param buffer
  61.      * @param size
  62.      * @param numberOfBytesWrite
  63.      * @return
  64.      */
  65.     public native static boolean readProcessMemory(final int pid,
  66.             final int address, final byte[] buffer, final int size,
  67.             final int[] numberOfBytesWrite);
  68.     /**
  69.      * 读指定线程内存
  70.      * 
  71.      * @param pid
  72.      * @param address
  73.      * @return
  74.      */
  75.     public static byte readProcessMemory(final int pid, final int address) {
  76.         byte[] buffer = new byte[1];
  77.         int[] numberOfBytesRead = new int[1];
  78.         readProcessMemory(pid, address, buffer, 1, numberOfBytesRead);
  79.         return buffer[0];
  80.     }
  81.     /**
  82.      * 以指定的访问方法进入一个已存在的进程
  83.      * 
  84.      * @param mode
  85.      * @param pid
  86.      * @return
  87.      */
  88.     public native static int openProcess(final int mode, final int pid);
  89.     /**
  90.      * 强制结束指定进程
  91.      * 
  92.      * @param pid
  93.      * @return
  94.      */
  95.     public native static boolean killProcessID(final int pid);
  96.     /**
  97.      * 检查指定进程是否在运行中
  98.      * 
  99.      * @param pid
  100.      * @return
  101.      */
  102.     public native static boolean isProcessRunning(final int pid);
  103.     /**
  104.      * 返回当前进程中所有程序名
  105.      * 
  106.      * @return
  107.      */
  108.     public native static Object[] getProcessNames();
  109.     /**
  110.      * 返回指定程序名的唯一进程id
  111.      * 
  112.      * @param processName
  113.      * @return
  114.      */
  115.     public native static int getProcessID(final String processName);
  116. }


示例代码:
  1. package org.loon.test;
  2. import org.loon.framework.os.ASM;
  3. /**
  4.  * Copyright 2008
  5.  * 
  6.  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  7.  * use this file except in compliance with the License. You may obtain a copy of
  8.  * the License at
  9.  * 
  10.  * http://www.apache.org/licenses/LICENSE-2.0
  11.  * 
  12.  * Unless required by applicable law or agreed to in writing, software
  13.  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  14.  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  15.  * License for the specific language governing permissions and limitations under
  16.  * the License.
  17.  * 
  18.  * @project loonframework
  19.  * @author chenpeng
  20.  * @email:ceponline@yahoo.com.cn
  21.  * @version 0.1
  22.  */
  23. public class TestASM {
  24.     /**
  25.      * 格式化输出信息
  26.      * 
  27.      * @param mes
  28.      * @param a
  29.      * @param b
  30.      * @return
  31.      */
  32.     public static String formatMessage(String mes, int a, int b) {
  33.         Integer a1 = new Integer(a);
  34.         Integer b1 = new Integer(b);
  35.         return String.format(mes, new Object[] { a1, b1 });
  36.     }
  37.     /**
  38.      * java进行汇编加法
  39.      * 
  40.      * @param asm
  41.      * @param a
  42.      * @param b
  43.      * @return
  44.      */
  45.     public static void plus(ASM asm, int a, int b) {
  46.         asm._MOV_EAX(a);
  47.         asm._ADD_EAX(b);
  48.         asm._RET();
  49.         String mes = formatMessage("Java 进行汇编加法计算 %d + %d = ", a, b);
  50.         System.out.println(mes + asm.doResult());
  51.     }
  52.     /**
  53.      * java进行汇编减法
  54.      * 
  55.      * @param asm
  56.      * @param a
  57.      * @param b
  58.      */
  59.     public static void minus(ASM asm, int a, int b) {
  60.         asm._MOV_EAX(a);
  61.         asm._SBB_EAX(b);
  62.         asm._RET();
  63.         String mes = formatMessage("Java 进行汇编减法计算 %d - %d = ", a, b);
  64.         System.out.println(mes + asm.doResult());
  65.     }
  66.     /**
  67.      * java进行汇编乘法
  68.      * 
  69.      * @param asm
  70.      * @param a
  71.      * @param b
  72.      */
  73.     public static void multiply(ASM asm, int a, int b) {
  74.         asm._MOV_EAX(a);
  75.         asm._MOV_EBX(b);
  76.         asm._IMUL_EAX_EBX();
  77.         asm._RET();
  78.         String mes = formatMessage("Java 进行汇编乘法计算 %d * %d = ", a, b);
  79.         System.out.println(mes + asm.doResult());
  80.     }
  81.     /**
  82.      * java进行汇编除法
  83.      * 
  84.      * @param asm
  85.      * @param a
  86.      * @param b
  87.      */
  88.     public static void divide(ASM asm, int a, int b) {
  89.         asm._XOR_EDX_EDX();
  90.         asm._MOV_EAX(a);
  91.         asm._MOV_ECX(b);
  92.         asm._IDIV_ECX();
  93.         asm._RET();
  94.         String mes = formatMessage("Java 进行汇编除法计算 %d / %d = ", a, b);
  95.         System.out.println(mes + asm.doResult());
  96.     }
  97.     public static void main(String[] args) {
  98.         // PS:ASM类中不是所有汇编指令都有,因为太多,有时间再慢慢加.已写的不保证全部正确|||.
  99.         // 添加ASM类的方法大体有三种:
  100.         // 1、用汇编指令名和操作码自己一个个对
  101.         // 2、自己写汇编,然后debug读操作码
  102.         // 3、开OD之类的反汇编~
  103.         // 反正所有汇编命令都能实现,关键看有没有恒心,谁有空帮忙写下……
  104.         ASM asm = new ASM();
  105.         // 加法测试
  106.         plus(asm, 6565763295454157);
  107.         // 减法测试
  108.         minus(asm, 99656512345);
  109.         // 乘法测试
  110.         multiply(asm, 18412009);
  111.         // 除法测试
  112.         divide(asm, 1982090112);
  113.     }
  114. }
 
  运行效果图如下:  

 

   大家用脚趾头都能猜出来是JNI实现的,毕竟和系统本地交互不用JNI是不可能的,目前仅支持Windows系统.不过在当今的Java世界中,由于SWT已经提供了良好的本地环境库支持,事实上用它便可以直接写出大多数系统与Java的汇编混合代码,但这与本例无关,自己琢磨吧,我安息了:)

  CSDN下载地址http://download.csdn.net/source/940199

  下载地址:http://code.google.com/p/greenvm/downloads/list (暂时先丢这上面 源码在Jar内)

   OD(ollydbg,传说中的汇编分析调试工具)下载地址:http://download.csdn.net/source/940795


posted on 2009-01-04 23:58  cping  阅读(775)  评论(0编辑  收藏  举报