a self-checking program in standard C

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • RoSsIaCrIiLoIA

    a self-checking program in standard C

    Do you know how to write a self-checking program in standard C?
    Do I can think that if I write in a file.c
    static g[100]="1234567";
    in the file.exe (or file) there is in some place
    1234567'\0''\0' '\0''\0''\0''\0 ''\0'...'\0'

    This is my first attempt:

    /* file.c -> file.exe */
    #include <stdio.h>
    #include <string.h>

    char* estr(char* nome)
    {
    char *a = nome, *b;
    char c;
    if(nome == 0)
    return 0;
    while( 1 )
    {b = a;
    while( (c = *a) && c != '\\' && c != '/' )
    ++a;
    if(c == 0) break;
    ++a;
    }
    return b;
    }

    int main(int c, char** argv)
    {
    (void) c;
    printf("I'm a self-checking program\n");
    if(strncmp("dc. exe", estr(argv[0]), 6) != 0)
    {printf("File corrupted\n"); return 0;}
    printf("Continu e\n");
    return 0;
    }

  • Victor Nazarov

    #2
    [OT] Re: a self-checking program in standard C

    RoSsIaCrIiLoIA wrote:[color=blue]
    > Do you know how to write a self-checking program in standard C?
    > Do I can think that if I write in a file.c
    > static g[100]="1234567";
    > in the file.exe (or file) there is in some place
    > 1234567'\0''\0' '\0''\0''\0''\0 ''\0'...'\0'
    >[/color]

    I think you can. But it is out of Standard C scope.

    [color=blue]
    > This is my first attempt:
    >
    > /* file.c -> file.exe */
    > #include <stdio.h>
    > #include <string.h>
    >
    > char* estr(char* nome)
    > {
    > char *a = nome, *b;
    > char c;
    > if(nome == 0)
    > return 0;
    > while( 1 )
    > {b = a;
    > while( (c = *a) && c != '\\' && c != '/' )[/color]

    what about c != '\0'
    [color=blue]
    > ++a;
    > if(c == 0) break;
    > ++a;
    > }
    > return b;
    > }
    >
    > int main(int c, char** argv)
    > {
    > (void) c;
    > printf("I'm a self-checking program\n");
    > if(strncmp("dc. exe", estr(argv[0]), 6) != 0)
    > {printf("File corrupted\n"); return 0;}[/color]

    Why do you think this garanties that file is corrupted? Why user
    shouldn't rename files? If he shouldn't then you must consider that file
    may had more than one name in some environments (hard and symbolic links).
    [color=blue]
    > printf("Continu e\n");
    > return 0;
    > }
    >[/color]

    How does this code relate with your proposal made in the begining.

    Anyway all of this is offtopic here.

    vir

    Comment

    • RoSsIaCrIiLoIA

      #3
      Re: [OT] Re: a self-checking program in standard C

      On Wed, 14 Apr 2004 15:26:47 +0400, Victor Nazarov <vviruss@mail.r u>
      wrote:
      [color=blue]
      >RoSsIaCrIiLo IA wrote:[color=green]
      >> Do you know how to write a self-checking program in standard C?
      >> Do I can think that if I write in a file.c
      >> static g[100]="1234567";
      >> in the file.exe (or file) there is in some place
      >> 1234567'\0''\0' '\0''\0''\0''\0 ''\0'...'\0'
      >>[/color]
      >
      >I think you can. But it is out of Standard C scope.[/color]

      But are there any system where this is not true?
      [color=blue]
      >
      >[color=green]
      >> This is my first attempt:
      >>
      >> /* file.c -> file.exe */
      >> #include <stdio.h>
      >> #include <string.h>
      >>
      >> char* estr(char* nome)
      >> {
      >> char *a = nome, *b;
      >> char c;
      >> if(nome == 0)
      >> return 0;
      >> while( 1 )
      >> {b = a;
      >> while( (c = *a) && c != '\\' && c != '/' )[/color]
      >
      >what about c != '\0'[/color]

      ok (c = *a)!=0
      [color=blue][color=green]
      >> ++a;
      >> if(c == 0) break;
      >> ++a;
      >> }
      >> return b;
      >> }
      >>
      >> int main(int c, char** argv)
      >> {
      >> (void) c;
      >> printf("I'm a self-checking program\n");
      >> if(strncmp("dc. exe", estr(argv[0]), 6) != 0)
      >> {printf("File corrupted\n"); return 0;}[/color]
      >
      >Why do you think this garanties that file is corrupted? Why user
      >shouldn't rename files? If he shouldn't then you must consider that file
      >may had more than one name in some environments (hard and symbolic links).[/color]

      I'm thinking some of this type:

      /* my1.c */
      #include <string.h>
      #include <stdio.h>
      #include <ctype.h>
      #include <stdlib.h>
      #include <assert.h>
      #include <limits.h>
      #define UNS unsigned

      char* estr(char* nome)
      {
      char *a = nome, *b;
      char c;
      if(nome == 0)
      return 0;
      while( 1 )
      {b = a;
      while( (c = *a)!=0 && c!='\\' && c!='/' )
      ++a;
      if(c == 0) break;
      ++a;
      }
      return b;
      }


      int info_p(char* );

      int main(int c, char** argv)
      {
      if(c != 2)
      {
      if( c==0 || !argv[0] )
      printf("Use:> this_prog prog\n");
      else printf("Use:> %s prog[.exe]\n", estr(argv[0]));

      return 0;
      }
      if( info_p( argv[1] )==0 )
      { printf("Not signed %s\n", estr(argv[1])); return 0;}

      printf("Ok signed %s\n", estr(argv[1]));
      return 0;
      }

      void printu(char* a)
      {
      int i;
      for( i = 0; i < sizeof(UNS); ++i)
      printf(" %d ", (int) a[i]);
      }


      int f(const char* a, FILE* fp)
      {
      int i, j;
      for( i= 0, j= EOF + 1 ; i < sizeof(UNS) && j!=EOF; ++i)
      j= fputc(a[i], fp);
      return j;
      }

      int info_p(char* as)
      {int c, r, k, cp;
      FILE *fp;
      unsigned count= 0, rox= 0, sum= 0, *p1, buffer= 0;
      char acount[ sizeof(UNS) + 8 ] = {0},
      arox [ sizeof(UNS) + 8 ] = {0},
      asum [ sizeof(UNS) + 8 ] = {0};
      char *pc1 = acount, *pc2 = arox, *pc3 = asum;
      long ove;

      if( (fp = fopen( as, "r+b"))==NU LL )
      {printf("Proble mi di apertura\n");
      return 0;
      }
      r = 0; cp = 0;
      label:
      while( (c = fgetc(fp))!=EOF && !(cp=='1' && c=='2') )
      {
      sum += c; ++count;
      cp = c;
      buffer = (buffer << CHAR_BIT) | ((UNS)(unsigned char) c);
      if(count % sizeof(UNS) ==0)
      rox = rox ^ buffer;
      }
      if( c!=EOF && r==0 )
      {k = 2;
      while( (c = fgetc(fp))!=EOF && c-'0'== ++k )
      {sum += c; ++count;
      cp = c;
      buffer = (buffer << CHAR_BIT) | (UNS)((unsigned char) c);
      if(count % sizeof(UNS) == 0)
      rox = rox ^ buffer;
      } /*34567*/
      if( k==8 && c!=EOF )
      {
      if((ove = ftell( fp ))== -1)
      { printf("Error\n "); exit(0); }
      printf("Find\n" );
      r=1;
      }
      else if(c != EOF) goto label;
      else goto fine;
      while( (c = fgetc(fp))!=EOF && k!=512 ) ++k;
      if(c !=EOF) goto label;
      }
      if(c == EOF) goto fine;
      goto label;
      fine:
      *(UNS*) pc1 = count; *(UNS*) pc2 = rox; *(UNS*) pc3 = sum;
      if(r == 1)
      {
      fseek(fp, ove, SEEK_SET);
      clearerr(fp);
      if(f(acount, fp)==EOF || f(arox, fp)==EOF || f(asum, fp)==EOF)
      {r=0; printf("Error in writing\n");}
      }
      fclose(fp);
      printf("count=% u rox=%u sum=%u\n", count, rox, sum );
      printf("String= %s%s%s\nIn_num= ", acount, arox, asum);
      printu(acount); printf("M") ;printu(arox);p rintf("M");
      printu(asum) ; printf("\n");
      return r;
      }
      /*end my1.c*/
      _______________ _____

      /* ali.c */
      #include <string.h>
      #include <stdio.h>
      #include <ctype.h>
      #include <stdlib.h>
      #include <assert.h>
      #include <limits.h>

      #define UNS unsigned


      char* estr(char* nome)
      {
      char *a = nome, *b;
      char c;

      if(nome == 0)
      return 0;
      while( 1 )
      {
      b = a;
      while( (c = *a)!=0 && c!='\\' && c!='/' ) ++a;
      if(c == 0) break;
      ++a;
      }
      return b;
      }

      int info_p(char* );

      int main(int c, char** argv)
      {
      printf("Start \n");
      if( c==0 || !argv[0] )
      return 0;
      if( info_p(argv[0]) == 0 )
      { printf("File corrupted\n"); return 0; }
      printf("oK continue\n");
      return 0;
      }


      void printu(char* a)
      {
      int i;

      for( i = 0 ; i < sizeof(UNS) ; ++i)
      printf(" %d ", (int) a[i]);
      }

      int f(const char* a, FILE* fp)
      {
      int i, j;

      for( i = 0 ; i < sizeof(UNS) ; ++i)
      {
      if( (j = fgetc( fp ))==EOF ) return 0;
      if( j != (unsigned char) a[i]) return 0;
      }
      return 1;
      }

      /* ###### HERE WE HAVE TO WRITE ###### */
      char al[512] = "1234567 ";


      int info_p(char* as)
      {
      int l, c, r, k, cp;
      FILE *fp;
      unsigned count = 0, rox = 0, sum = 0, *p1, buffer = 0;
      char acount[ sizeof(UNS) + 8 ] = {0},
      arox [ sizeof(UNS) + 8 ] = {0},
      asum [ sizeof(UNS) + 8 ] = {0};
      char *pc1 = acount, *pc2 = arox, *pc3 = asum;
      long ove;

      if((fp = fopen( as, "rb"))==NUL L)
      {
      printf("Problem i di apertura\n");
      return 0;
      }
      r = 0; cp = 0;
      label:
      while((c = fgetc(fp))!=EOF && !(cp=='1' && c=='2'))
      {
      sum += c; ++count;
      cp = c;
      buffer = (buffer << CHAR_BIT) | ((UNS)(unsigned char) c);
      if(count % sizeof(UNS) ==0)
      rox = rox ^ buffer;
      }
      if(c!=EOF && r==0)
      {
      k = 2;
      while((c = fgetc(fp))!=EOF && c== ++k + '0' )
      {
      sum += c; ++count;
      cp = c;
      buffer = (buffer << CHAR_BIT) | (UNS)((unsigned char) c);
      if(count % sizeof(UNS) == 0)
      rox = rox ^ buffer;
      } /*34567*/
      if(k==8 && c!=EOF)
      {
      if( (ove = ftell(fp)) == -1)
      { printf( "Error\n" ); exit( 0 ); }
      r = 1;
      }
      else if(c != EOF) goto label;
      else goto fine;
      while((c = fgetc(fp))!=EOF && k!=512) ++k;
      if(c !=EOF ) goto label;
      }
      if(c == EOF) goto fine;
      goto label;
      fine:
      *(UNS*) pc1 = count; *(UNS*) pc2 = rox; *(UNS*) pc3 = sum;
      if(r == 1){
      fseek(fp, ove, SEEK_SET);
      clearerr(fp);
      if( f(acount, fp)==0 || f(arox, fp)==0 || f(asum, fp)==0 )
      r = 0;
      }
      fclose(fp);
      return r;
      }
      /*end ali.c*/
      _______________ ___


      C:\b>ali
      Start
      File corrupted

      C:\b>my1 ali.exe
      Find
      count=59909 rox=3806249208 sum=5559428
      String=?Û°+ÌÔäÈ T
      In_num= 5 -22 0 0 M -8 -64 -34 -30 M -124 -44 84 0
      Ok signed ali.exe

      C:\b>ali
      Start
      oK continue
      _______________

      Are there errors?
      Are there system where this doesn't run with success?

      Comment

      • Dan Pop

        #4
        Re: a self-checking program in standard C

        In <hcrp70de1qccpn c5j6r402ujvqst9 clv8o@4ax.com> RoSsIaCrIiLoIA <n@esiste.ee> writes:
        [color=blue]
        >Do you know how to write a self-checking program in standard C?[/color]

        I don't know what a self-checking program is in the first place.
        [color=blue]
        >Do I can think that if I write in a file.c
        >static g[100]="1234567";
        >in the file.exe (or file) there is in some place
        >1234567'\0''\0 ''\0''\0''\0''\ 0''\0'...'\0'[/color]

        Not necessarily. Long sequences of null bytes are so common that some
        linkers compress them.
        [color=blue]
        >This is my first attempt:
        >
        >/* file.c -> file.exe */
        >#include <stdio.h>
        >#include <string.h>
        >
        >char* estr(char* nome)
        >{
        > char *a = nome, *b;
        > char c;
        > if(nome == 0)
        > return 0;
        > while( 1 )
        > {b = a;
        > while( (c = *a) && c != '\\' && c != '/' )
        > ++a;
        > if(c == 0) break;
        > ++a;
        > }
        > return b;
        >}
        >
        >int main(int c, char** argv)
        >{
        >(void) c;
        > printf("I'm a self-checking program\n");
        > if(strncmp("dc. exe", estr(argv[0]), 6) != 0)
        > {printf("File corrupted\n"); return 0;}
        > printf("Continu e\n");
        > return 0;
        >}[/color]

        Let's try it:

        fangorn:~/tmp 371> gcc test.c
        fangorn:~/tmp 372> ./a.out
        I'm a self-checking program
        File corrupted

        Which file is corrupted and why? And how can you determine whether a
        file is corrupted or not by examining argv[0]?

        Dan
        --
        Dan Pop
        DESY Zeuthen, RZ group
        Email: Dan.Pop@ifh.de

        Comment

        • Paul M.

          #5
          Re: a self-checking program in standard C

          Dan.Pop@cern.ch (Dan Pop) wrote in message news:<c5jhpc$i5 r$3@sunnews.cer n.ch>...[color=blue]
          > In <hcrp70de1qccpn c5j6r402ujvqst9 clv8o@4ax.com> RoSsIaCrIiLoIA <n@esiste.ee> writes:
          >[color=green]
          > >Do you know how to write a self-checking program in standard C?[/color]
          >
          > I don't know what a self-checking program is in the first place.
          >[color=green]
          > >Do I can think that if I write in a file.c
          > >static g[100]="1234567";
          > >in the file.exe (or file) there is in some place
          > >1234567'\0''\0 ''\0''\0''\0''\ 0''\0'...'\0'[/color]
          >
          > Not necessarily. Long sequences of null bytes are so common that some
          > linkers compress them.
          >[color=green]
          > >This is my first attempt:
          > >
          > >/* file.c -> file.exe */
          > >#include <stdio.h>
          > >#include <string.h>
          > >
          > >char* estr(char* nome)
          > >{
          > > char *a = nome, *b;
          > > char c;
          > > if(nome == 0)
          > > return 0;
          > > while( 1 )
          > > {b = a;
          > > while( (c = *a) && c != '\\' && c != '/' )
          > > ++a;
          > > if(c == 0) break;
          > > ++a;
          > > }[/color]
          > return b;[color=green]
          > >}
          > >
          > >int main(int c, char** argv)
          > >{
          > >(void) c;
          > > printf("I'm a self-checking program\n");
          > > if(strncmp("dc. exe", estr(argv[0]), 6) != 0)
          > > {printf("File corrupted\n"); return 0;}
          > > printf("Continu e\n");
          > > return 0;
          > >}[/color]
          >
          > Let's try it:
          >
          > fangorn:~/tmp 371> gcc test.c
          > fangorn:~/tmp 372> ./a.out
          > I'm a self-checking program
          > File corrupted
          >
          > Which file is corrupted and why? And how can you determine whether a
          > file is corrupted or not by examining argv[0]?
          >
          > Dan[/color]


          A lot of work along these lines has already been done. A company that
          I used to work for had visual studio add-in tools. See the page:
          Application Security from Digital.ai is an app security tool designed to monitor and protect apps across mobile, desktop, and web. Build secure software today!


          Paul

          Comment

          • Victor Nazarov

            #6
            Re: [OT] Re: a self-checking program in standard C

            RoSsIaCrIiLoIA wrote:[color=blue]
            > C:\b>ali
            > Start
            > File corrupted
            >
            > C:\b>my1 ali.exe
            > Find
            > count=59909 rox=3806249208 sum=5559428
            > String=?Û°+ÌÔäÈ T
            > In_num= 5 -22 0 0 M -8 -64 -34 -30 M -124 -44 84 0
            > Ok signed ali.exe
            >
            > C:\b>ali
            > Start
            > oK continue
            > _______________
            >
            > Are there errors?
            > Are there system where this doesn't run with success?
            >[/color]

            This is not portable anyway. Really It depends on the executable format.
            Executables often get comressed. Try another newsgroup...

            vir

            Comment

            • RoSsIaCrIiLoIA

              #7
              Re: [OT] Re: a self-checking program in standard C

              [comp.lang.c, sci.crypt]
              On Wed, 14 Apr 2004 +0400, Victor Nazarov <vviruss@mail.r u> wrote:
              [color=blue]
              >This is not portable anyway. Really It depends on the executable format.
              >Executables often get comressed. Try another newsgroup...[/color]

              yes compressed or *encrypted*
              If the OS has a database of all the public key of software houses,
              if M.exe is a encrypted program of Company XX
              the loader of the OS could take the public key of Company XX
              encrypt it [on the fly] and jump on it.

              If a crakker want to crack M.exe he has to know the secret key of
              Company XX or change the code in the memory space of that running
              program.(in many OSes seems to me that the memory space of a program
              is closed to other process)

              Regarding Internet **why** OSes seems to have the default in allowing
              *every* FTP-telnet-http-all *fu?king* protocol- IN EXIT for the
              home-PC ( and change it in a world server of resources) ???
              _______________ ___
              Il popolo dell'iraaq non vuole eserciti oppressori sia onu o altro
              (se non ci credete fate un referendum)
              Come ragionate con i piedi?

              Comment

              Working...