1

I want to use python to log into a website which uses Microsoft Forefront, and retrieve the content of an internal webpage for processing.

I am not new to python but I have not used any URL libraries. I checked the following posts:

I have also tried a couple of modules such as requests. Still I am unable to understand how this should be done, Is it enough to enter username/password? Or should I somehow use the cookies to authenticate? Any sample code would really be appreciated.

This is the code I have so far:

import requests

NAME = 'XXX'
PASSWORD = 'XXX'

URL = 'https://intra.xxx.se/CookieAuth.dll?GetLogon?curl=Z2F&reason=0&formdir=3'

def main():
    # Start a session so we can have persistant cookies
    session = requests.session()

    # This is the form data that the page sends when logging in
    login_data = {
        'username': NAME,
        'password': PASSWORD,
        'SubmitCreds': 'login',
    }

    # Authenticate
    r = session.post(URL, data=login_data)

    # Try accessing a page that requires you to be logged in
    r = session.get('https://intra.xxx.se/?t=1-2')
    print r

main()

but the above code results in the following exception, on the session.post-line:

raise ConnectionError(e)
requests.exceptions.ConnectionError: HTTPSConnectionPool(host='intra.xxx.se', port=443): Max retries exceeded with url: /CookieAuth.dll?GetLogon?curl=Z2F&reason=0&formdir=3 (Caused by <class 'socket.error'>: [Errno 10060] A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond)

UPDATE: I noticed that I was providing wrong username/password. Once that was updated I get a HTTP-200 response with the above code, but when I try to access any internal site I get a HTTP 401 response. Why Is this happening? What is wrong with the above code? Should I be using the cookies somehow?

Community
  • 1
  • 1
theAlse
  • 5,577
  • 11
  • 68
  • 110

1 Answers1

0

TMG can be notoriously fussy about what types of connections it blocks. The next step is to find out why TMG is blocking your connection attempts.

If you have access to the TMG server, log in to it, start the TMG management user-interface (I can't remember what it is called) and have a look at the logs for failed requests coming from your IP address. Hopefully it should tell you why the connection was denied.

It seems you are attempting to connect to it over an intranet. One way I've seen it block connections is if it receives them from an address it considers to be on its 'internal' network. (TMG has two network interfaces as it is intended to be used between two networks: an internal network, whose resources it protects from threats, and an external network, where threats may come from.) If it receives on its external network interface a request that appears to have come from the internal network, it assumes the IP address has been spoofed and blocks the connection. However, I can't be sure that this is the case as I don't know what this TMG server's internal network is set up as nor whether your machine's IP address is on this internal network.

Luke Woodward
  • 63,336
  • 16
  • 89
  • 104
  • I am trying to access the intranet from an external IP address. If I try this with chrome, FF and IE I can simply enter my name/pass and proceed, I don´t understand what the difference could be using python! – theAlse Feb 22 '13 at 13:10
  • I'm not a TMG expert, just someone who has set up TMG + UAG (Unified Access Gateway) in a test environment. As I said, the next step is to consult the TMG logs. They should tell you why the connection is being blocked. – Luke Woodward Feb 22 '13 at 19:51