Client Hello data printed out in ASCII - python

I am trying to extract some Client Hello information from a network packet.
I am actually printing some values just for testing.
def parse_client_hello(handshake):
if isinstance(handshake.data, dpkt.ssl.TLSClientHello):
client = dpkt.ssl.TLSClientHello(str(handshake.data))
print(client.random)
print(client.version)
The result is as the above :
Is the printed out data represented in ASCII?
Why is the data printed out in ASCII when in fact the captured packet is in binary?
How can I print it in another form , for ex. in hexadecimal?
Thank you!

New in Python version 3.5: bytes.hex():
...
print(client.random.hex())
print(client.version)
If you're on anything older than that, binascii.hexlify will work:
from binascii import hexlify
...
...
print(hexlify(client.random))
print(client.version)
Or a bilingual solution:
if hasattr(bytes, 'hex'):
b2h = bytes.hex
else:
from binascii import hexlify as b2h
...
...
print(b2h(client.random))
print(client.version)

Related

Read JSON data from UTF-8 encoded byte string

I have a script that sends a JSON UTF-8 encoded Byte string to a socket. (A github project: https://github.com/alios/raildriver). Now I'm writing the python script that needs to read the incoming data. Right now I can receive the data and print it to the terminal. With the following script: https://www.binarytides.com/code-telnet-client-sockets-python/
Output:
data = '{"Current": 117.42609405517578, "Accelerometer": -5.394751071929932, "SpeedometerKPH": 67.12493133544922, "Ammeter": 117.3575210571289, "Amp": 117.35590362548828, "Acceleration": -0.03285316377878189, "TractiveEffort": -5.394751071929932, "Effort": 48.72163772583008, "RawTargetDistance": 3993.927734375, "TargetDistanceBar": 0.9777777791023254, "TargetDistanceDigits100": -1.0, "TargetDistanceDigits1000": -1.0}'
The problem is that I can't find how to read the JSON array. For example read "Ammeter" and return its value 117.357521057289 to a new variable.
All the data is being received in the variable data
The code I have right now:
decodedjson = data.decode('utf-8')
dumpedjson = json.dumps(decodedjson)
loadedjson = json.loads(dumpedjson)
Can you please help me?
You are encoding to JSON then decoding again. SImply not encode, remove the second line:
decodedjson = data.decode('utf-8')
loadedjson = json.loads(decodedjson)
If you are using Python 3.6 or newer, you don't actually have to decode from UTF-8, as the json.loads() function knows how to deal with UTF-encoded JSON data directly. The same applies to Python 2:
loadedjson = json.loads(data)
Demo using Python 3.7:
>>> data = b'{"Current": 117.42609405517578, "Accelerometer": -5.394751071929932, "SpeedometerKPH": 67.12493133544922, "Ammeter": 117.3575210571289, "Amp": 117.35590362548828, "Acceleration": -0.03285316377878189, "TractiveEffort": -5.394751071929932, "Effort": 48.72163772583008, "RawTargetDistance": 3993.927734375, "TargetDistanceBar": 0.9777777791023254, "TargetDistanceDigits100": -1.0, "TargetDistanceDigits1000": -1.0}'
>>> loadedjson = json.loads(data)
>>> loadedjson['Ammeter']
117.3575210571289

CryptoJS HmacSHA256 Encryption results differ from Python

CryptoJS encrypted string can pass different parameters in python can only pass a string? How to implement the second CryptoJS implementation in python
,how to get clientKey2,This will only give the first result.Thanks!
> saltedPassword=CryptoJS.PBKDF2("key", "salt", {keySize: 8,iterations:500,hasher: CryptoJS.algo.SHA256});
> clientKey1=CryptoJS.HmacSHA256(saltedPassword.toString(), "Client Key")
> clientKey2=CryptoJS.HmacSHA256(saltedPassword, "Client Key")
> clientKey1.toString()
> "857ef8988876a3bb6bcadb85ca257787074e73e830d7dc14c1f838ba46aef1f5"
> clientKey2.toString()
> "9a8574da9b276ee1162dcb92071df587f4513bc03060bda1e9b3897d46233416"
> saltedPassword.toString()
> "6e441ccd26e6b35198b4b17457dc0266d36b751d0062b5850b0e302ceb1d6dcc"
i use this way can get clientKey1,
import hashlib
import hmac
def HmacSHA256(k,v):
message = bytes(k).encode('utf-8')
secret = bytes(v).encode('utf-8')
signature = hmac.new(secret, message, digestmod=hashlib.sha256).hexdigest()
return signature
signature = HmacSHA256("6e441ccd26e6b35198b4b17457dc0266d36b751d0062b5850b0e302ceb1d6dcc","Client Key")
print signature
How to get the second result in Python,Thanks!
To get the desired clientKey2 you need to encode the hex digits of your saltedPassword string to bytes. One way to do that which works on both Python 2 & Python 3 is to use binascii.unhexlify.
Your HmacSHA256 function is a bit odd. It won't work on Python 3, since bytes objects don't have an .encode method. In Python 2, bytes is just a synonym for str.
Anyway, here's some code that works on both Python 2 & Python 3.
from __future__ import print_function
import hashlib
import hmac
import binascii
key = "Client Key".encode()
salted = "6e441ccd26e6b35198b4b17457dc0266d36b751d0062b5850b0e302ceb1d6dcc"
raw = binascii.unhexlify(salted)
signature = hmac.new(key, salted.encode(), digestmod=hashlib.sha256).hexdigest()
print(signature)
signature = hmac.new(key, raw, digestmod=hashlib.sha256).hexdigest()
print(signature)
output
857ef8988876a3bb6bcadb85ca257787074e73e830d7dc14c1f838ba46aef1f5
9a8574da9b276ee1162dcb92071df587f4513bc03060bda1e9b3897d46233416
The output is identical on Python 2 & Python 3.
BTW, it would be simpler to do this task in Python 3, which makes a clean distinction between text and byte strings. Also, the Python 3 hashlib module has a pbkdf2 function.

Need a Python code to convert a secret key to base64 encoded

I have a secret key generated - A0[Bt"V59.xA-bKO|/A""/Z.!#Y:wfpR , need to convert this to base64 encoded format , Please help with the python code to do this.
For encoding to base64 use the base64 module and its function b64encode for that.
Example -
>>> import base64
>>> base64.b64encode("Hello")
'SGVsbG8='
>>> s = 'A0[Bt\"V59.xA-bKO|/A\"\"/Z.!#Y:wfpR'
>>> base64.b64encode(s)
'QTBbQnQiVjU5LnhBLWJLT3wvQSIiL1ouISNZOndmcFI='
For decoding , use the b64decode() function -
>>> import base64
>>> base64.b64decode('SGVsbG8=')
'Hello'
>>> base64.b64decode('QTBbQnQiVjU5LnhBLWJLT3wvQSIiL1ouISNZOndmcFI=')
'A0[Bt"V59.xA-bKO|/A""/Z.!#Y:wfpR'

Displaying scapy StreamSocket data

I want to be able to display hex data from a StreamSocket in scapy. This is my current scapy code
from scapy.all import socket, StreamSocket, Raw
import data_packets, returned_data
data = data_packets.client_to_server_init()
mysocket = socket.socket()
mysocket.connect((args.IP_Address, args.dPort))
myStream = StreamSocket(mysocket)
result=myStream.sr1(Raw(data))
result.show()
The result is similar to that shown below
###[ Raw ]###
load = '\x00\x00\x02\x99R\x92\x05R\xec\x02R\xe9\x02R...
Basically I want everything to be in hex whereas currently the returned data is coming back as a mix of hex and ASCII characters as you can see from the output above (Notice the capital R's that are mixed in between, those are \x52 being converted to ASCII)
NOTE: I've tried creating my own show() function but it hasn't changed anything. That code is below
def my_show(packet):
for f in packet.fields_desc:
fvalue = packet.getfieldval(f.name)
reprval = f.i2repr(packet,fvalue)
return "%s" % (reprval)
You could try taking a page from Python: print a string as hex bytes?
and use encode('hex') on your outpt as part of my_show:
return ':'.join(x.encode('hex') for x in fvalue)

Encoding/decoding works in browser, but not in terminal

Here's my code:
import urllib
print urllib.urlopen('http://www.indianexpress.com/news/heart-of-the-deal/811626/').read().decode('iso-8859-1')
When I view the page in Firefox, the text is displayed correctly. However, on the terminal, I see issues with character encoding.
Here are some malformed output examples:
long-term in
Indias
no-go areas
How can I fix this?
Try this (ignore unknown chars)
import urllib
url = 'http://www.indianexpress.com/news/heart-of-the-deal/811626/'
print urllib.urlopen(url).read().decode('iso-8859-1').encode('ascii','ignore')
You need to use the actual charset sent by the server instead of always assuming it's ISO 8859-1. Using a capable HTML parser such as Beautiful Soup can help.
The web-page lies; it is encoded in cp1252 aka windows-1252, NOT in ISO-8859-1.
>>> import urllib
>>> guff = urllib.urlopen('http://www.indianexpress.com/news/heart-of-the-deal/811626/').read()
>>> uguff = guff.decode('latin1')
>>> baddies = set(c for c in uguff if u'\x80' <= c < u'\xa0')
>>> baddies
set([u'\x93', u'\x92', u'\x94', u'\x97'])

Categories