when format strings attack

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Eric_Dexter@msn.com

    #1

    when format strings attack



    I saw a warning from homeland security about this. I only comment on
    the because I am trying to use os.system('comm and1 arg') and it doesn't
    work but I do see examples with % that is borrowed from the c language.
    Seems like if I can write a batch file that does something the same
    behavior should happen in the os module..

  • Gabriel Genellina

    #2
    Re: when format strings attack

    <Eric_Dexter@ms n.comescribió en el mensaje
    news:1169207467 .989977.162940@ q2g2000cwa.goog legroups.com...

    >
    I saw a warning from homeland security about this. I only comment on
    the because I am trying to use os.system('comm and1 arg') and it doesn't
    work but I do see examples with % that is borrowed from the c language.
    Seems like if I can write a batch file that does something the same
    behavior should happen in the os module..
    Pure Python programs are not affected, but a review of the C implementation
    should be made to see if any (variant of) printf is used without a proper
    format. Anyway I doubt you could find something, because the vulnerability
    is so well known for ages.

    --
    Gabriel Genellina


    Comment

    • Nick Maclaren

      #3
      Re: when format strings attack


      In article <mailman.2908.1 169221530.32031 .python-list@python.org >,
      "Gabriel Genellina" <gagsl-py@yahoo.com.ar writes:
      |<Eric_Dexter@m sn.comescribió en el mensaje
      |news:116920746 7.989977.162940 @q2g2000cwa.goo glegroups.com.. .
      |>
      | http://www.ddj.com/184405774;jsessio...QCKHSCJUNN2JVN
      |
      | I saw a warning from homeland security about this. I only comment on
      | the because I am trying to use os.system('comm and1 arg') and it doesn't
      | work but I do see examples with % that is borrowed from the c language.
      | Seems like if I can write a batch file that does something the same
      | behavior should happen in the os module..
      |>
      |Pure Python programs are not affected, but a review of the C implementation
      |should be made to see if any (variant of) printf is used without a proper
      |format. Anyway I doubt you could find something, because the vulnerability
      |is so well known for ages.

      Not really. There are LOTS of vulnerabilities that have been known
      for ages and are still legion. The reason that this is unlikely is
      that it is both easy to spot and trivial to fix.


      Regards,
      Nick Maclaren.

      Comment

      • Gabriel Genellina

        #4
        Re: when format strings attack

        "Nick Maclaren" <nmm1@cus.cam.a c.ukescribió en el mensaje
        news:eoqr1s$khg $1@gemini.csx.c am.ac.uk...
        In article <mailman.2908.1 169221530.32031 .python-list@python.org >,
        "Gabriel Genellina" <gagsl-py@yahoo.com.ar writes:
        |>
        |Pure Python programs are not affected, but a review of the C
        implementation
        |should be made to see if any (variant of) printf is used without a
        proper
        |format. Anyway I doubt you could find something, because the
        vulnerability
        |is so well known for ages.
        >
        Not really. There are LOTS of vulnerabilities that have been known
        for ages and are still legion. The reason that this is unlikely is
        that it is both easy to spot and trivial to fix.
        Yes... Anyway, unless someone actually *do* revise the code, if it's easy or
        not has no importance. I think that some automated tools were used to find
        problems, but I don't know if this specific vulnerability was searched.

        --
        Gabriel Genellina


        Comment

        • John Zenger

          #5
          Re: when format strings attack

          Perhaps it is not as severe a security risk, but pure Python programs
          can run into similar problems if they don't check user input for %
          codes. Example:
          >>k = raw_input("Try to trick me: ")
          Try to trick me: How about %s this?
          >>j = "User %s just entered: " + k
          >>print j % "John"
          Traceback (most recent call last):
          File "<pyshell#8 >", line 1, in ?
          print j % "John"
          TypeError: not enough arguments for format string



          On Jan 19, 10:44 am, "Gabriel Genellina" <gagsl...@yahoo .com.arwrote:
          <Eric_Dex...@ms n.comescribió en el mensajenews:116 9207467.989977. 162940@q2g2000c wa.googlegroups .com...
          >>
          I saw a warning from homeland security about this. I only comment on
          the because I am trying to use os.system('comm and1 arg') and it doesn't
          work but I do see examples with % that is borrowed from the c language.
          Seems like if I can write a batch file that does something the same
          behavior should happen in the os module..Pure Python programs are not affected, but a review of the C implementation
          should be made to see if any (variant of) printf is used without a proper
          format. Anyway I doubt you could find something, because the vulnerability
          is so well known for ages.

          --
          Gabriel Genellina

          Comment

          • Steven D'Aprano

            #6
            Re: when format strings attack

            On Fri, 19 Jan 2007 03:51:08 -0800, Eric_Dexter@msn .com wrote:

            >
            I saw a warning from homeland security about this. I only comment on
            the because I am trying to use os.system('comm and1 arg') and it doesn't
            work
            What do you mean, doesn't work? It works fine for me, precisely as
            expected. What does it do for you? Crash Windows? Crash Python? Raise an
            exception? Return an unexpected result?
            but I do see examples with % that is borrowed from the c language.
            The "When Format Strings Attack" article isn't relevant to Python. Unlike
            C, Python doesn't arbitrary dump bytes from the stack into a string if you
            print a string containing %s. In Python, print just prints strings, it
            doesn't do any string formatting. String formatting is done by the %
            operator, so print "a string containing %s" is safe.

            You'd be better off looking at Python examples than C. This is what I'm
            guessing you're doing:
            >>command1 = 'dir'
            >>args = '-l text.txt'
            >>os.system('co mmand1 arg')
            sh: command1: command not found
            32512

            os.system doesn't do name-lookups of the string you pass to it. The right
            way to do this is some variation on this:
            >>commandline = "%s %s" % (command1, args)
            >>commandline
            'dir -l text.txt'
            >>os.system(com mandline)
            -rw-rw-r-- 1 steve steve 333 Sep 24 16:51 text.txt
            0

            or even something like this:

            os.system('dir -l %s' % 'text.txt')


            Now, there is a security risk: you might set command1 yourself, and
            allow the user to set args. If command1 is an external application
            with a security hole, and the user provides arguments that trigger that
            bug, then naturally your application will inherit whatever security
            vulnerabilities the external application suffers from. No surprises there.


            --
            Steven.

            Comment

            • Gabriel Genellina

              #7
              Re: when format strings attack

              At Friday 19/1/2007 15:43, John Zenger wrote:
              >Perhaps it is not as severe a security risk, but pure Python programs
              >can run into similar problems if they don't check user input for %
              >codes. Example:
              >
              >k = raw_input("Try to trick me: ")
              >Try to trick me: How about %s this?
              >j = "User %s just entered: " + k
              >print j % "John"
              >Traceback (most recent call last):
              File "<pyshell#8 >", line 1, in ?
              print j % "John"
              >TypeError: not enough arguments for format string
              That's not a problem, it's an exception. *This* is a problem:
              printf("Hello, %s")


              --
              Gabriel Genellina
              Softlab SRL






              _______________ _______________ _______________ _____
              Preguntá. Respondé. Descubrí.
              Todo lo que querías saber, y lo que ni imaginabas,
              está en Yahoo! Respuestas (Beta).
              ¡Probalo ya!


              Comment

              • Steven D'Aprano

                #8
                Re: when format strings attack

                On Fri, 19 Jan 2007 10:43:53 -0800, John Zenger wrote:
                Perhaps it is not as severe a security risk, but pure Python programs
                can run into similar problems if they don't check user input for %
                codes.
                Please don't top-post.

                A: Because it messes up the order that we read things.
                Q: Why?
                A: Top-posting.
                Q: What is the most annoying newsgroup habit?

                Example:
                >
                >>>k = raw_input("Try to trick me: ")
                Try to trick me: How about %s this?
                >>>j = "User %s just entered: " + k
                >>>print j % "John"
                Traceback (most recent call last):
                File "<pyshell#8 >", line 1, in ?
                print j % "John"
                TypeError: not enough arguments for format string
                That's hardly the same sort of vulnerability the article was talking
                about, but it is a potential bug waiting to bite.

                In a serious application, you should keep user-inputted strings separate
                from application strings, and never use user strings unless they've been
                made safe. See Joel Spolsky's excellent article about one way of doing
                that:

                Way back in September 1983, I started my first real job, working at Oranim, a big bread factory in Israel that made something like 100,000 loaves of bread every night in six giant ovens the size of…




                --
                Steven.

                Comment

                • Jeremy Sanders

                  #9
                  Re: when format strings attack

                  Steven D'Aprano wrote:
                  os.system('dir -l %s' % 'text.txt')
                  >
                  >
                  Now, there is a security risk: you might set command1 yourself, and
                  allow the user to set args. If command1 is an external application
                  with a security hole, and the user provides arguments that trigger that
                  bug, then naturally your application will inherit whatever security
                  vulnerabilities the external application suffers from. No surprises there.
                  There are also big risks like this

                  filename = 'foo; rm importantfile'
                  cmd = 'ls %s' % filename
                  os.system(cmd)

                  oops!

                  --
                  Jeremy Sanders

                  Comment

                  • Eric_Dexter@msn.com

                    #10
                    Re: when format strings attack

                    I will give the formatting a try. I noticed another formatting thing I
                    wasn't looking for. It is possible to have a \n at the end of a word
                    or at least that is how it is shown and fixed through python 2.5. I
                    had an error where 36\n isn't a number. easy to fix though.


                    Jeremy Sanders wrote:
                    Steven D'Aprano wrote:
                    >
                    os.system('dir -l %s' % 'text.txt')


                    Now, there is a security risk: you might set command1 yourself, and
                    allow the user to set args. If command1 is an external application
                    with a security hole, and the user provides arguments that trigger that
                    bug, then naturally your application will inherit whatever security
                    vulnerabilities the external application suffers from. No surprises there.
                    >
                    There are also big risks like this
                    >
                    filename = 'foo; rm importantfile'
                    cmd = 'ls %s' % filename
                    os.system(cmd)
                    >
                    oops!
                    >
                    --
                    Jeremy Sanders
                    http://www.jeremysanders.net/

                    Comment

                    Working...