how to write C program that copy's it's self onto the hard drive, while its running?

Discussion in 'C Programming' started by jon, Sep 3, 2006.

  1. jon

    jon Guest

    I am a beginner C programmer, this is actually my first programming
    lanugage, besides html, cgi, and javascript. I am looking for a way to
    make an .exe file, or a copy of my own file. I have tried writing a
    file, compling it, and reading it in a notepad, then writing a program
    to write it again, but i have had no luck. I assure you i'm not trying
    to create the next big virus, or worm, but only trying to expaned my
    knowledge on what programming language i will take. C is my perferred
    taste, though i may go into java if this dosen't work out.

    aspiring programmer
    Jon
    15
     
    jon, Sep 3, 2006
    #1
    1. Advertising

  2. jon

    jacob navia Guest

    Re: how to write C program that copy's it's self onto the hard drive,while its running?

    jon wrote:
    > I am a beginner C programmer, this is actually my first programming
    > lanugage, besides html, cgi, and javascript. I am looking for a way to
    > make an .exe file, or a copy of my own file. I have tried writing a
    > file, compling it, and reading it in a notepad, then writing a program
    > to write it again, but i have had no luck. I assure you i'm not trying
    > to create the next big virus, or worm, but only trying to expaned my
    > knowledge on what programming language i will take. C is my perferred
    > taste, though i may go into java if this dosen't work out.
    >
    > aspiring programmer
    > Jon
    > 15
    >


    Mmmm a program that copies itself???

    If that isn't a virus... please tell me what you want to achieve.

    You know that

    copy myexe.exe my-NEW-exe.exe

    will work at the command line prompt.

    This is quite easy. But no, you want it to copy
    it when it is running... Why?

    In any case it will be difficult, since when your program
    is running, it is locked and fopen will fail... as you
    have seen.

    Try Java. I am sure there you will find an
    easy way to copy a running JAVA program isn't it?

    :)

    jacob
     
    jacob navia, Sep 3, 2006
    #2
    1. Advertising

  3. jon

    jon Guest

    jacob navia wrote:
    > jon wrote:
    > > I am a beginner C programmer, this is actually my first programming
    > > lanugage, besides html, cgi, and javascript. I am looking for a way to
    > > make an .exe file, or a copy of my own file. I have tried writing a
    > > file, compling it, and reading it in a notepad, then writing a program
    > > to write it again, but i have had no luck. I assure you i'm not trying
    > > to create the next big virus, or worm, but only trying to expaned my
    > > knowledge on what programming language i will take. C is my perferred
    > > taste, though i may go into java if this dosen't work out.
    > >
    > > aspiring programmer
    > > Jon
    > > 15
    > >

    >
    > Mmmm a program that copies itself???
    >
    > If that isn't a virus... please tell me what you want to achieve.
    >
    > You know that
    >
    > copy myexe.exe my-NEW-exe.exe
    >
    > will work at the command line prompt.
    >
    > This is quite easy. But no, you want it to copy
    > it when it is running... Why?
    >
    > In any case it will be difficult, since when your program
    > is running, it is locked and fopen will fail... as you
    > have seen.
    >
    > Try Java. I am sure there you will find an
    > easy way to copy a running JAVA program isn't it?
    >
    > :)
    >
    > jacob



    Well, The program writes webpages, and when user wants to start another
    one, the new copy will be made.
     
    jon, Sep 4, 2006
    #3
  4. Fork?

    "jon" <> wrote in message
    news:...
    >
    > jacob navia wrote:
    >> jon wrote:
    >> > I am a beginner C programmer, this is actually my first programming
    >> > lanugage, besides html, cgi, and javascript. I am looking for a way to
    >> > make an .exe file, or a copy of my own file. I have tried writing a
    >> > file, compling it, and reading it in a notepad, then writing a program
    >> > to write it again, but i have had no luck. I assure you i'm not trying
    >> > to create the next big virus, or worm, but only trying to expaned my
    >> > knowledge on what programming language i will take. C is my perferred
    >> > taste, though i may go into java if this dosen't work out.
    >> >
    >> > aspiring programmer
    >> > Jon
    >> > 15
    >> >

    >>
    >> Mmmm a program that copies itself???
    >>
    >> If that isn't a virus... please tell me what you want to achieve.
    >>
    >> You know that
    >>
    >> copy myexe.exe my-NEW-exe.exe
    >>
    >> will work at the command line prompt.
    >>
    >> This is quite easy. But no, you want it to copy
    >> it when it is running... Why?
    >>
    >> In any case it will be difficult, since when your program
    >> is running, it is locked and fopen will fail... as you
    >> have seen.
    >>
    >> Try Java. I am sure there you will find an
    >> easy way to copy a running JAVA program isn't it?
    >>
    >> :)
    >>
    >> jacob

    >
    >
    > Well, The program writes webpages, and when user wants to start another
    > one, the new copy will be made.
    >
     
    tirrell payton, Sep 4, 2006
    #4
  5. On 3 Sep 2006 17:34:49 -0700, in comp.lang.c , "jon"
    <> wrote:

    >
    >Well, The program writes webpages, and when user wants to start another
    >one, the new copy will be made.


    Er, you don't want to make a new copy of the programme, you want to
    run a new instance of it in memory. That is platform specific, search
    your platform documentation or ask in a relevant group.
    --
    Mark McIntyre

    "Debugging is twice as hard as writing the code in the first place.
    Therefore, if you write the code as cleverly as possible, you are,
    by definition, not smart enough to debug it."
    --Brian Kernighan
     
    Mark McIntyre, Sep 4, 2006
    #5
  6. jon

    jmcgill Guest

    Re: how to write C program that copy's it's self onto the hard drive,while its running?

    jon wrote:

    > Well, The program writes webpages, and when user wants to start another
    > one, the new copy will be made.



    It's hard to understand what you are trying to do, but it sounds like
    you are trying to go straight into a multi-threaded programming model
    without learning the basics of the language first.

    I would say the Java thread model is *much* easier to work with than any
    threads library you will find for C.

    If you really want help from others you will need to do a much better
    job of explaining what it is you are trying to do.
     
    jmcgill, Sep 5, 2006
    #6
  7. Re: how to write C program that copy's it's self onto the hard drive,while its running?

    In article <44fb5446$0$5095$>,
    jacob navia <> wrote:
    >This is quite easy. But no, you want it to copy
    >it when it is running... Why?


    >In any case it will be difficult, since when your program
    >is running, it is locked and fopen will fail... as you
    >have seen.


    Not necessarily true, Jacob. The OP did not specify a platform,
    and in some platforms it is possible to open the running binary:


    $ cc -o rr rr.c
    $ ./rr
    ../rr had no problem openning itself for ab+
    $ cat rr.c
    #include <stdio.h>
    int main(int argc, char **argv) {
    FILE *me = fopen(argv[0],"ab+");
    if (!me) {
    printf( "%s was not able to open itself for ab+\n", argv[0] );
    } else {
    printf( "%s had no problem openning itself for ab+\n", argv[0] );
    }
    return 0;
    }
    $ uname -a
    IRIX64 origin 6.5 10151453 IP27


    Indeed, if I recall correctly, there is specific USG (Unix Systems Group --
    i.e., the official Unix specification) language about this situation,
    dealing with which version of a swapped-out page that is to be read in
    if the binary is rewritten underneath it (oddly, the requirement
    is that it is the -new- version of the page that must be swapped in.)
    --
    Okay, buzzwords only. Two syllables, tops. -- Laurie Anderson
     
    Walter Roberson, Sep 5, 2006
    #7
  8. jon

    jacob navia Guest

    Re: how to write C program that copy's it's self onto the hard drive,while its running?

    Walter Roberson wrote:
    > In article <44fb5446$0$5095$>,
    > jacob navia <> wrote:
    >
    >>This is quite easy. But no, you want it to copy
    >>it when it is running... Why?

    >
    >
    >>In any case it will be difficult, since when your program
    >>is running, it is locked and fopen will fail... as you
    >>have seen.

    >
    >
    > Not necessarily true, Jacob. The OP did not specify a platform,
    > and in some platforms it is possible to open the running binary:
    >
    >
    > $ cc -o rr rr.c
    > $ ./rr
    > ./rr had no problem openning itself for ab+
    > $ cat rr.c
    > #include <stdio.h>
    > int main(int argc, char **argv) {
    > FILE *me = fopen(argv[0],"ab+");
    > if (!me) {
    > printf( "%s was not able to open itself for ab+\n", argv[0] );
    > } else {
    > printf( "%s had no problem openning itself for ab+\n", argv[0] );
    > }
    > return 0;
    > }
    > $ uname -a
    > IRIX64 origin 6.5 10151453 IP27
    >
    >
    > Indeed, if I recall correctly, there is specific USG (Unix Systems Group --
    > i.e., the official Unix specification) language about this situation,
    > dealing with which version of a swapped-out page that is to be read in
    > if the binary is rewritten underneath it (oddly, the requirement
    > is that it is the -new- version of the page that must be swapped in.)


    Gosh!

    You can write self modifying code in there!

    That's news for me.

    Actually there could be a lot of applications, like
    instead of saving the settings in an ASCII file,
    then reading them back in the next time the program is run,
    you can directly write the settings structure to the executable
    and it will be there next time automagically!!!

    Thanks for this clarification, I stand corrected.
     
    jacob navia, Sep 5, 2006
    #8
  9. jon

    goose Guest

    Re: how to write C program that copy's it's self onto the hard drive,while its running?

    Walter Roberson wrote:

    <snipped>

    > Indeed, if I recall correctly, there is specific USG (Unix Systems Group --
    > i.e., the official Unix specification) language about this situation,
    > dealing with which version of a swapped-out page that is to be read in
    > if the binary is rewritten underneath it (oddly, the requirement
    > is that it is the -new- version of the page that must be swapped in.)


    <OT> They don't actually work like that. Removing a link to a
    file (filename) still leaves the data happily on disk. Once
    the last process (that has an open handle on the file)
    closes the file *then* the data space is marked as free.
    </OT>

    --
    goose
    Have I offended you? Send flames to root@localhost
    real email: lelanthran at gmail dot com
    website : www.lelanthran.com
     
    goose, Sep 7, 2006
    #9
  10. Re: how to write C program that copy's it's self onto the hard drive,while its running?

    In article <edpuf8$4su$>,
    goose <> wrote:
    >Walter Roberson wrote:


    >> Indeed, if I recall correctly, there is specific USG (Unix Systems Group --
    >> i.e., the official Unix specification) language about this situation,
    >> dealing with which version of a swapped-out page that is to be read in
    >> if the binary is rewritten underneath it (oddly, the requirement
    >> is that it is the -new- version of the page that must be swapped in.)


    ><OT> They don't actually work like that. Removing a link to a
    >file (filename) still leaves the data happily on disk. Once
    >the last process (that has an open handle on the file)
    >closes the file *then* the data space is marked as free.
    ></OT>


    That's a different situation. I'm referring to the situation in which
    the contents of the executable file are *updated* while the executable is
    running. For example, if you dd a few blocks of /dev/random into the file,
    or if you do a binary patch.

    Updating in place is sometimes a temptation because it takes less work
    to set up the ownership and permissions (and access-control lists if
    applicable), especially if the binary is writable by some entity
    but that entity doesn't have update permissions on the directory that
    contains the binary.
    --
    "It is important to remember that when it comes to law, computers
    never make copies, only human beings make copies. Computers are given
    commands, not permission. Only people can be given permission."
    -- Brad Templeton
     
    Walter Roberson, Sep 7, 2006
    #10
  11. Re: how to write C program that copy's it's self onto the hard drive,while its running?

    >> Indeed, if I recall correctly, there is specific USG (Unix Systems Group --
    >> i.e., the official Unix specification) language about this situation,
    >> dealing with which version of a swapped-out page that is to be read in
    >> if the binary is rewritten underneath it (oddly, the requirement
    >> is that it is the -new- version of the page that must be swapped in.)


    You're talking about different situations. The above assumes
    you opened a running executable for write (some systems prohibit this)
    and write new data on it (e.g. apply patch or copy whole new executable
    over the old one.

    ><OT> They don't actually work like that. Removing a link to a
    >file (filename) still leaves the data happily on disk. Once
    >the last process (that has an open handle on the file)
    >closes the file *then* the data space is marked as free.
    ></OT>


    This situation assumes you *delete* the file (under one of its
    names, at least), then re-create the file under the same name. This
    is a very different situation.

    Some systems consider *any* writing on executable code to be symptomatic
    of a virus and don't allow it. That would include all compilers and
    linkers. Some systems require executable code to be "blessed" before
    it's usable as executable code (a weak example here is the "executable"
    bit in UNIX. A stronger example would be that executables that aren't
    already signed by the vendor can only be created by a compiler signed
    by the vendor. In these situations, the answer could be that you
    can't portably copy an executable and still use it as an executable.
     
    Gordon Burditt, Sep 8, 2006
    #11
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. mh
    Replies:
    4
    Views:
    720
    Wolfgang Strobl
    May 31, 2005
  2. Ralf W. Grosse-Kunstleve
    Replies:
    16
    Views:
    603
    Lonnie Princehouse
    Jul 11, 2005
  3. Ralf W. Grosse-Kunstleve
    Replies:
    18
    Views:
    609
    Bengt Richter
    Jul 11, 2005
  4. Ralf W. Grosse-Kunstleve
    Replies:
    2
    Views:
    417
    Dan Sommers
    Jul 12, 2005
  5. shailesh
    Replies:
    1
    Views:
    819
    Tim Golden
    Mar 28, 2007
Loading...

Share This Page