Java与汇编的混合操作类库及示例



关于本例:


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

但事实上看,今天我回家后发觉一边写Java汇编的基础类库一边分析游戏做外挂似乎不赶趟|||,所以暂时放弃Java开发,直接用VB做界面,C++写核心了.

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

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

目前提供的系统接口类:

  1. packageorg.loon.framework.os;
  2. /**
  3. *Copyright2008
  4. *
  5. *LicensedundertheApacheLicense,Version2.0(the"License");youmaynot
  6. *usethisfileexceptincompliancewiththeLicense.Youmayobtainacopyof
  7. *theLicenseat
  8. *
  9. *http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. *Unlessrequiredbyapplicablelaworagreedtoinwriting,software
  12. *distributedundertheLicenseisdistributedonan"ASIS"BASIS,WITHOUT
  13. *WARRANTIESORCONDITIONSOFANYKIND,eitherexpressorimplied.Seethe
  14. *Licenseforthespecificlanguagegoverningpermissionsandlimitationsunder
  15. *theLicense.
  16. *
  17. *@projectloonframework
  18. *@authorchenpeng
  19. *@email:ceponline@yahoo.com.cn
  20. *@version0.1
  21. */
  22. classKernel{
  23. static{
  24. System.loadLibrary("localos");
  25. }
  26. /**
  27. *获得内联汇编执行结果
  28. *
  29. *@paramasmBytes
  30. *@return
  31. */
  32. publicnativestaticlongdoResult(finalbyte[]asmBytes);
  33. /**
  34. *将内联汇编动态注入指定进程
  35. *
  36. *@parampid
  37. *@paramasmBytes
  38. *@return
  39. */
  40. publicnativestaticbooleandoInject(finalintpid,byte[]asmBytes);
  41. /**
  42. *写指定线程内存
  43. *
  44. *@parampid
  45. *@paramaddress
  46. *@parambuffer
  47. *@paramsize
  48. *@paramnumberOfBytesWrite
  49. *@return
  50. */
  51. publicnativestaticbooleanwriteProcessMemory(finalintpid,
  52. finalintaddress,finalbyte[]buffer,finalintsize,
  53. finalint[]numberOfBytesWrite);
  54. /**
  55. *读指定线程内存
  56. *
  57. *@parampid
  58. *@paramaddress
  59. *@parambuffer
  60. *@paramsize
  61. *@paramnumberOfBytesWrite
  62. *@return
  63. */
  64. publicnativestaticbooleanreadProcessMemory(finalintpid,
  65. finalintaddress,finalbyte[]buffer,finalintsize,
  66. finalint[]numberOfBytesWrite);
  67. /**
  68. *读指定线程内存
  69. *
  70. *@parampid
  71. *@paramaddress
  72. *@return
  73. */
  74. publicstaticbytereadProcessMemory(finalintpid,finalintaddress){
  75. byte[]buffer=newbyte[1];
  76. int[]numberOfBytesRead=newint[1];
  77. readProcessMemory(pid,address,buffer,1,numberOfBytesRead);
  78. returnbuffer[0];
  79. }
  80. /**
  81. *以指定的访问方法进入一个已存在的进程
  82. *
  83. *@parammode
  84. *@parampid
  85. *@return
  86. */
  87. publicnativestaticintopenProcess(finalintmode,finalintpid);
  88. /**
  89. *强制结束指定进程
  90. *
  91. *@parampid
  92. *@return
  93. */
  94. publicnativestaticbooleankillProcessID(finalintpid);
  95. /**
  96. *检查指定进程是否在运行中
  97. *
  98. *@parampid
  99. *@return
  100. */
  101. publicnativestaticbooleanisProcessRunning(finalintpid);
  102. /**
  103. *返回当前进程中所有程序名
  104. *
  105. *@return
  106. */
  107. publicnativestaticObject[]getProcessNames();
  108. /**
  109. *返回指定程序名的唯一进程id
  110. *
  111. *@paramprocessName
  112. *@return
  113. */
  114. publicnativestaticintgetProcessID(finalStringprocessName);
  115. }


示例代码:
  1. packageorg.loon.test;
  2. importorg.loon.framework.os.ASM;
  3. /**
  4. *Copyright2008
  5. *
  6. *LicensedundertheApacheLicense,Version2.0(the"License");youmaynot
  7. *usethisfileexceptincompliancewiththeLicense.Youmayobtainacopyof
  8. *theLicenseat
  9. *
  10. *http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. *Unlessrequiredbyapplicablelaworagreedtoinwriting,software
  13. *distributedundertheLicenseisdistributedonan"ASIS"BASIS,WITHOUT
  14. *WARRANTIESORCONDITIONSOFANYKIND,eitherexpressorimplied.Seethe
  15. *Licenseforthespecificlanguagegoverningpermissionsandlimitationsunder
  16. *theLicense.
  17. *
  18. *@projectloonframework
  19. *@authorchenpeng
  20. *@email:ceponline@yahoo.com.cn
  21. *@version0.1
  22. */
  23. publicclassTestASM{
  24. /**
  25. *格式化输出信息
  26. *
  27. *@parammes
  28. *@parama
  29. *@paramb
  30. *@return
  31. */
  32. publicstaticStringformatMessage(Stringmes,inta,intb){
  33. Integera1=newInteger(a);
  34. Integerb1=newInteger(b);
  35. returnString.format(mes,newObject[]{a1,b1});
  36. }
  37. /**
  38. *java进行汇编加法
  39. *
  40. *@paramasm
  41. *@parama
  42. *@paramb
  43. *@return
  44. */
  45. publicstaticvoidplus(ASMasm,inta,intb){
  46. asm._MOV_EAX(a);
  47. asm._ADD_EAX(b);
  48. asm._RET();
  49. Stringmes=formatMessage("Java进行汇编加法计算%d+%d=",a,b);
  50. System.out.println(mes+asm.doResult());
  51. }
  52. /**
  53. *java进行汇编减法
  54. *
  55. *@paramasm
  56. *@parama
  57. *@paramb
  58. */
  59. publicstaticvoidminus(ASMasm,inta,intb){
  60. asm._MOV_EAX(a);
  61. asm._SBB_EAX(b);
  62. asm._RET();
  63. Stringmes=formatMessage("Java进行汇编减法计算%d-%d=",a,b);
  64. System.out.println(mes+asm.doResult());
  65. }
  66. /**
  67. *java进行汇编乘法
  68. *
  69. *@paramasm
  70. *@parama
  71. *@paramb
  72. */
  73. publicstaticvoidmultiply(ASMasm,inta,intb){
  74. asm._MOV_EAX(a);
  75. asm._MOV_EBX(b);
  76. asm._IMUL_EAX_EBX();
  77. asm._RET();
  78. Stringmes=formatMessage("Java进行汇编乘法计算%d*%d=",a,b);
  79. System.out.println(mes+asm.doResult());
  80. }
  81. /**
  82. *java进行汇编除法
  83. *
  84. *@paramasm
  85. *@parama
  86. *@paramb
  87. */
  88. publicstaticvoiddivide(ASMasm,inta,intb){
  89. asm._XOR_EDX_EDX();
  90. asm._MOV_EAX(a);
  91. asm._MOV_ECX(b);
  92. asm._IDIV_ECX();
  93. asm._RET();
  94. Stringmes=formatMessage("Java进行汇编除法计算%d/%d=",a,b);
  95. System.out.println(mes+asm.doResult());
  96. }
  97. publicstaticvoidmain(String[]args){
  98. //PS:ASM类中不是所有汇编指令都有,因为太多,有时间再慢慢加.已写的不保证全部正确|||.
  99. //添加ASM类的方法大体有三种:
  100. //1、用汇编指令名和操作码自己一个个对
  101. //2、自己写汇编,然后debug读操作码
  102. //3、开OD之类的反汇编~
  103. //反正所有汇编命令都能实现,关键看有没有恒心,谁有空帮忙写下……
  104. ASMasm=newASM();
  105. //加法测试
  106. plus(asm,65657632,95454157);
  107. //减法测试
  108. minus(asm,996565,12345);
  109. //乘法测试
  110. multiply(asm,1841,2009);
  111. //除法测试
  112. divide(asm,19820901,12);
  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


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值