How can I extract HTML code with Scapy? - python-2.7

I recently began to use the scapy library for Python 2.x I found there to be minimal documentation on the sniff() function. I began to play around with it and found that I can veiw TCP packets at a very low level. So far I have only found informational data. For example:
Here is what I put in the scapy terminal:
A = sniff(filter="tcp and host 216.58.193.78", count=2)
This is a request to google.com asking for the homepage:
<Ether dst=e8:de:27:55:17:f3 src=00:24:1d:20:a6:1b type=0x800 |<IP version=4L ihl=5L tos=0x0 len=60 id=46627 flags=DF frag=0L ttl=64 proto=tcp chksum=0x2a65 src=192.168.0.2 dst=216.58.193.78 options=[] |<TCP sport=54036 dport=www seq=2948286264 ack=0 dataofs=10L reserved=0L flags=S window=29200 chksum=0x5a62 urgptr=0 options=[('MSS', 1460), ('SAckOK', ''), ('Timestamp', (389403, 0)), ('NOP', None), ('WScale', 7)] |>>>
Here is the response:
<Ether dst=00:24:1d:20:a6:1b src=e8:de:27:55:17:f3 type=0x800 |<IP version=4L ihl=5L tos=0x0 len=60 id=42380 flags= frag=0L ttl=55 proto=tcp chksum=0x83fc src=216.58.193.78 dst=192.168.0.2 options=[] |<TCP sport=www dport=54036 seq=3087468609 ack=2948286265 dataofs=10L reserved=0L flags=SA window=42540 chksum=0xecaf urgptr=0 options=[('MSS', 1430), ('SAckOK', ''), ('Timestamp', (2823173876, 389403)), ('NOP', None), ('WScale', 7)] |>>>
Using this function, is there a way that I can extract HTML code from the response?
Also, what do those packets look like?
And finaly, Why are both of these packets nearly identical?

The segments in your example are "nearly identical" because they are the TCP SYN and SYN-ACK segments which are part of the TCP handshake, HTTP request and response comes after that during the connection (usually when in ESTABLISHED state except when TCP Fast Open option is used) so you need to look at segments after the handshake to get the data you are interested in.
SYN
C ---------------> S
SYN-ACK
C <--------------- S
ACK
C ---------------> S
HTTP request
C ---------------> S
ACK
C <--------------- S
HTTP response
C <--------------- S <= Here is the server's answer
ACK
C ---------------> S
...
You can use Scapy's Raw layer to extract data above TCP in your case (e.g. pkt[Raw])

Have you tried using scapy-http? It's a great scapy extension that helps with this exact issue

Related

Sofia SIP, response to Invite with no SDP

My Sofia SIP application responds to Invite with no SDP like this
nua_respond(_nh, 200, "OK",
SIPTAG_CONTENT_TYPE_STR("application/sdp"),
NUTAG_OFFER_SENT(1),
SOATAG_USER_SDP_STR("m=audio 1 RTP/AVP 0\n"
"a=rtpmap:0 PCMU/8000"),
TAG_NULL());
However, ACK after this one is not processed, and I see "ACK Timeout". Are there any tags I'm missing?

<Raw> tag in scapy, how do I decode the content?

I used Scapy to sniff for TCP packets in an attempt to get HTML code from the response.
>>>A = sniff(filter='tcp')
(Then I refreshed a webpage)
>>>A
<Sniffed: TCP:148 UDP:0 ICMP:0 Other:0>
>>>A.show()
0000 Ether / IP / TCP 192.168.0.2:60210 > 74.125.28.95:www S
(obviously there were more than this, this is just an example)
>>>A[28]
<Ether dst=00:24:1d:20:a6:1b src=e8:de:27:55:17:f3 type=IPv4 |<IP version=4L ihl=5L tos=0x0 len=1056 id=49239 flags= frag=0L ttl=45 proto=tcp chksum=0xa1fa src=74.125.28.95 dst=192.168.0.2 options=[] |<TCP sport=www dport=60210 seq=956108121 ack=2048727264 dataofs=8L reserved=0L flags=PA window=343 chksum=0x3df5 urgptr=0 options=[('NOP', None), ('NOP', None), ('Timestamp', (644692201, 2737592))] |<Raw load='\x1f\x8b\x08\x00\x00\x00\x00\x00\x00\x00\xed\x97\xd9\x92\xa2H\x14\x86\xef\xeb)\xb8\xab\xaa\x9e\xa6Hp\x01\xec\x9ba\x11AE\\(\x15o\x0cD6E#6\x95\x89y\xf7\x01\xb5jz&\xc2\xb15\xaa\'\xc4\xea\x1b3\x0e\'3I\xfe/\xf2?G\xe4\x0b\xe4\xa8\x91\xed\xc2\xfa6\x82\xbe \x0f\xbf\x1b\x9e\x1b\xc1\x86\xaa\xe9\xd0\x1f\x0f\x10t\x8cV\xb6\xb3\xabA\x8f\x03/\x0e\xb2\x04\xe3\xcdu\xa8\x1bx\x8f\xdf\xdef\x84\xd1\xce\xd1k\x90\xeb\x05+\xd5y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xcd\x1d\xc6\x85\x8bJ\xb8(\xd5Sg\xec\xaf\x87\xe3~\xcb\xfbeBWw\xb9\xe7\xd8\xbd\xcd\xfb\xc0B\xc2\xa4"6V\xa85\xa1\xb1\x96:\x0e\x83\x0e1\x1akT\xf1\\\xe9\xb6U\x96\xc6-y\xa2t;Q\x03\xdd\x89\xf4Ti\x0e\xd6\xd9\t\xee\xd7\xa6n\x9bF\xa3\xd7\x8aS1v\xb5\x004i\x1c\x05\x1b\x15\x9bw\x8b\xd4<\xfd\x05\xf0Y\xff2\xe8!\x00\x00' |>>>>
The Raw tag contained this:
<Raw load='\x1f\x8b\x08\x00\x00\x00\x00\x00\x00\x00\xed\x97\xd9\x92\xa2H\x14\x86\xef\xeb)\xb8\xab\xaa\x9e\xa6Hp\x01\xec\x9ba\x11AE\\(\x15o\x0cD6E#6\x95\x89y\xf7\x01\xb5jz&\xc2\xb15\xaa\'\xc4\xea\x1b3\x0e\'3I\xfe/\xf2?G\xe4\x0b\xe4\xa8\x91\xed\xc2\xfa6\x82\xbe \x0f\xbf\x1b\x9e\x1b\xc1\x86\xaa\xe9\xd0\x1f\x0f\x10t\x8cV\xb6\xb3\xabA\x8f\x03/\x0e\xb2\x04\xe3\xcdu\xa8\x1bx\x8f\xdf\xdef\x84\xd1\xce\xd1k\x90\xeb\x05+\xd5y\x7f\xba\xd1m\xd3\x8ajP\t\x80\xfcY\x18h5\xc8\xf14\xd5y\xfa\xf7NP;\x9f\xf9\xf8\xfc\xf5\x9f\xf9<\x9de\xe1\xf7l\x1c8OV\x14\xf95\x04\xc9\xdf\x10\xbe\x98a\x94\x9d^{\xd1\xbc\x15\x12"\xe1~\x95\x96\xad\xf2\x03\x0fI\xaa\x88\xa3\xaf\x93R\x02\xef\xe4\xb0\xd9ap\x97K\xdd\x81\xb8\x96\xfb\xc3\x84Nz\x82\x86\xa68\xa1M\xa7\xb1Gk;\xe1e\xe3\x19\x06\xf6\x9c\x9d<\xfb\x86\xe8\xe9q\x1f>>\x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03\xc0\xaa\xe7k\xc6#_\xd93\xcf\x99\x9f$\xf7\xfd\x84\x0f\xa9\x1c\x8c\xa3l\x86\x92\xc5\xf6\xd7X2\xf3\x1b}y\x82\xcffa\xf1\x8c\xe8&\xc5\xe5Z\x9c\x85\xca\xac\xdc\r^\'\xa3t\xe8W\xb9\xc8o\x10\xf7kI7\t\x81\x918wTj.\x81\x01\xc8t4U\x1c\xb7\x8aS\xda\xa75\';R\x1d[\xbb\xba\xa1\x85\x84\xfd\xfa\xff\xeek\x85\xab:[\xc3\x97\x87|\x8bX\x87\xe3\x99\xa0\xcb\xbc\x1c\xb8=\xbeZ\xc7\x89F\xd4\xf7K\x8e\xa5Y\xb1\xcf4;\x1b"\xaa\\z\x85\xfeO\x7f*\x98\xbem\x7f[&\xf1\x88,{l\xac\x12-\x83j\x93\x06\\\xbf\x03\x8b*\x18\x07\x91Z,w\xb6\xbe\x19)\xc1h\xa2U\x80#X-\xfc\xb2?\xd0\x9f\xc0\xa5\xce\xf7\xbc\xe7\xb8]\x87L\xc4\x9as\x1c\xf5\xbaM\xab\xdd\xf2A:\x8f\xe4\xc4\x13q\x85_\x979\x93\x17\x00F\x13AW\xd3Z\x80k\xf6\x8agM7%j\x19D\xe6\x96$\x17+\xa5\x91L\xb7\xa9\x12o\xe6h\xf0z\xbf~tS\xe2o\xcd\x1d\xc6\x85\x8bJ\xb8(\xd5Sg\xec\xaf\x87\xe3~\xcb\xfbeBWw\xb9\xe7\xd8\xbd\xcd\xfb\xc0B\xc2\xa4"6V\xa85\xa1\xb1\x96:\x0e\x83\x0e1\x1akT\xf1\\\xe9\xb6U\x96\xc6-y\xa2t;Q\x03\xdd\x89\xf4Ti\x0e\xd6\xd9\t\xee\xd7\xa6n\x9bF\xa3\xd7\x8aS1v\xb5\x004i\x1c\x05\x1b\x15\x9bw\x8b\xd4<\xfd\x05\xf0Y\xff2\xe8!\x00\x00' |>>>>
What type of encoding is this? & How do I decode it?
0x1f8b0800, the beginning of the Raw field, if the beginning of a tar file (http://digital-forensics.sans.org/media/hex_file_and_regex_cheat_sheet.pdf)
Might want to try untarring the contents of the hexdump

How to extrapolate data from an nmap scan result

I'm still quite new to Python and I'm currently looking at network scanning for available hosts. With my current code, I can search an IP range to determine if hosts are available or not. However, how can I restrict what information the nmap scan results show me, or is there a function I need to be using to only show the host IP address, scan time and if its available?
#!/usr/bin/env python
import nmap
import sys
nm = nmap.PortScannerAsync()
def callback_result(host, scan_result):
print '------------------'
print host, scan_result
try:
nm.scan('192.168.1.86-87', arguments='-O -v', callback=callback_result)
while nm.still_scanning():
print('<<< Scanning >>>')
nm.wait(2)
except KeyboardInterrupt:
print 'Cancelling current operation'
sys.exit()
except KeyError as e:
pass
This provides the output which is broad and contains too much information;
192.168.1.87 {'nmap': {'scanstats': {'uphosts': u'0', 'timestr': u'Wed Apr 8 13:28:29 2015', 'downhosts': u'1', 'totalhosts': u'1', 'elapsed': u'3.77'},
'scaninfo': {u'tcp': {'services': u'1,3-4,6-7,9,13,17,19-26,30,32-33,37,42-43,49,53,70,79-85,88-90,99-100,106,109-111,113,119,125,135,139,143-
144,146,161,163,179,199,211-212,222,254-
256,259,264,280,301,306,311,340,366,389,406-407,416-417,425,427,443-445,458,464-
465,481,497,500,512-515,524,541,543-545,548,554-555,563,587,593,616-617,625,631,636,646,648,666-
668,683,687,691,700,705,711,714,720,722,726,749,765,777,783,787,800-
801,808,843,873,880,888,898,900-903,911-912,981,987,990,992-993,995,999-
1002,1007,1009-1011,1021-1100,1102,1104-1108,1110-1114,1117,1119,1121-
1124,1126,1130-1132,1137-1138,1141,1145,1147-1149,1151-1152,1154,1163-
1166,1169,1174-1175,1183,1185-1187,1192,1198-1199,1201,1213,1216-1218,1233-1234,1236,1244,1247-1248,1259,1271-1272,1277,1287,1296,1300-1301,1309-1311,1322,1328,1334,1352,1417,1433-1434,1443,1455,1461,1494,1500-1501,1503,1521,1524,1533,1556,1580,1583,1594,1600,1641,1658,1666,1687-1688,1700,1717-1721,1723,1755,1761,1782-1783,1801,1805,1812,1839-1840,1862-1864,1875,1900,1914,1935,1947,1971-1972,1974,1984,1998-2010,2013,2020-2022,2030,2033-2035,2038,2040-2043,2045-2049,2065,2068,2099-2100,2103,2105-2107,2111,2119,2121,2126,2135,2144,2160-2161,2170,2179,2190-2191,2196,2200,2222,2251,2260,2288,2301,2323,2366,2381-2383,2393-2394,2399,2401,2492,2500,2522,2525,2557,2601-2602,2604-2605,2607-2608,2638,2701-2702,2710,2717-2718,2725,2800,2809,2811,2869,2875,2909-2910,2920,2967-2968,2998,3000-3001,3003,3005-3007,3011,3013,3017,3030-3031,3052,3071,3077,3128,3168,3211,3221,3260-3261,3268-3269,3283,3300-3301,3306,3322-3325,3333,3351,3367,3369-3372,3389-3390,3404,3476,3493,3517,3527,3546,3551,3580,3659,3689-3690,3703,3737,3766,3784,3800-3801,3809,3814,3826-3828,3851,3869,3871,3878,3880,3889,3905,3914,3918,3920,3945,3971,3986,3995,3998,4000-4006,4045,4111,4125-4126,4129,4224,4242,4279,4321,4343,4443-4446,4449,4550,4567,4662,4848,4899-4900,4998,5000-5004,5009,5030,5033,5050-5051,5054,5060-5061,5080,5087,5100-5102,5120,5190,5200,5214,5221-5222,5225-5226,5269,5280,5298,5357,5405,5414,5431-5432,5440,5500,5510,5544,5550,5555,5560,5566,5631,5633,5666,5678-5679,5718,5730,5800-5802,5810-5811,5815,5822,5825,5850,5859,5862,5877,5900-5904,5906-5907,5910-5911,5915,5922,5925,5950,5952,5959-5963,5987-5989,5998-6007,6009,6025,6059,6100-6101,6106,6112,6123,6129,6156,6346,6389,6502,6510,6543,6547,6565-6567,6580,6646,6666-6669,6689,6692,6699,6779,6788-6789,6792,6839,6881,6901,6969,7000-7002,7004,7007,7019,7025,7070,7100,7103,7106,7200-7201,7402,7435,7443,7496,7512,7625,7627,7676,7741,7777-7778,7800,7911,7920-7921,7937-7938,7999-8002,8007-8011,8021-8022,8031,8042,8045,8080-8090,8093,8099-8100,8180-8181,8192-8194,8200,8222,8254,8290-8292,8300,8333,8383,8400,8402,8443,8500,8600,8649,8651-8652,8654,8701,8800,8873,8888,8899,8994,9000-9003,9009-9011,9040,9050,9071,9080-9081,9090-9091,9099-9103,9110-9111,9200,9207,9220,9290,9415,9418,9485,9500,9502-9503,9535,9575,9593-9595,9618,9666,9876-9878,9898,9900,9917,9929,9943-9944,9968,9998-10004,10009-10010,10012,10024-10025,10082,10180,10215,10243,10566,10616-10617,10621,10626,10628-10629,10778,11110-11111,11967,12000,12174,12265,12345,13456,13722, 13782-
13783,14000,14238,14441-14442,15000,15002-15004,15660,15742,16000-
16001,16012,16016,16018,16080,16113,16992-16993,17877,17988,18040,18101,18988,19101,19283,19315,19350,19780,19801,19842,20
000,20005,20031,20221-20222,20828,21571,22939,23502,24444,24800,25734-
25735,26214,27000,27352-27353,27355-
27356,27715,28201,30000,30718,30951,31038,31337,32768-32785,33354,33899,34571-
34573,35500,38292,40193,40911,41511,42510,44176,44442-
44443,44501,45100,48080,49152-49161,49163,49165,49167,49175-49176,49400,49999-50003,50006,50300,50389,50500,50636,50800,51103,51493,52673,52822,52848,52869,54
045,54328,55055-55056,55555,55600,56737-
56738,57294,57797,58080,60020,60443,61532,61900,62078,63331,64623,64680,65000,65
129,65389', 'method': u'syn'}}, 'command_line': u'nmap -oX - -O -v
192.168.1.87'}, 'scan': {u'192.168.1.87': {'status': {'state': u'down',
'reason': u'no-response'}, 'hostname': '', 'vendor': {}, 'addresses': {u'ipv4':
u'192.168.1.87'}}}}
You can address this from two directions: what actions Nmap takes, and what you do with the output.
The Nmap options in your program (-O -v) instruct Nmap to do the following things:
Increase verbosity (-v). This doesn't matter for python-nmap because it uses the XML output, which doesn't change based on verbosity.
Check if the host is up (default).
Check for a reverse-DNS name for the host (default).
Scan the top 1000 TCP ports on the host (default).
Fingerprint the host's OS based on TCP/IP stack quirks (-O).
If all you want is whether the host is up, you should leave off the -O and use some other options to turn off the other parts of Nmap's default behavior:
-n will turn off reverse-DNS name resolution.
-sn will turn off the port scan.
The scan information like time will always be printed.
Secondly, your callback function currently just prints the string representation of the scan object. If you want less output, then use string formatting to select the object attributes that you want to print.

APNS issue with django

I'm using the following project for enabling APNS in my project:
https://github.com/stephenmuss/django-ios-notifications
I'm able to send and receive push notifications on my production app fine, but the sandbox apns is having strange issues which i'm not able to solve. It's constantly not connecting to the push service. When I do manually the _connect() on the APNService or FeedbackService classes, I get the following error:
File "/Users/MyUser/git/prod/django/ios_notifications/models.py", line 56, in _connect
self.connection.do_handshake()
Error: [('SSL routines', 'SSL3_READ_BYTES', 'sslv3 alert handshake failure')]
I tried recreating the APN certificate a number of times and constantly get the same error. Is there anything else i'm missing?
I'm using the endpoints gateway.push.apple.com and gateway.sandbox.push.apple.com for connecting to the service. Is there anything else I should look into for this? I have read the following:
Apns php error "Failed to connect to APNS: 110 Connection timed out."
Converting PKCS#12 certificate into PEM using OpenSSL
Error Using PHP for iPhone APNS
Turns out Apple changed ssl context from SSL3 to TLSv1 in development. They will do this in Production eventually (not sure when). The following link shows my pull request which was accepted into the above project:
https://github.com/stephenmuss/django-ios-notifications/commit/879d589c032b935ab2921b099fd3286440bc174e
Basically, use OpenSSL.SSL.TLSv1_METHOD if you're using python or something similar in other languages.
Although OpenSSL.SSL.SSLv3_METHOD works in production, it may not work in the near future. OpenSSL.SSL.TLSv1_METHOD works in production and development.
UPDATE
Apple will remove SSL 3.0 support in production on October 29th, 2014 due to the poodle flaw.
https://developer.apple.com/news/?id=10222014a
I have worked on APN using python-django, for this you need three things URL, PORT and Certificate provided by Apple for authentication.
views.py
import socket, ssl, json, struct
theCertfile = '/tmp/abc.cert' ## absolute path where certificate file is placed.
ios_url = 'gateway.push.apple.com'
ios_port = 2195
deviceToken = '3234t54tgwg34g' ## ios device token to which you want to send notification
def ios_push(msg, theCertfile, ios_url, ios_port, deviceToken):
thePayLoad = {
'aps': {
'alert':msg,
'sound':'default',
'badge':0,
},
}
theHost = ( ios_url, ios_port )
data = json.dumps( thePayLoad )
deviceToken = deviceToken.replace(' ','')
byteToken = deviceToken.decode('hex') # Python 2
theFormat = '!BH32sH%ds' % len(data)
theNotification = struct.pack( theFormat, 0, 32, byteToken, len(data), data )
# Create our connection using the certfile saved locally
ssl_sock = ssl.wrap_socket( socket.socket( socket.AF_INET, socket.SOCK_STREAM ), certfile = theCertfile )
ssl_sock.connect( theHost )
# Write out our data
ssl_sock.write( theNotification )
# Close the connection -- apple would prefer that we keep
# a connection open and push data as needed.
ssl_sock.close()
Hopefully this would work for you.

Send m-search packets on all network interfaces

I am implementing a code through which i have to get devices connected to all network interfaces on my machine.
For this, i am first getting the ip of all network interfaces and then sending m-search command on them.
After 2.5 seconds port is stopped to listen.
But it is giving me some assertion error.
Code:
class Base(DatagramProtocol):
""" Class to send M-SEARCH message to devices in network and receive datagram
packets from them
"""
SSDP_ADDR = "239.255.255.250"
SSDP_PORT = 1900
MS = "M-SEARCH * HTTP/1.1\r\nHOST: {}:{}\r\nMAN: 'ssdp:discover'\r\nMX: 2\r\nST: ssdp:all\r\n\r\n".format(SSDP_ADDR, SSDP_PORT)
def sendMsearch(self):
""" Sending M-SEARCH message
"""
ports = []
for address in self.addresses:
ports.append(reactor.listenUDP(0, self, interface=address))
for port in ports:
for num in range(4):
port.write(Base.MS, (Base.SSDP_ADDR,Base.SSDP_PORT))
reactor.callLater(2.5, self.stopMsearch, port) # MX + a wait margin
def stopMsearch(self, port):
""" Stop listening on port
"""
port.stopListening()
Error:
Traceback (most recent call last):
File "work\find_devices.py", line 56, in sendMsearch
ports.append(reactor.listenUDP(0, self, interface=address))
File "C:\Python27\lib\site-packages\twisted\internet\posixbase.py", line 374, in listenUDP
p.startListening()
File "C:\Python27\lib\site-packages\twisted\internet\udp.py", line 172, in startListening
self._connectToProtocol()
File "C:\Python27\lib\site-packages\twisted\internet\udp.py", line 210, in _connectToProtocol
self.protocol.makeConnection(self)
File "C:\Python27\lib\site-packages\twisted\internet\protocol.py", line 709, in makeConnection
assert self.transport == None
AssertionError
Please tell what's wrong in this code and how to correct this.
Also on linux machines, if no device is found on network then it doesn't go to stopMsearch() why ?
A protocol can only have one transport. The loop:
for address in self.addresses:
ports.append(reactor.listenUDP(0, self, interface=address))
tries to create multiple UDP transports and associate them all with self - a single protocol instance.
This is what the assertion error is telling you. The protocol's transport must be None (ie, it must not have a transport). But on the second iteration through the loop, it already has a transport.
Try using multiple protocol instances instead.