PHP Seite mit Cookie und BasicAuth

Sockets, TCP/IP, (XML-)RPC und ähnliche Themen gehören in dieses Forum
Antworten
i1337
User
Beiträge: 11
Registriert: Mittwoch 23. September 2009, 15:39

Hallo,
ich versuche eine Http-Seite auszulesen, der Server benutz BasicAuth und ein Cookie.
z.Z. Versuche ich zunächst eine gültige Verbinung zu dem Server zubekommen.
Habe mir dazu diesen Code aus Beispielen aus dem Netz zusammen gebaut.
Aber er funktioniert nicht, ich bekomme immer den 401 Error.
Vielleicht kann mir jemand sagen, was ich falsch mache.

Code: Alles auswählen

#!/usr/bin/env python

import os.path
import urllib2


def get_it (un_para, pw_para, ip_para, tu_para):

    username = un_para
    password = pw_para
    ip = 'http://%s/' %ip_para
    theurl = '%s'%ip+'%s'%tu_para
    # def of the vars

    print 'The UserName is: "%s".' %username
    print 'The Password is: "%s".' %password
    print 'IP is: "%s".' %ip
    print 'The URl is: "%s".' %theurl
    # just 4 control

    passman = urllib2.HTTPPasswordMgrWithDefaultRealm()
    # this creates a password manager

    passman.add_password(None, ip, username, password)
    # because we have put None at the start it will always
    # use this username/password combination for  urls
    # for which `theurl` is a super-url


    COOKIEFILE = 'cookies.lwp'
    # the path and filename to save your cookies in

    cj = None
    ClientCookie = None
    cookielib = None


    ##authhandler = urllib2.HTTPDigestAuthHandler(passman)
    authhandler = urllib2.HTTPBasicAuthHandler(passman)
    # create the AuthHandler

    proxy_support = urllib2.ProxyHandler({})
    # No proxy for the lab server

    ##opener = urllib2.build_opener(authhandler,proxy_support)

    ##urllib2.install_opener(opener)

    # All calls to urllib2.urlopen will now use our handler
    # Make sure not to include the protocol in with the URL, or
    # HTTPPasswordMgrWithDefaultRealm will be very confused.
    # You must (of course) use it when fetching the page though.


    # Let's see if cookielib is available
    try:
        import cookielib
    except ImportError:
        # If importing cookielib fails
        # let's try ClientCookie
        try:
            import ClientCookie
        except ImportError:
            # ClientCookie isn't available either
            urlopen = urllib2.urlopen
            Request = urllib2.Request
            print 'no Cookie-Handler-Lib found !'
        else:
            # imported ClientCookie
            urlopen = ClientCookie.urlopen
            Request = ClientCookie.Request
            cj = ClientCookie.LWPCookieJar()
            print 'Cookie-Handler-Lib: "ClientCookie"'

    else:
        # importing cookielib worked
        urlopen = urllib2.urlopen
        Request = urllib2.Request
        cj = cookielib.LWPCookieJar()
        print 'Cookie-Handler-Lib: "cookielib"'
        # This is a subclass of FileCookieJar
        # that has useful load and save methods

    if cj is not None:
    # we successfully imported
    # one of the two cookie handling modules

        if os.path.isfile(COOKIEFILE):
            # if we have a cookie file already saved
            # then load the cookies into the Cookie Jar
            cj.load(COOKIEFILE)

        # Now we need to get our Cookie Jar
        # installed in the opener;
        # for fetching URLs
        if cookielib is not None:
            # if we use cookielib
            # then we get the HTTPCookieProcessor
            # and install the opener in urllib2
            opener = urllib2.build_opener(authhandler,proxy_support,urllib2.HTTPCookieProcessor(cj))
            urllib2.install_opener(opener)

        else:
           # if we use ClientCookie
           # then we get the HTTPCookieProcessor
           # and install the opener in ClientCookie
           opener = ClientCookie.build_opener(ClientCookie.HTTPCookieProcessor(cj))
           ClientCookie.install_opener(opener)

    txdata = None
    # if we were making a POST type request,
    # we could encode a dictionary of values here,
    # using urllib.urlencode(somedict)

    txheaders =  {'User-agent' : 'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)'}
    # fake a user agent, some websites (like google) don't like automated exploration

    try:
        req = Request(theurl, txdata, txheaders)
        # create a request object

        handle = urlopen(req)
        # and open it to return a handle on the url

    except IOError, e:
        print 'We failed to open "%s".' % theurl
        if hasattr(e, 'code'):
            print 'We failed with error code - %s.' % e.code
        elif hasattr(e, 'reason'):
            print "The error object has the following 'reason' attribute :"
            print e.reason
            print "This usually means the server doesn't exist,"
            print "is down, or we don't have an internet connection."
        print '###############################'
        return

    else:
        print 'Here are the headers of the page :'
        print handle.info()
        print handle.read() # returns the page
        print handle.geturl() # returns the true url of the page fetched
        # (in case urlopen has followed any redirects, which it sometimes does)

    print
    if cj is None:
        print "We don't have a cookie library available - sorry."
        print "I can't show you any cookies."
    else:
        print 'These are the cookies we have received so far :'
        for index, cookie in enumerate(cj):
            print index, '  :  ', cookie
        cj.save(COOKIEFILE)                     # save the cookies again
    print '###############################'


tmp_username = 'admin'
tmp_password = 'admin'
# set username & passwort
tmp_ip = '10.40.36.162'
tmp_theurl = 'phpfiles/OverView.php'
# set ip & current path

get_it (tmp_username, tmp_password, tmp_ip, tmp_theurl)

Hier noch der Mitschintt vom FireFox, wenn ich auf die Adresse http:\\10.40.36.162\ zugreife.

Code: Alles auswählen


http://10.40.36.162/phpfiles/OverView.php

GET /phpfiles/OverView.php HTTP/1.1
Host: 10.40.36.162
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.2; de; rv:1.9.1.3) Gecko/20090824 Firefox/3.5.3
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: de-DE,en-us;q=0.5
Accept-Encoding: gzip,deflate
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive: 300
Connection: keep-alive
Cookie: PHPSESSID=a9cc8806eed9c715bc06f7da764b8396
Authorization: Basic cmVzZXQ6cmVzZXQ=

HTTP/1.x 302 Found
Date: Thu, 17 Sep 2009 14:27:57 GMT
Server: Apache/2.0.54 (Unix) DAV/2 PHP/5.2.9
X-Powered-By: PHP/5.2.9
Expires: Thu, 19 Nov 1981 08:52:00 GMT
Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Pragma: no-cache
Location: auth3.php
Content-Length: 2678
Keep-Alive: timeout=1, max=1
Connection: Keep-Alive
Content-Type: text/html; charset=UTF-8
----------------------------------------------------------
http://10.40.36.162/phpfiles/auth3.php

GET /phpfiles/auth3.php HTTP/1.1
Host: 10.40.36.162
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.2; de; rv:1.9.1.3) Gecko/20090824 Firefox/3.5.3
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: de-DE,en-us;q=0.5
Accept-Encoding: gzip,deflate
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive: 300
Connection: keep-alive
Cookie: PHPSESSID=a9cc8806eed9c715bc06f7da764b8396
Authorization: Basic cmVzZXQ6cmVzZXQ=

HTTP/1.x 401 Unauthorized
Date: Thu, 17 Sep 2009 14:27:57 GMT
Server: Apache/2.0.54 (Unix) DAV/2 PHP/5.2.9
X-Powered-By: PHP/5.2.9
Expires: Thu, 19 Nov 1981 08:52:00 GMT
Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Pragma: no-cache
WWW-Authenticate: Basic realm=ServerView Remote Management
Content-Length: 0
Connection: close
Content-Type: text/html; charset=UTF-8
----------------------------------------------------------
http://10.40.36.162/phpfiles/auth3.php

GET /phpfiles/auth3.php HTTP/1.1
Host: 10.40.36.162
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.2; de; rv:1.9.1.3) Gecko/20090824 Firefox/3.5.3
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: de-DE,en-us;q=0.5
Accept-Encoding: gzip,deflate
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive: 300
Connection: keep-alive
Cookie: PHPSESSID=a9cc8806eed9c715bc06f7da764b8396
Authorization: Basic YWRtaW46YWRtaW4=

HTTP/1.x 302 Found
Date: Thu, 17 Sep 2009 14:27:59 GMT
Server: Apache/2.0.54 (Unix) DAV/2 PHP/5.2.9
X-Powered-By: PHP/5.2.9
Expires: Thu, 19 Nov 1981 08:52:00 GMT
Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Pragma: no-cache
Location: OverView.php
Content-Length: 0
Keep-Alive: timeout=1, max=1
Connection: Keep-Alive
Content-Type: text/html; charset=UTF-8
----------------------------------------------------------
http://10.40.36.162/phpfiles/OverView.php

GET /phpfiles/OverView.php HTTP/1.1
Host: 10.40.36.162
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.2; de; rv:1.9.1.3) Gecko/20090824 Firefox/3.5.3
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: de-DE,en-us;q=0.5
Accept-Encoding: gzip,deflate
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive: 300
Connection: keep-alive
Cookie: PHPSESSID=a9cc8806eed9c715bc06f7da764b8396
Authorization: Basic YWRtaW46YWRtaW4=

HTTP/1.x 200 OK
Date: Thu, 17 Sep 2009 14:27:59 GMT
Server: Apache/2.0.54 (Unix) DAV/2 PHP/5.2.9
X-Powered-By: PHP/5.2.9
Expires: Thu, 19 Nov 1981 08:52:00 GMT
Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Pragma: no-cache
Content-Length: 2678
Keep-Alive: timeout=1, max=1
Connection: Keep-Alive
Content-Type: text/html; charset=UTF-8
----------------------------------------------------------
http://10.40.36.162/phpfiles/MainFrame.php

GET /phpfiles/MainFrame.php HTTP/1.1
Host: 10.40.36.162
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.2; de; rv:1.9.1.3) Gecko/20090824 Firefox/3.5.3
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: de-DE,en-us;q=0.5
Accept-Encoding: gzip,deflate
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive: 300
Connection: keep-alive
Referer: http://10.40.36.162/phpfiles/OverView.php
Cookie: PHPSESSID=a9cc8806eed9c715bc06f7da764b8396
Authorization: Basic YWRtaW46YWRtaW4=

HTTP/1.x 200 OK
Date: Thu, 17 Sep 2009 14:28:00 GMT
Server: Apache/2.0.54 (Unix) DAV/2 PHP/5.2.9
X-Powered-By: PHP/5.2.9
Expires: Thu, 19 Nov 1981 08:52:00 GMT
Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Pragma: no-cache
Content-Length: 1116
Connection: close
Content-Type: text/html; charset=UTF-8

Hoffe mir kann geholfen werden
mfg.
sma
User
Beiträge: 3018
Registriert: Montag 19. November 2007, 19:57
Wohnort: Kiel

Erster Fehler: Zu viel Code für meinen Geschmack.

Daher nur so viel: Ist dir klar, dass du laut Mitschnitt "reset:reset" als User/Password schickst und nicht das in deinem Code stehende admin/admin?

Stefan
i1337
User
Beiträge: 11
Registriert: Mittwoch 23. September 2009, 15:39

Ja, weniger Code hätte ich auch gern :D.

Das mit reset:reset und admin:admin, ist mir bewust.
Das ist etwas, was mich an dem Http-Mitschnitt wundert.

Wenn ich zum erstenmal auf den Server zugreife,
fragt FireFox nach user:pass, das ist admin:admin.
Er sendet aber erst 2x reset:reset:

/phpfiles/OverView.php

/phpfiles/auth3.php

Dann den rest mit admin:admin

/phpfiles/auth3.php

/phpfiles/OverView.php

Wie bekomme ich diese Anmeldprozedur mit Python hin?

mfg
Antworten