home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #30 / NN_1992_30.iso / spool / comp / lang / pascal / 7575 < prev    next >
Encoding:
Text File  |  1992-12-21  |  8.8 KB  |  294 lines

  1. Newsgroups: comp.lang.pascal
  2. Path: sparky!uunet!spool.mu.edu!uwm.edu!ux1.cso.uiuc.edu!news.cso.uiuc.edu!s.psych.uiuc.edu!amead
  3. From: amead@s.psych.uiuc.edu (Alan Mead)
  4. Subject: Does this work (was My PD editor strikes back)
  5. Message-ID: <BzHr6x.CBs@news.cso.uiuc.edu>
  6. Sender: usenet@news.cso.uiuc.edu (Net Noise owner)
  7. Organization: UIUC Department of Psychology
  8. Date: Sat, 19 Dec 1992 05:28:08 GMT
  9. Lines: 283
  10.  
  11. A short while ago someone posted about the strange behavior of an
  12. editor to having its object code fiddled with.  I think we all agreed
  13. that the code monitored its own CRC and self-destructed when it found
  14. that it had been modified.
  15.  
  16. Here is a unit that proports to give your code the same level of
  17. protection.  I also included code that modifies itself so as to
  18. retain arbitrary valiable values ACROSS runs.
  19.  
  20. In trying to integrate the two bits of code, I fail to see where the
  21. virus protection code is excluding its CRC from later CRC checks???
  22.  
  23. -alan mead
  24.  
  25. program modifier;
  26. type
  27.    str7 = string[7];
  28.    modtype = record
  29.                TAG : str7;  { TAG is FIRST field!! }
  30.                Val : byte;
  31.                UserData : array[1..100] of byte;
  32.              end;
  33.  
  34. const
  35.   modify : modtype = (TAG:'ILL-INI'; Val:0; UserData:( 0,0,0,0,0,0,0,0,0,0,
  36.                                                        0,0,0,0,0,0,0,0,0,0,
  37.                                                        0,0,0,0,0,0,0,0,0,0,
  38.                                                        0,0,0,0,0,0,0,0,0,0,
  39.                                                        0,0,0,0,0,0,0,0,0,0,
  40.                                                        0,0,0,0,0,0,0,0,0,0,
  41.                                                        0,0,0,0,0,0,0,0,0,0,
  42.                                                        0,0,0,0,0,0,0,0,0,0,
  43.                                                        0,0,0,0,0,0,0,0,0,0,
  44.                                                        0,0,0,0,0,0,0,0,0,0 ));
  45.  
  46. var
  47.   c : longint;
  48.   f : file;
  49.   s : str7;
  50.   i : integer;
  51.   j : byte;
  52.  
  53. begin
  54.  
  55.   writeln('Tag field is now : ',Modify.TAG);
  56.   writeln('Value field is now: ',Modify.Val);
  57.   Modify.Val := Modify.Val + 1;
  58.   writeln('Next time, it will be: ',Modify.Val);
  59.   repeat
  60.     writeln( 'Set any memory location (0 or 1-100)? ' );
  61.     readln( i );
  62.     if ( i>0 ) AND ( i<=100 ) then
  63.       begin
  64.         writeln( 'Location ',i,' currently equals ',
  65.                   Modify.UserData[i],', new value?' );
  66.         readln( j );
  67.         Modify.UserData[i] := j;
  68.       end;
  69.   until i = 0;
  70.  
  71.   assign(f,ParamStr(0)); { open file currently running (will only work under }
  72.   reset(f,1);            { DOS 3+; set buffer size to 1  }
  73.  
  74.   c := filesize(f);      { set c to current file size (because data is }
  75.   c := c - SizeOf(s);    { probably at end of file); subtract off size of s }
  76.   s := '';
  77.  
  78.   {- loop towards front of file looking until match or file starts -}
  79.   While (c > 0) and (s <> Modify.TAG) do
  80.     begin
  81.       Seek(f,c);
  82.       Blockread(f,s,SizeOf(s));
  83.       Dec(c);
  84.     end;
  85.  
  86.   {- seek to the offset of the found string write new record -}
  87.   if c > 0 then
  88.     begin
  89.       Seek(f,c+1);
  90.       BlockWrite(f,Modify,SizeOf(Modify));
  91.     end;
  92.  
  93.   close(f);
  94.  
  95. end.
  96.  
  97.  
  98. unit VirCheck;
  99. interface
  100. uses dos;
  101.  
  102.   { ==================================================================
  103.  
  104.                               Unit: VirCheck v1.0
  105.                             Author: Igal Brener
  106.                                     Solid State Institute and
  107.                                     Physics Department
  108.                                     Technion, Haifa
  109.                                     Israel
  110.                            e-mail.: ssrecib@technion.bitnet
  111.                                     ssrecib@techunix.bitnet
  112.                         July, 1990.
  113.     ==================================================================
  114.   The subroutine CheckVirus is based on the program "AUTOINST.PAS" posted
  115.   to usenet news, group comp.lang.pascal  by David Dubois. For details,
  116.   see his documentation. After compilation, on the first run of your
  117.   program, this subroutine will calculate the checksum of the EXE file
  118.   (passed as a parameter to the subroutine),
  119.   and store it again on disk (on the same EXE file). Next time you will
  120.   run your program, the subroutine will compare the new checksum with the
  121.   one stored, and if there is a discrepancy, you will get a notice.
  122.   The checksum calculation is done excluding the variable InstallBlock
  123.   which retains the checksum, the flag used for detecting which run
  124.   you are doing, and the last date your file was checked.
  125.   In order to make this routine work with big EXE files,
  126.   I have decided to read the EXE file from disk in records of length
  127.   RecSize, a constant you can change in the subroutine FindChecksum.
  128.   I've chosen records of ~10K, since I'm always short of heap space.
  129.   There is no check to see weather the EXE file exists, but this can
  130.   be easily added.
  131.  
  132.           Any comments or problems, use my e-mail address.
  133.  
  134.                               Igal Brener.
  135.  
  136.  
  137.     ==================================================================}
  138. procedure CheckVirus(FileName:string);
  139.  
  140. type
  141.     InstallBlockType = record
  142.                       already : integer;
  143.                       checksum : longint;
  144.                       date_Year, date_Month, date_Day: Word;
  145.                      end;
  146.     Instptr= array[1..12] of byte;
  147.  
  148.  
  149.  
  150. const
  151.     InstallBlock : InstallBlockType
  152.  
  153.               = ( already : 0;
  154.                   checksum : 0;
  155.                    date_Year: 0;
  156.                    date_Month: 0;
  157.                    date_Day: 0);
  158.  
  159. implementation
  160. procedure CheckVirus(FileName:string);
  161.  
  162. var
  163.     c : Longint;
  164.     pInst : ^Instptr;
  165.     i  : integer;
  166.     y,m,d,dow: Word;
  167.  
  168. {*****************************************************************************}
  169. procedure ReInstall;
  170.  
  171. var
  172.     ExeFile    : file;
  173.     HeaderSize: word;
  174.  
  175. begin
  176.     assign    ( ExeFile, FileName );
  177.     reset     ( ExeFile, 1 );
  178.  
  179.     seek      ( ExeFile, 8 );
  180.     blockread ( ExeFile, HeaderSize, sizeof ( HeaderSize ) );
  181.  
  182.     seek      ( ExeFile,   16 * (   longint ( seg ( InstallBlock ) )
  183.                                   - PrefixSeg
  184.                                   + HeaderSize          )
  185.                          + ofs ( InstallBlock )
  186.                          - 256                           );
  187.  
  188.     blockwrite ( ExeFile, InstallBlock, sizeof ( InstallBlock ) );
  189.  
  190.     close      ( ExeFile );
  191. end;
  192.  
  193. {****************************************************************************}
  194. procedure Findchecksum ( var Checksum: Longint);
  195.  
  196. const
  197.     RecSize=10000;
  198.  
  199. type
  200.     barr = array[1..RecSize] of byte;
  201.  
  202. var
  203.     ExeFile : file;
  204.     t : word;
  205.     b : byte;
  206.     fpointer, pSize, bytesleft, i: Longint;
  207.     DirInfo: SearchRec;
  208.     p : ^barr;
  209.  
  210. begin
  211.     assign    ( ExeFile, FileName );
  212.     reset     ( ExeFile, 1 );
  213.     checksum := 0;
  214.     fpointer := 0;
  215.  
  216.     FindFirst(Filename, Archive, DirInfo);
  217.     bytesleft := DirInfo.Size;
  218.  
  219.     { If available heap space is less than RecSize, we don't execute
  220.          this routine}
  221.     if MemAvail>RecSize then
  222.     begin
  223.          while bytesleft>0 do
  224.          begin
  225.               {Here we read RecSize bytes at a time from the EXE file}
  226.               if RecSize<bytesleft then pSize :=RecSize
  227.                    else pSize := bytesleft;
  228.  
  229.               GetMem ( p, pSize);
  230.               seek      ( ExeFile, fpointer );
  231.               blockread ( ExeFile, p^, pSize );
  232.               i := 1;
  233.               while i <= pSize do
  234.               begin
  235.                    checksum := checksum + p^[i];
  236.                    inc(i);
  237.               end;
  238.  
  239.               FreeMem ( p, pSize);
  240.               bytesleft := bytesleft - pSize;
  241.               fpointer := fpointer + pSize;
  242.          end;
  243.  
  244.          close      ( ExeFile );
  245.     end;
  246. end;
  247.  
  248. {****************************************************************************}
  249.  
  250. begin {of CheckVirus}
  251.  
  252.     GetDate(y,m,d,dow);
  253.  
  254.     with InstallBlock do
  255.     begin
  256.          if already=1 then writeln('** Last date your file was verified: ',
  257.               date_Month:2, '/', date_Day:2, '/', date_Year:4);
  258.          Writeln('** Please wait, checking EXE file ... **');
  259.          findchecksum( c );
  260.  
  261.          if already=1 then
  262.          begin
  263.               pInst := @InstallBlock;
  264.               i := 1;
  265.               While i<=Sizeof(InstallBlock) do
  266.               begin
  267.                    c := c - pInst^[i];
  268.                    inc(i);
  269.               end;
  270.               if c<>checksum then
  271.               begin
  272.                    writeln('** Your EXE file has been changed: possible virus!!');
  273.                    Writeln(chr(7), 'Press return ');
  274.                    Readln;
  275.               end;
  276.          end
  277.          else
  278.          begin
  279.               checksum := c;
  280.               already := 1;
  281.          end;
  282.  
  283.          date_Year := y;
  284.          date_Month:= m;
  285.          date_Day  := d;
  286.          { update .EXE file only if checksum is ok, or if this is the first time
  287.          it is run }
  288.          if c=checksum then ReInstall;
  289.     end;
  290. end;
  291.  
  292. end.
  293.  
  294.