Advertisement
Guest User

Untitled

a guest
Nov 22nd, 2011
4,031
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.10 KB | None | 0 0
  1. mem=[0x31, 0x04, 0x33, 0xaa, 0x40, 0x02, 0x80, 0x03, 0x52, 0x00, 0x72, 0x01, 0x73, 0x01, 0xb2, 0x50,0x30, 0x14, 0xc0, 0x01, 0x80, 0x00, 0x10, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00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, 0x45, 0x5e, 0x09, 0x74, 0x5c, 0x3f, 0x29, 0x2b, 0x66, 0x3d, 0x0d,0x02, 0x30, 0x28, 0x35, 0x15, 0x09, 0x15, 0xdd, 0xec, 0xb8, 0xe2, 0xfb, 0xd8, 0xcb, 0xd8, 0xd1,0x8b, 0xd5, 0x82, 0xd9, 0x9a, 0xf1, 0x92, 0xab, 0xe8, 0xa6, 0xd6, 0xd0, 0x8c, 0xaa, 0xd2, 0x94,0xcf, 0x45, 0x46, 0x67, 0x20, 0x7d, 0x44, 0x14, 0x6b, 0x45, 0x6d, 0x54, 0x03, 0x17, 0x60, 0x62,0x55, 0x5a, 0x4a, 0x66, 0x61, 0x11, 0x57, 0x68, 0x75, 0x05, 0x62, 0x36, 0x7d, 0x02, 0x10, 0x4b,0x08, 0x22, 0x42, 0x32, 0xba, 0xe2, 0xb9, 0xe2, 0xd6, 0xb9, 0xff, 0xc3, 0xe9, 0x8a, 0x8f, 0xc1,0x8f, 0xe1, 0xb8, 0xa4, 0x96, 0xf1, 0x8f, 0x81, 0xb1, 0x8d, 0x89, 0xcc, 0xd4, 0x78, 0x76, 0x61,0x72, 0x3e, 0x37, 0x23, 0x56, 0x73, 0x71, 0x79, 0x63, 0x7c, 0x08, 0x11, 0x20, 0x69, 0x7a, 0x14,0x68, 0x05, 0x21, 0x1e, 0x32, 0x27, 0x59, 0xb7, 0xcf, 0xab, 0xdd, 0xd5, 0xcc, 0x97, 0x93, 0xf2,0xe7, 0xc0, 0xeb, 0xff, 0xe9, 0xa3, 0xbf, 0xa1, 0xab, 0x8b, 0xbb, 0x9e, 0x9e, 0x8c, 0xa0, 0xc1,0x9b, 0x5a, 0x2f, 0x2f, 0x4e, 0x4e, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]
  2. vm_instructions= ["jmp", "movr", "movm", "add", "xor", "cmp", "jmpe", "hlt"]
  3. vm_segment_size=0x10
  4. cs=4
  5. ds=5
  6. def ModDetect(opcode):
  7. if (opcode&0x10)==0:
  8. return False
  9. return True
  10. def GetOpcode(opcode):
  11. return (opcode>>5)
  12. def GetOperand1(opcode):
  13. return (opcode&0x0F)
  14. ########################################################################
  15. class _VM_CPU:
  16. vm_ip=0
  17. vm_reg=[0 for i in range(6)]
  18. vm_flag=0
  19. class VirtualMachine:
  20. vm_cpu=_VM_CPU()
  21. mem=[]
  22. def __init__(self,mem):
  23. self.mem=mem
  24. self.vm_cpu.vm_reg[ds]=0x10
  25. while 1:
  26. self.execute(self.vm_cpu.vm_ip)
  27.  
  28. def execute(self,ip):
  29. if (ip>(len(self.mem)-1)):
  30. return False
  31. opcode=self.mem[ip]
  32. vm_opcode=GetOpcode(opcode)
  33. vm_mod=ModDetect(opcode)
  34. vm_operand1=GetOperand1(opcode)
  35. #if vm_mod:
  36. #vm_operand2=self.mem[ip+1]
  37. vm_operand2=self.mem[ip+1]
  38. self.vm_cpu.vm_ip+=2
  39. if vm_opcode==0:
  40. if vm_mod:
  41. if vm_operand2>=0x10:
  42. self.vm_cpu.vm_reg[cs]=0x10
  43. print "%X %s r%d,%X" % (self.vm_cpu.vm_ip,vm_instructions[vm_opcode],vm_operand1,vm_operand2)
  44. self.vm_cpu.vm_ip=self.vm_cpu.vm_reg[cs]*0x10+(vm_operand2-self.vm_cpu.vm_reg[cs])*0x10+self.vm_cpu.vm_reg[vm_operand1]
  45. else:
  46. print "%X %s r%d" % (self.vm_cpu.vm_ip,vm_instructions[vm_opcode],vm_operand1)
  47. self.vm_cpu.vm_ip=vm_cpu.vm_reg[cs]*0x10+vm_cpu.vm_reg[vm_operand1]
  48. elif vm_opcode==1:
  49. if vm_mod:
  50. print "%X %s r%d,%X" % (self.vm_cpu.vm_ip,vm_instructions[vm_opcode],vm_operand1,vm_operand2)
  51. self.vm_cpu.vm_reg[vm_operand1]=vm_operand2
  52. else:
  53. print "%X %s r%d,r%d" % (opcode,vm_instructions[vm_opcode],vm_operand1,vm_operand2)
  54. self.vm_cpu.vm_reg[vm_operand1]=self.vm_cpu.vm_reg[vm_operand1]
  55. elif vm_opcode==2:
  56. if vm_mod==False:
  57. print "%X %s r%d,[ds:r%d]" % (self.vm_cpu.vm_ip,vm_instructions[vm_opcode],vm_operand1,vm_operand2)
  58. self.vm_cpu.vm_reg[vm_operand1]=self.mem[self.vm_cpu.vm_reg[ds]*0x10+self.vm_cpu.vm_reg[vm_operand2]]
  59. else:
  60. print "%X %s [ds:r%d],r%d" % (self.vm_cpu.vm_ip,vm_instructions[vm_opcode],vm_operand1,vm_operand2)
  61. self.mem[self.vm_cpu.vm_reg[ds]*0x10+self.vm_cpu.vm_reg[vm_operand1]]=self.vm_cpu.vm_reg[vm_operand2]
  62. elif vm_opcode==3:
  63. if vm_mod:
  64. print "%X %s r%d,%X" % (self.vm_cpu.vm_ip,vm_instructions[vm_opcode],vm_operand1,vm_operand2)
  65. self.vm_cpu.vm_reg[vm_operand1]+=vm_operand2
  66. else:
  67. print "%X %s r%d,r%d" % (self.vm_cpu.vm_ip,vm_instructions[vm_opcode],vm_operand1,vm_operand2)
  68. self.vm_cpu.vm_reg[vm_operand1]+=self.vm_cpu.vm_reg[vm_operand2]
  69. elif vm_opcode==4:
  70. if vm_mod:
  71. print "%X %s r%d,%X" % (self.vm_cpu.vm_ip,vm_instructions[vm_opcode],vm_operand1,vm_operand2)
  72. self.vm_cpu.vm_reg[vm_operand1]^=vm_operand2
  73. else:
  74. print "%X %s r%d,r%d" % (opcode,vm_instructions[vm_opcode],vm_operand1,vm_operand2)
  75. self.vm_cpu.vm_reg[vm_operand1]^=self.vm_cpu.vm_reg[vm_operand2]
  76. elif vm_opcode==5:
  77. if vm_mod:
  78. print "%X %s r%d,%X" % (self.vm_cpu.vm_ip,vm_instructions[vm_opcode],vm_operand1,vm_operand2)
  79. if(self.vm_cpu.vm_reg[vm_operand1]==vm_operand2):
  80. self.vm_cpu.vm_flag=0
  81. elif(self.vm_cpu.vm_reg[vm_operand1]>vm_operand2):
  82. self.vm_cpu.vm_flag=1
  83. else:
  84. self.vm_cpu.vm_flag=0xff
  85. else:
  86. print "%X %s r%d,r%d" % (self.vm_cpu.vm_ip,vm_instructions[vm_opcode],vm_operand1,vm_operand2)
  87. if(self.vm_cpu.vm_reg[vm_operand1]==self.vm_cpu.vm_reg[vm_operand2]):
  88. self.vm_cpu.vm_flag=0
  89. elif(self.vm_cpu.vm_reg[vm_operand1]>self.vm_cpu.vm_reg[vm_operand2]):
  90. self.vm_cpu.vm_flag=1
  91. else:
  92. self.vm_cpu.vm_flag=0xff
  93. elif vm_opcode==6:
  94. if vm_mod:
  95. print "%X %s r%d,%X" % (self.vm_cpu.vm_ip,vm_instructions[vm_opcode],vm_operand1,vm_operand2)
  96. if self.vm_cpu.vm_flag==0:
  97. #far jump
  98. if vm_operand2>=0x10:
  99. self.vm_cpu.vm_reg[cs]=0x10
  100. self.vm_cpu.vm_ip=vm_cpu.vm_reg[cs]*0x10+(vm_operand2-vm_cpu.vm_reg[cs])*0x10+vm_cpu.vm_reg[vm_operand1]
  101. else:
  102. print "%X %s r%d" % (self.vm_cpu.vm_ip,vm_instructions[vm_opcode],vm_operand1)
  103. if self.vm_cpu.vm_flag==0:
  104. self.vm_cpu.vm_ip=self.vm_cpu.vm_reg[cs]*0x10+self.vm_cpu.vm_reg[vm_operand1]
  105. #len 1
  106. self.vm_cpu.vm_ip-=1
  107. else:
  108. for i in len(self.mem):
  109. print "%c" % (mem[i])
  110. exit()
  111. print "ip:%X" %(self.vm_cpu.vm_ip)
  112. print "flag:%X" % (self.vm_cpu.vm_flag)
  113. print "r0:%X" % (self.vm_cpu.vm_reg[0])
  114. print "r1:%X" % (self.vm_cpu.vm_reg[1])
  115. print "r2:%X" % (self.vm_cpu.vm_reg[2])
  116. print "r3:%X" % (self.vm_cpu.vm_reg[3])
  117. print "cs:%X" % (self.vm_cpu.vm_reg[cs])
  118. print "ds:%X" % (self.vm_cpu.vm_reg[ds])
  119. return True
  120. vm=VirtualMachine(mem)
  121.  
  122.  
  123.  
  124.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement