home *** CD-ROM | disk | FTP | other *** search
/ Guide To Cracking 2002 / Guide_to_Cracking_2002.iso / Programming / yPP-B-SRC.ZIP / Unit1.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  2000-08-31  |  16.0 KB  |  558 lines

  1. // Coded by yoda(_f2f@gmx.net) of FReAK2FReAK
  2. // You are allowed to use parts of this code by mention of by name !
  3. //
  4. // Have fun :)
  5. //
  6.  
  7. // the loader images
  8. {$R loader.res}
  9.  
  10. unit Unit1;
  11.  
  12. interface
  13.  
  14. uses
  15.   Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  16.   Buttons, StdCtrls, ComCtrls, ExtCtrls,d32lib, Gauges, Menus,hexfun,pelib,
  17.   shellapi,inifiles;
  18.  
  19. type
  20.   TForm1 = class(TForm)
  21.     ListView1: TListView;
  22.     GroupBox1: TGroupBox;
  23.     Edit1: TEdit;
  24.     Panel3: TPanel;
  25.     OpenDialog1: TOpenDialog;
  26.     SaveDialog1: TSaveDialog;
  27.     SpeedButton2: TSpeedButton;
  28.     SpeedButton3: TSpeedButton;
  29.     SpeedButton4: TSpeedButton;
  30.     SpeedButton5: TSpeedButton;
  31.     SpeedButton7: TSpeedButton;
  32.     SpeedButton8: TSpeedButton;
  33.     SpeedButton9: TSpeedButton;
  34.     SpeedButton10: TSpeedButton;
  35.     SpeedButton1: TSpeedButton;
  36.     SpeedButton12: TSpeedButton;
  37.     PopupMenu1: TPopupMenu;
  38.     clearall1: TMenuItem;
  39.     deleteselected1: TMenuItem;
  40.     edit2: TMenuItem;
  41.     N2: TMenuItem;
  42.     Label1: TLabel;
  43.     procedure SpeedButton1Click(Sender: TObject);
  44.     procedure SpeedButton2Click(Sender: TObject);
  45.     procedure SpeedButton3Click(Sender: TObject);
  46.     procedure SpeedButton5Click(Sender: TObject);
  47.     procedure SpeedButton10Click(Sender: TObject);
  48.     procedure SpeedButton7Click(Sender: TObject);
  49.     procedure SpeedButton11Click(Sender: TObject);
  50.     procedure ListView1DblClick(Sender: TObject);
  51.     procedure SpeedButton9Click(Sender: TObject);
  52.     procedure SpeedButton4Click(Sender: TObject);
  53.     procedure SpeedButton8Click(Sender: TObject);
  54.     procedure FormShow(Sender: TObject);
  55.     procedure ListView1Change(Sender: TObject; Item: TListItem;
  56.       Change: TItemChange);
  57.     procedure FormClose(Sender: TObject; var Action: TCloseAction);
  58.     procedure SpeedButton12Click(Sender: TObject);
  59.     procedure edit2Click(Sender: TObject);
  60.     procedure deleteselected1Click(Sender: TObject);
  61.     procedure clearall1Click(Sender: TObject);
  62.     procedure RadioButton1Click(Sender: TObject);
  63.   private
  64.     { Private-Deklarationen }
  65.   public
  66.     { Public-Deklarationen }
  67.   end;
  68.  
  69. const maxpatchbytesnum : integer = 500; // prevents a crash while comparing files
  70.       loadersize_b     : longint = $8000;
  71.       icoOFF_b         : longint = $70a0;
  72.       loadersize_s     : longint = $2c50;
  73.       icoOFF_s         : longint = $28A4;
  74.       nullterm         : dword   = 0;
  75.       waitsign         : dword   = 1;
  76.       resumesign       : dword   = 2;
  77.       s_n_r_sign       : dword   = 3;
  78.       SR_first         : byte    = 0;
  79.       SR_all           : byte    = 1;
  80.       datasectionname  : string  = 'yoda';
  81.       fineicosize      : integer = $2fe;
  82.       inifilename      : string  = 'ypp.ini';
  83.  
  84. var
  85.   Form1          :   TForm1;
  86.   fileh          :   THandle;
  87.   fsize          :   DWORD;
  88.   newitem        :   TListItem;
  89.   loader         :   Tmemorystream;
  90.   loaderdata     :   TMemorystream;
  91.   // Tmemorystream is one of the best Delphi classes, I think !!!
  92.   // if you know whether there exists something like that in C/C++ then please mail me
  93.   checknum       :   real;
  94.   currsel        :   longint;
  95.   i,ii           :   dword;
  96.   tmpnum         :   dword;
  97.   tmpstring      :   string;
  98.   editing        :   boolean;
  99.   theICO         :   Ticon;
  100.   tmpICO         :   Ticon;
  101.   IniFile        :   Tinifile;
  102.   inidir         :   string;
  103.   // PE variables
  104.   dosh                    : PIMAGE_DOS_HEADER;
  105.   peh                     : PIMAGE_NT_HEADERS;
  106.   sectionh                : PIMAGE_SECTION_HEADER;
  107.   sectionh2               : PIMAGE_SECTION_HEADER; // needed for the Offset2va function
  108.  
  109. implementation
  110.  
  111. uses Unit2, Unit3, Unit4, Unit5, Unit6, Unit7, Unit8;
  112. {$R *.DFM}
  113. procedure TForm1.SpeedButton1Click(Sender: TObject);
  114. begin
  115. opendialog1.filter:='Executable Files|*.exe|All files|*.*';
  116. opendialog1.filename:='';
  117. if opendialog1.execute then
  118.   edit1.text:=extractfilename(opendialog1.filename)
  119. end;
  120.  
  121. procedure processedit;
  122. var i : integer;
  123. begin
  124. with form1.listview1 do
  125.   if selcount > 0 then // else we would crash
  126.     begin
  127.     editing:=true;
  128.     i:=selected.index;
  129.     if items[i].caption = 'Patch' then // edit a patch
  130.       begin
  131.       patchform.Edit1.text:=items[i].subitems[0];
  132.       patchform.edit3.text:=items[i].subitems[1];
  133.       patchform.edit2.text:=items[i].subitems[2];
  134.       patchform.show;
  135.       end;
  136.     if items[i].caption[1]= 'W' then //edit a wait
  137.       begin
  138.       tmpnum:=10; // position of the wait number
  139.       while items[i].caption[tmpnum] <> chr($20) do
  140.         begin
  141.         waitform.edit1.text:=waitform.edit1.text+items[i].caption[tmpnum];
  142.         inc(tmpnum);
  143.         end;
  144.       waitform.show;
  145.       end;
  146.     if items[i].caption[1] = 'S' then // edit Search & Replace
  147.       begin
  148.       if items[i].caption[19] = 'A' then srform.checkbox1.checked:=true;
  149.       srform.edit1.text:=items[i].SubItems[1];
  150.       srform.edit2.text:=items[i].subitems[2];
  151.       srform.show;
  152.       end;
  153.     end;
  154. end;
  155.  
  156. function makeloader:boolean;
  157. var hres                    : THandle;
  158.     pres                    : pointer;
  159.     rs,ro,vs,vo             : dword;
  160.     nrs,nro,nvs,nvo         : dword;
  161.     sectionalign            : dword;
  162.     ico                     : TMemoryStream;
  163. begin
  164. result:=false;
  165. loaderdata:=tmemorystream.create;
  166. // process the attack number
  167. tmpnum:=hex2int(optionform.edit1.text);
  168. loaderdata.write(tmpnum,4);
  169. // process the target filename
  170. tmpstring:=form1.edit1.text;
  171. loaderdata.write(pchar(tmpstring)^,succ(length(tmpstring)));
  172. // process all items in the listview
  173. with form1.listview1 do
  174. for i:=0 to items.count-1 do
  175.   begin
  176.   // a patch
  177.   if items[i].Caption = 'Patch' then
  178.     begin
  179.     tmpnum:=hex2int(items[i].subitems[0]);
  180.     loaderdata.write(tmpnum,4);               // the patch VA
  181.     // write the byte size
  182.     tmpnum:=round(length(items[i].subitems[1])/2);
  183.     loaderdata.write(tmpnum,1);
  184.     // original bytes
  185.     tmpstring:='';
  186.     for ii:=1 to length(items[i].subitems[1]) do
  187.       begin
  188.       checknum:=ii/2;
  189.       if frac(checknum) = 0 then
  190.         begin
  191.         tmpstring:=tmpstring+items[i].subitems[1][ii];
  192.         tmpnum:=hex2int(tmpstring);
  193.         loaderdata.write(tmpnum,1);
  194.         tmpstring:='';
  195.         continue;
  196.         end;
  197.       tmpstring:=items[i].subitems[1][ii]+tmpstring;
  198.       end;
  199.     // write the patch bytes
  200.     tmpstring:='';
  201.     for ii:=1 to length(items[i].subitems[2]) do
  202.       begin
  203.       checknum:=ii/2;
  204.       if frac(checknum) = 0 then
  205.         begin
  206.         tmpstring:=tmpstring+items[i].subitems[2][ii];
  207.         tmpnum:=hex2int(tmpstring);
  208.         loaderdata.write(tmpnum,1);
  209.         tmpstring:='';
  210.         continue;
  211.         end;
  212.       tmpstring:=items[i].subitems[2][ii]+tmpstring;
  213.       end;
  214.     end;
  215.   // Search and Replace
  216.   if items[i].caption[1] = 'S' then
  217.     begin
  218.     // write the sign
  219.     loaderdata.write(s_n_r_sign,4);
  220.     // replace the first
  221.     if items[i].caption[19] = 'F' then
  222.       loaderdata.write(SR_first,1)
  223.      else // or all
  224.       loaderdata.write(SR_all,1);
  225.     // write the byte size
  226.     tmpnum:=round(length(items[i].subitems[1])/2);
  227.     loaderdata.write(tmpnum,1);
  228.     // search bytes
  229.     tmpstring:='';
  230.     for ii:=1 to length(items[i].subitems[1]) do
  231.       begin
  232.       checknum:=ii/2;
  233.       if frac(checknum) = 0 then
  234.         begin
  235.         tmpstring:=tmpstring+items[i].subitems[1][ii];
  236.         tmpnum:=hex2int(tmpstring);
  237.         loaderdata.write(tmpnum,1);
  238.         tmpstring:='';
  239.         continue;
  240.         end;
  241.       tmpstring:=items[i].subitems[1][ii]+tmpstring;
  242.       end;
  243.     // write the replace bytes
  244.     tmpstring:='';
  245.     for ii:=1 to length(items[i].subitems[2]) do
  246.       begin
  247.       checknum:=ii/2;
  248.       if frac(checknum) = 0 then
  249.         begin
  250.         tmpstring:=tmpstring+items[i].subitems[2][ii];
  251.         tmpnum:=hex2int(tmpstring);
  252.         loaderdata.write(tmpnum,1);
  253.         tmpstring:='';
  254.         continue;
  255.         end;
  256.       tmpstring:=items[i].subitems[2][ii]+tmpstring;
  257.       end;
  258.     end;
  259.   // WAIT
  260.   if items[i].Caption[1] = 'W' then
  261.     begin
  262.     tmpnum:=10;
  263.     tmpstring:='';
  264.     while items[i].caption[tmpnum] <> chr($20) do
  265.         begin
  266.         tmpstring:=tmpstring+items[i].caption[tmpnum];
  267.         inc(tmpnum);
  268.         end;
  269.     loaderdata.write(waitsign,4);
  270.     tmpnum:=hex2int(tmpstring);
  271.     loaderdata.write(tmpnum,2);
  272.     end;
  273.   //resume
  274.   if items[i].caption = 'Resume the Process' then
  275.     loaderdata.write(resumesign,4);
  276.   end; // of main loop
  277. // write NULL-terminator
  278. loaderdata.write(nullterm,4);
  279. // all patch infos are now in the loaderdata stream
  280.  
  281. // catch the loader image out of the resource ...
  282. if optionform.radiobutton1.checked then
  283.   begin // the small exe image
  284.   hres:=findresource(0,'LOADER_IMAGE_S','BAD_THINGS');
  285.   hres:=loadresource(0,hres);
  286.   pres:=lockresource(hres);
  287.   end
  288.  else
  289.   begin
  290.   hres:=findresource(0,'LOADER_IMAGE_B','BAD_THINGS');
  291.   hres:=loadresource(0,hres);
  292.   pres:=lockresource(hres);
  293.   end;
  294. if pres = nil then
  295.   begin
  296.   mb('Hey !! What did you do with this proggy ?'#13+
  297.      'Where''s my Loader Image ?...aborting !','Arrghhh...',mb_iconerror);
  298.   loaderdata.free;
  299.   exit;
  300.   end;
  301. //...and put it into the loader stream
  302. loader:=tmemorystream.create;
  303. if optionform.radiobutton1.checked then
  304.   loader.write(pres^,loadersize_s)
  305.  else
  306.   loader.write(pres^,loadersize_b);
  307. loader.seek(0,sofromend);
  308. // write a new section into the section header
  309. dosh := PIMAGE_DOS_HEADER(loader.memory);
  310. peh:= PIMAGE_NT_HEADERS(Longword(dosh) + Longword(dosh.e_lfanew));
  311. sectionh:=PIMAGE_SECTION_HEADER(peh);
  312. inc(pimage_nt_headers(sectionh));
  313. // go to the last section..
  314. for i:=1 to peh.FileHeader.NumberOfSections-1 do
  315.   inc(sectionh);
  316. ro:=sectionh.PointerToRawData; //.. and get its infos
  317. rs:=sectionh.SizeOfRawData;
  318. vo:=sectionh.VirtualAddress;
  319. vs:=sectionh.PhysicalAddress;
  320. sectionalign:=peh.OptionalHeader.SectionAlignment;
  321. inc(sectionh); // go to the new section
  322. // some alignment shit (get valid sizes and offsets/addresses which fit to the Alignments)
  323. peh.OptionalHeader.FileAlignment:=$200; // doesn't interested anything...I hope :)
  324. nrs:=((loaderdata.size div $200) + 1) * $200;
  325. tmpnum:=loaderdata.size;
  326. loaderdata.setsize(nrs);
  327. Fillchar((pchar(loaderdata.memory)+tmpnum)^,(loaderdata.size-tmpnum),0); // force Zero's in the data section
  328. nro:=rs+ro;
  329. if (nro mod $200) <> 0 then
  330.   nro:=((nro div $200) +1) * $200; 
  331. nvo:=vs+vo;
  332. if (nvo mod sectionalign) <> 0 then
  333.   nvo:=((nvo div sectionalign) +1) * sectionalign;
  334. nvs:=((nrs div sectionalign) + 1) * sectionalign;
  335. // write the section values of the new section
  336. strcopy(sectionh.Name,pchar(datasectionname));
  337. sectionh.PhysicalAddress:=nvs;
  338. sectionh.VirtualAddress:=nvo;
  339. sectionh.SizeOfRawData:=nrs;
  340. sectionh.PointerToRawData:=nro;
  341. sectionh.Characteristics:=$C0000040;
  342. peh.OptionalHeader.SizeOfImage:=nvo+nvs;
  343. inc(peh.FileHeader.NumberOfSections);
  344. loader.seek(nro,sofrombeginning);
  345. loader.write(loaderdata.memory^,nrs); // write the data at the end of the loader image
  346. // process the loader icon
  347. ico:=tmemorystream.create;
  348. theico.SaveToStream(ico);
  349. if ico.size = fineICOsize then
  350.   begin
  351.   if optionform.radiobutton1.checked then
  352.     loader.seek(icooff_s,soFromBeginning)
  353.    else
  354.     loader.seek(icooff_b,sofrombeginning);
  355.   loader.write((PChar(ico.Memory)+$16)^,ico.Size-$16);
  356.   end
  357.   else mb('The size of the selected icon isn''t supported !'#13+
  358.           'I use the standart icon...',':(',mb_iconwarning);
  359. ico.free;
  360. // save the loader
  361. form1.savedialog1.filename:='loader.exe';
  362. form1.savedialog1.title:='Where to save the generated Loader ?';
  363. if form1.savedialog1.execute then
  364.   begin
  365.   loader.savetofile(form1.savedialog1.filename);
  366.   mb('Loader done - Have fun !',':)',mb_iconinformation+mb_topmost);
  367.   exit;
  368.   end;
  369. // OK OK here's the memory back
  370. loaderdata.free;
  371. loader.free;
  372. result:=true;
  373. end;
  374.  
  375. procedure TForm1.SpeedButton2Click(Sender: TObject);
  376. begin
  377. form1.enabled:=false;
  378. with patchform do
  379.   begin
  380.   edit1.text:='';
  381.   edit2.text:='';
  382.   edit3.text:='';
  383.   show;
  384.   end;
  385. end;
  386.  
  387. procedure TForm1.SpeedButton3Click(Sender: TObject);
  388. begin
  389. // save some settings
  390. with inifile do
  391.   begin
  392.   inifile:=tinifile.create(inidir+inifilename);
  393.   WriteString('LastSettings','attacknumber',optionform.edit1.text);
  394.   if optionform.radiobutton1.checked then
  395.     writestring('LastSettings','smallloader?','1')
  396.    else
  397.     writestring('LastSettings','smallloader?','0');
  398.   writestring('LastSettings','lastdirectory',Getcurrentdir);
  399.   Free;
  400.   end;
  401. close;
  402. end;
  403.  
  404. procedure TForm1.SpeedButton5Click(Sender: TObject);
  405. begin
  406. waitform.show;
  407. end;
  408.  
  409. procedure TForm1.SpeedButton10Click(Sender: TObject);
  410. begin
  411. optionform.show;
  412. end;
  413.  
  414. procedure TForm1.SpeedButton7Click(Sender: TObject);
  415. begin
  416. // check whether there's already a resume
  417. with listview1 do
  418. if items.count > 0 then
  419.   for i:=0 to Items.Count-1 do
  420.     if items[i].caption='Resume the Process' then
  421.       begin
  422.       mb('The Resume command can only be inserted one time !',':(',mb_iconerror);
  423.       exit;
  424.       end;
  425. resumeform.show;
  426. end;
  427.  
  428. procedure TForm1.SpeedButton11Click(Sender: TObject);
  429. begin
  430. processedit;
  431. end;
  432.  
  433. procedure TForm1.ListView1DblClick(Sender: TObject);
  434. begin
  435. processedit;
  436. end;
  437.  
  438. procedure TForm1.SpeedButton9Click(Sender: TObject);
  439. begin
  440. if listview1.items.count = 0 then
  441.   begin
  442.   mb('You have to add something in the list before creating the loader !','!!!',mb_iconerror);
  443.   exit;
  444.   end;
  445. if edit1.text = '' then
  446.   begin
  447.   mb('No filename entered up to now !','!!!',mb_iconerror);
  448.   exit;
  449.   end;
  450. makeloader;
  451. end;
  452.  
  453. procedure TForm1.SpeedButton4Click(Sender: TObject);
  454. begin
  455. aboutform.show;
  456. end;
  457.  
  458. procedure TForm1.SpeedButton8Click(Sender: TObject);
  459. begin
  460. compareform.show;
  461. end;
  462.  
  463. procedure TForm1.FormShow(Sender: TObject);
  464. begin
  465. // disable the popup menu items
  466. edit2.enabled:=false;
  467. deleteselected1.enabled:=false;
  468. clearall1.enabled:=false;
  469.  
  470. // restore some setting
  471. inidir:=GetCurrentdir;
  472. if inidir[length(inidir)] <> '\' then inidir:=inidir+'\';
  473. with inifile do
  474.  begin
  475.  inifile:=tinifile.create(inidir+inifilename);
  476.  setcurrentdirectory(pchar(readstring('LastSettings','lastdirectory','.')));
  477.  if readstring('LastSettings','smallloader?','1') = '1' then
  478.    optionform.radiobutton1.checked:=true
  479.   else
  480.    optionform.radiobutton2.checked:=true;
  481.  optionform.Edit1.text:=readstring('LastSettings','attacknumber','1');
  482.  end;
  483.  
  484. // initialize some variables
  485. currsel:=-1;
  486. editing:=false;
  487.  
  488. // show the icon of the program in the options form
  489. theico:=ticon.create;
  490. tmpico:=ticon.create;
  491. theico.Handle:=ExtractIcon(hInstance,pchar(application.exename),0);
  492. end;
  493.  
  494. procedure TForm1.ListView1Change(Sender: TObject; Item: TListItem;
  495.   Change: TItemChange);
  496. begin
  497. if listview1.SelCount > 0 then
  498.   begin
  499.   if listview1.Selected.Caption[1] <> 'R' then edit2.Enabled:=true;
  500.   deleteselected1.Enabled:=true;
  501.   currsel:=listview1.Selected.Index;
  502.   end
  503.  else
  504.   begin
  505.   deleteselected1.Enabled:=false;
  506.   edit2.enabled:=false;
  507.   currsel:=-1;
  508.   end;
  509. if listview1.Items.Count = 0 then
  510.   clearall1.Enabled:=false
  511.   else
  512.   clearall1.Enabled:=true;
  513. end;
  514.  
  515. procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
  516. begin
  517. // clean the icon shit up
  518. theico.free;
  519. tmpico.free;
  520. end;
  521.  
  522. procedure TForm1.SpeedButton12Click(Sender: TObject);
  523. begin
  524. srform.show;
  525. end;
  526.  
  527. procedure TForm1.edit2Click(Sender: TObject);
  528. begin
  529. processedit;
  530. end;
  531.  
  532. procedure TForm1.deleteselected1Click(Sender: TObject);
  533. begin
  534. if listview1.SelCount > 0 then
  535.   with listview1 do
  536.     for i:=0 to Items.Count-1 do
  537.       if items[i].Selected then    // delete the selected item
  538.         begin
  539.         items[i].Delete;
  540.         exit;
  541.         end
  542.         else begin;end
  543. end;
  544.  
  545. procedure TForm1.clearall1Click(Sender: TObject);
  546. begin
  547. if messagebox(0,'Are you sure that you want to clear the Action list ?',
  548.   'agreement',mb_iconquestion+mb_topmost+mb_yesno) = ID_YES then
  549.   listview1.Items.Clear;
  550. end;
  551.  
  552. procedure TForm1.RadioButton1Click(Sender: TObject);
  553. begin
  554. speedbutton1.enabled:=true;
  555. end;
  556.  
  557. end.
  558.