Guest User

Untitled

a guest
Jan 23rd, 2018
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. format PE console 4.0
  2. include 'win32a.inc'
  3. jmp a0
  4. data import
  5. ;krnlname:
  6.  library kernel32,'KERNEL32.DLL'
  7.  import kernel32,\
  8.     CloseHandle,'CloseHandle',\
  9.     CreateFile,'CreateFileA',\
  10.     FreeEnvironmentStrings,'FreeEnvironmentStringsA',\
  11.     FreeLibrary,'FreeLibrary',\
  12.     GetCurrentProcessId,'GetCurrentProcessId',\
  13.     GetCurrentThreadId,'GetCurrentThreadId',\
  14.     GetDiskFreeSpaceEx,'GetDiskFreeSpaceExA',\
  15.     GetEnvironmentStrings,'GetEnvironmentStringsA',\
  16.     GetProcAddress,'GetProcAddress',\
  17.     GetStdHandle,'GetStdHandle',\
  18.     GetSystemTime,'GetSystemTime',\
  19.     GetSystemTimeAsFileTime,'GetSystemTimeAsFileTime',\
  20.     GetTickCount,'GetTickCount',\
  21.     GlobalMemoryStatus,'GlobalMemoryStatus',\
  22.     LoadLibrary,'LoadLibraryA',\
  23.     ReadFile,'ReadFile',\
  24.     VirtualAlloc,'VirtualAlloc',\
  25.     VirtualFree,'VirtualFree',\
  26.     WriteFile,'WriteFile',\
  27.     ExitProcess,'ExitProcess'
  28. end data
  29.  
  30. RandSeed    db 20 dup 0
  31. Rand_SysData    db 64 dup 0
  32. Rand_Context    db 104 dup 0
  33. ARC_Context db 258 dup 0
  34. Rand_KRNL   db 'KERNEL32.DLL',0
  35. ;krnladdr        dd krnlname
  36. Rand_GMSE   db 'GlobalMemoryStatusEx',0
  37. Rand_HMOD   dd 0
  38. stdout      dd 0
  39. bittable    dd 0
  40. tmptable    dd 0
  41. blocksize   dw 4 ; VERY IMPORTANT - must be divisable by 4
  42. sblocksize  dd 0
  43. powermod    db $01,$AB,$CD,$B7,$39,$A3,$C5,$EF,$F1,$1B,$3D,$A7,$29,$13,$35,$DF ;PowerMod[i*2+1,-1,256]
  44.         db $E1,$8B,$AD,$97,$19,$83,$A5,$CF,$D1,$FB,$1D,$87,$09,$F3,$15,$BF ; i.e. if we want to calculate PowerMod[x,-1,256] where x is odd number between 1 and 255,
  45.         db $C1,$6B,$8D,$77,$F9,$63,$85,$AF,$B1,$DB,$FD,$67,$E9,$D3,$F5,$9F ; shr eax,1
  46.         db $A1,$4B,$6D,$57,$D9,$43,$65,$8F,$91,$BB,$DD,$47,$C9,$B3,$D5,$7F ; mov eax,[powermod+eax]
  47.         db $81,$2B,$4D,$37,$B9,$23,$45,$6F,$71,$9B,$BD,$27,$A9,$93,$B5,$5F
  48.         db $61,$0B,$2D,$17,$99,$03,$25,$4F,$51,$7B,$9D,$07,$89,$73,$95,$3F
  49.         db $41,$EB,$0D,$F7,$79,$E3,$05,$2F,$31,$5B,$7D,$E7,$69,$53,$75,$1F
  50.         db $21,$CB,$ED,$D7,$59,$C3,$E5,$0F,$11,$3B,$5D,$C7,$49,$33,$55,$FF
  51. memerr      db 'Memory allocation error',13,10
  52. memerrl = $-memerr
  53. forwardname db 'forward.bin',0
  54. backwardname    db 'backward.bin',0
  55. ;here subs
  56. noconst = 1
  57. include 'sha1.inc'
  58. include 'ISCrypt.inc'
  59.  
  60. a0:
  61.     mov ax,[blocksize]
  62.     mul ax
  63.     xchg    dx,ax
  64.     shl eax,16
  65.     mov ax,dx
  66.     mov [sblocksize],eax
  67.     stdcall SHA1Reset,Rand_Context
  68.     invoke  GetCurrentProcessId
  69.     mov dword[Rand_SysData],eax
  70.     stdcall SHA1Input,Rand_Context,Rand_SysData,4
  71.     invoke  GetCurrentThreadId
  72.     mov dword[Rand_SysData],eax
  73.     stdcall SHA1Input,Rand_Context,Rand_SysData,4
  74.     invoke  GetDiskFreeSpaceEx,0,Rand_SysData,Rand_SysData+8,Rand_SysData+16
  75.     stdcall SHA1Input,Rand_Context,Rand_SysData,24
  76.     invoke  GetEnvironmentStrings
  77.     mov dword[Rand_SysData],eax
  78.     Rand_for1begin:
  79.     cmp byte[eax],0
  80.     jz  Rand_for1end
  81.     @@:
  82.     cmp byte[eax],0
  83.     jz  @f
  84.     inc eax
  85.     jmp @b
  86.     @@:
  87.     inc eax
  88.     jmp Rand_for1begin
  89.     Rand_for1end:
  90.     sub eax,dword[Rand_SysData]
  91.     stdcall SHA1Input,Rand_Context,dword[Rand_SysData],eax
  92.     invoke  FreeEnvironmentStrings,dword[Rand_SysData]
  93.     invoke  GetSystemTime,Rand_SysData
  94.     stdcall SHA1Input,Rand_Context,Rand_SysData,16
  95.     invoke  GetSystemTimeAsFileTime,Rand_SysData
  96.     stdcall SHA1Input,Rand_Context,Rand_SysData,8
  97.     invoke  GetTickCount
  98.     mov dword[Rand_SysData],eax
  99.     stdcall SHA1Input,Rand_Context,Rand_SysData,4
  100.     invoke  LoadLibrary,Rand_KRNL
  101.     ;invoke  LoadLibrary,krnlname+$28
  102.     or  eax,eax
  103.     jz  Rand_nolib
  104.     mov [Rand_HMOD],eax
  105.     invoke  GetProcAddress,[Rand_HMOD],Rand_GMSE
  106.     or  eax,eax
  107.     jz  Rand_noproc
  108.     mov dword[Rand_SysData],64
  109.     stdcall eax,Rand_SysData
  110.     stdcall SHA1Input,Rand_Context,Rand_SysData,64
  111.     jmp @f
  112.     Rand_noproc:
  113.     invoke  FreeLibrary,[Rand_HMOD]
  114.     Rand_nolib:
  115.     mov dword[Rand_SysData],32
  116.     invoke  GlobalMemoryStatus,Rand_SysData
  117.     stdcall SHA1Input,Rand_Context,Rand_SysData,32
  118. @@:
  119.     stdcall SHA1Result,Rand_Context,RandSeed
  120.     stdcall ArcFourInit,ARC_Context,RandSeed,20
  121.     stdcall ArcFourCrypt,ARC_Context,0,0,1000
  122.     ; now RandSeed is initialised
  123.     invoke  GetStdHandle,$FFFFFFF5
  124.     mov [stdout],eax
  125.  
  126.     invoke  VirtualAlloc,0,[sblocksize],MEM_COMMIT,PAGE_READWRITE
  127.     mov [bittable],eax
  128.     or  eax,eax
  129.     jnz @f
  130. pmemerr:
  131.     invoke  WriteFile,[stdout],memerr,memerrl,RandSeed,0
  132.     jmp exit
  133. @@:
  134.     invoke  VirtualAlloc,0,[sblocksize],MEM_COMMIT,PAGE_READWRITE
  135.     mov [tmptable],eax
  136.     or  eax,eax
  137.     jz  pmemerr
  138.  
  139.     ; now memory is allocated
  140.     cld
  141.     mov ebp,[tmptable]
  142.     movzx   edx,[blocksize]
  143. mainloop:   ;ok=ebx, n=edx, i=esi, j=edi,
  144.     mov ecx,[sblocksize]
  145.     stdcall ArcFourCrypt,ARC_Context,0,[bittable],ecx   ;bittable = Table[RandomInteger[], {i, n}, {j, n}];
  146.     ; now matrix is filled, check - it's det must be odd
  147.     shr ecx,2
  148.     mov esi,[bittable]
  149.     mov edi,ebp
  150.     rep movsd   ;tmptable = bittable;
  151.     xor ebx,ebx
  152.     inc ebx ;ok = True;
  153.     xor esi,esi ;i = 1; because 0..n-1
  154. lab2:
  155.     lea eax,[edx-1] ; because 0..n-1
  156.     cmp esi,eax
  157.     je  lab3    ;If[i == n, Goto[lab3]];
  158.     mov ecx,edx
  159.     mov eax,esi
  160.     mul cx
  161.     xchg    dx,ax
  162.     shl eax,16
  163.     mov ax,dx
  164.     mov edx,ecx
  165.     add eax,ebp
  166.     mov al,byte[eax+esi]
  167.     bt  eax,0
  168.     jc  lab4    ;If[tmptable[[i, i]] != 0, Goto[lab4]];
  169.     xor ebx,ebx ;ok = False;
  170.     mov edi,esi
  171.     inc edi ;j = i + 1;
  172. lab6:
  173.     cmp edi,edx
  174.     je  lab5    ;If[j > n, Goto[lab5]];
  175.     mov ecx,edx
  176.     mov eax,edi
  177.     mul cx
  178.     xchg    dx,ax
  179.     shl eax,16
  180.     mov ax,dx
  181.     mov edx,ecx
  182.     add eax,ebp
  183.     mov al,byte[eax+esi]
  184.     bt  eax,0
  185.     jnc lab7    ;If[tmptable[[j, i]] != 1, Goto[lab7]];
  186.     push    esi
  187.     push    edi
  188.     mov ecx,edx
  189.     mov eax,esi
  190.     mul cx
  191.     xchg    dx,ax
  192.     shl eax,16
  193.     mov ax,dx
  194.     mov esi,eax ;now esi=i*n
  195.     mov eax,edi
  196.     mul cx
  197.     xchg    dx,ax
  198.     shl eax,16
  199.     mov ax,dx
  200.     mov edi,eax ;now edi=j*n
  201.     mov edx,ecx
  202.     shr edx,2
  203.     xor ecx,ecx
  204. @@:
  205.     xchg    eax,[ebp+esi]
  206.     xchg    eax,[ebp+edi]
  207.     xchg    eax,[ebp+esi]
  208.     inc ecx
  209.     add ebp,4
  210.     cmp ecx,edx
  211.     jne @b
  212.     mov ebp,[tmptable]
  213.     movzx   edx,[blocksize]
  214.     pop edi
  215.     pop esi  ;tmpline = tmptable[[i]];tmptable[[i]] = tmptable[[j]];tmptable[[j]] = tmpline;
  216.     xor ebx,ebx
  217.     inc ebx ;ok = True;
  218.     jmp lab5
  219. lab7:
  220.     inc edi ;j++;
  221.     jmp lab6
  222. lab5:
  223.     or  ebx,ebx
  224.     jz  lab3    ;If[! ok, Goto[lab3]];
  225. lab4:
  226.     mov edi,esi
  227.     inc edi ;j = i + 1;
  228. lab8:
  229.     cmp edi,edx
  230.     je  lab9    ;If[j > n, Goto[lab9]];
  231.     mov ecx,edx
  232.     mov eax,edi
  233.     mul cx
  234.     xchg    dx,ax
  235.     shl eax,16
  236.     mov ax,dx
  237.     mov edx,ecx
  238.     add eax,ebp
  239.     mov al,byte[eax+esi]
  240.     bt  eax,0
  241.     jnc lab10    ;If[tmptable[[j, i]] != 1, Goto[lab10]];
  242.     push    esi
  243.     push    edi
  244.     mov ecx,edx
  245.     mov eax,esi
  246.     mul cx
  247.     xchg    dx,ax
  248.     shl eax,16
  249.     mov ax,dx
  250.     mov esi,eax ;now esi=i*n
  251.     mov eax,edi
  252.     mul cx
  253.     xchg    dx,ax
  254.     shl eax,16
  255.     mov ax,dx
  256.     mov edi,eax ;now edi=j*n
  257.     mov edx,ecx
  258.     shr edx,2
  259.     xor ecx,ecx
  260. @@:
  261.     mov eax,[edi+ebp]
  262.     xor eax,[esi+ebp]
  263.     mov [edi+ebp],eax
  264.     inc ecx
  265.     add ebp,4
  266.     cmp ecx,edx
  267.     jne @b
  268.     mov ebp,[tmptable]
  269.     movzx   edx,[blocksize]
  270.     pop edi
  271.     pop esi  ;tmptable[[j]] = BitXor[tmptable[[i]], tmptable[[j]]];
  272. lab10:
  273.     inc edi ;j++;
  274.     jmp lab8
  275. lab9:
  276.     inc esi ;i++;
  277.     jmp lab2
  278. lab3:
  279.     or  ebx,ebx
  280.     jz  mainloop    ;If[! ok, Goto[lab1]];
  281.     mov eax,[sblocksize]
  282.     dec eax
  283.     movzx   eax,byte[ebp+eax]
  284.     bt  eax,0
  285.     jnc mainloop
  286.     ;now we must check bittable for zeros and if there any, change them to random even number
  287.     mov esi,[bittable]
  288.     xor ecx,ecx
  289.     mov edx,[sblocksize]
  290. dezero_loop:
  291.     movzx   eax,byte[esi]
  292.     or  eax,eax
  293.     jnz @f
  294.     stdcall ArcFourCrypt,ARC_Context,eax,esi,1
  295.     and byte[esi],$FE
  296.     jmp dezero_loop
  297. @@:
  298.     inc esi
  299.     inc ecx
  300.     cmp ecx,edx
  301.     jnz dezero_loop
  302.     invoke  CreateFile,forwardname,GENERIC_WRITE,0,NULL,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL
  303.     mov [Rand_HMOD],eax
  304.     invoke  WriteFile,eax,[bittable],[sblocksize],RandSeed,NULL
  305.     invoke  CloseHandle,[Rand_HMOD]
  306.     ;calculate inverse matrix
  307.     ;bittable contains keytable, there will be identity matrix
  308.     ;tmptable will contain inverse matrix
  309.     mov ecx,[sblocksize]
  310.     xor eax,eax
  311.     mov edi,[tmptable]
  312.     rep stosd
  313.  
  314.  
  315.  
  316.     invoke  CreateFile,backwardname,GENERIC_WRITE,0,NULL,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL
  317.     mov [Rand_HMOD],eax
  318.     invoke  WriteFile,eax,[tmptable],[sblocksize],RandSeed,NULL
  319.     invoke  CloseHandle,[Rand_HMOD]
  320. exit:   invoke  ExitProcess,0
Add Comment
Please, Sign In to add comment