Send data with Scapy - hex

I want to send payload which is in the form of hex value with Scapy, the hex format is like this: \xe3\x00\xfa and go on but the hex value I have is like x0ba324de ... How can I convert it to the group of two by two with \x in front of it?

You can try that (using python):
>>> x = '0ba324de'
>>> y = x.decode('hex')
>>> y
'\x0b\xa3$\xde'
and to revert back:
>>> y.encode('hex')
'0ba324de'

Related

Finding power of exponent without known base in python

From an exponent xa, I am trying to get 'a' without a known value of 'x' using logarithm law logx(xa) = a
Originally I tried:
from sympy import *
x = Symbol('x')
print(log(x,x))
Which returns 1 as expected. But when raised to any other power, does not give single value e.g.
print(log(x**2,x))
Which returns:
log(x**2)/log(x)
instead of 2.
Is there any alternative way to get the exponent or fix to my original code?
If you use expand it will give you the result you are looking for. In addition, you can just get the exponent directly:
>>> log(x**2,x).expand() # or expand_log(log(x**2,x))
2
>>> (x**2).exp
2
>>> (x**2).as_base_exp()[1] # works for x**1, too, which is not a Pow

I have a Json formatted sales data to make VEC or VAR for time series

I have a dataset of daily sales number from e-commerce to apply VEC and VAR models on it.
The csv has only 2 columns as "data.event" and "data.lastUpdate".
The "data.lastUpdate" column is the date. But in the format of
"2017-04-10T06:22:33.230Z". First I need to convert it into YMD format. I did it with string slicing. All pieces of advice are welcome if you know a better way.
But the real problem is with the first column "data.event". The column has a title but in the column, there are the numbers of sales for each platform(Android, iOS, Rest, Total). I want to separate all this into new columns according to platforms and of course the total numbers. The sample lines are as below. How can I convert the lines into separated columns?
0 - {"ANDROID":6106,"REST":3322,"IOS":3974,"TOTAL"... 2017-04-10T06:22:33.230Z
10 - {"ANDROID":9,"TOTAL":9} 2017-03-31T05:28:23.081Z
The output I want to get is simply like:
Date Total Android Ios
25/6/2018 35757 12247 9065
24/6/2018 18821 7582 5693
Since this is the first time that I use stackoverflow sorry for my bad body.
Thanks in advance.
convert it into YMD format ... if you know a better way
The usual strptime / loads idioms would be to use:
$ python
>>> import datetime as dt
>>> stamp = '2017-04-10T06:22:33.230Z'
>>> dt.datetime.strptime(stamp, '%Y-%m-%dT%H:%M:%S.%fZ')
datetime.datetime(2017, 4, 10, 6, 22, 33, 230000)
>>>
and
>>> import json
>>> csv_event = '{"ANDROID":9,"TOTAL":9}'
>>> d = json.loads(csv_event)
>>> d['ANDROID']
9
>>> d['TOTAL']
9
>>>

ord() Function or ASCII Character Code of String with Z3 Solver

How can I convert a z3.String to a sequence of ASCII values?
For example, here is some code that I thought would check whether the ASCII values of all the characters in the string add up to 100:
import z3
def add_ascii_values(password):
return sum(ord(character) for character in password)
password = z3.String("password")
solver = z3.Solver()
ascii_sum = add_ascii_values(password)
solver.add(ascii_sum == 100)
print(solver.check())
print(solver.model())
Unfortunately, I get this error:
TypeError: ord() expected string of length 1, but SeqRef found
It's apparent that ord doesn't work with z3.String. Is there something in Z3 that does?
The accepted answer dates back to 2018, and things have changed in the mean time which makes the proposed solution no longer work with z3. In particular:
Strings are now formalized by SMTLib. (See https://smtlib.cs.uiowa.edu/theories-UnicodeStrings.shtml)
Unlike the previous version (where strings were simply sequences of bit vectors), strings are now sequences unicode characters. So, the coding used in the previous answer no longer applies.
Based on this, the following would be how this problem would be coded, assuming a password of length 3:
from z3 import *
s = Solver()
# Ord of character at position i
def OrdAt(inp, i):
return StrToCode(SubString(inp, i, 1))
# Adding ascii values for a string of a given length
def add_ascii_values(password, len):
return Sum([OrdAt(password, i) for i in range(len)])
# We'll have to force a constant length
length = 3
password = String("password")
s.add(Length(password) == length)
ascii_sum = add_ascii_values(password, length)
s.add(ascii_sum == 100)
# Also require characters to be printable so we can view them:
for i in range(length):
v = OrdAt(password, i)
s.add(v >= 0x20)
s.add(v <= 0x7E)
print(s.check())
print(s.model()[password])
Note Due to https://github.com/Z3Prover/z3/issues/5773, to be able to run the above, you need a version of z3 that you downloaded on Jan 12, 2022 or afterwards! As of this date, none of the released versions of z3 contain the functions used in this answer.
When run, the above prints:
sat
" #!"
You can check that it satisfies the given constraint, i.e., the ord of characters add up to 100:
>>> sum(ord(c) for c in " #!")
100
Note that we no longer have to worry about modular arithmetic, since OrdAt returns an actual integer, not a bit-vector.
2022 Update
Below answer, written back in 2018, no longer applies; as strings in SMTLib received a major update and thus the code given is outdated. Keeping it here for archival purposes, and in case you happen to have a really old z3 that you cannot upgrade for some reason. See the other answer for a variant that works with the new unicode strings in SMTLib: https://stackoverflow.com/a/70689580/936310
Old Answer from 2018
You're conflating Python strings and Z3 Strings; and unfortunately the two are quite different types.
In Z3py, a String is simply a sequence of 8-bit values. And what you can do with a Z3 is actually quite limited; for instance you cannot iterate over the characters like you did in your add_ascii_values function. See this page for what the allowed functions are: https://rise4fun.com/z3/tutorialcontent/sequences (This page lists the functions in SMTLib parlance; but the equivalent ones are available from the z3py interface.)
There are a few important restrictions/things that you need to keep in mind when working with Z3 sequences and strings:
You have to be very explicit about the lengths; In particular, you cannot sum over strings of arbitrary symbolic length. There are a few things you can do without specifying the length explicitly, but these are limited. (Like regex matches, substring extraction etc.)
You cannot extract a character out of a string. This is an oversight in my opinion, but SMTLib just has no way of doing so for the time being. Instead, you get a list of length 1. This causes a lot of headaches in programming, but there are workarounds. See below.
Anytime you loop over a string/sequence, you have to go up to a fixed bound. There are ways to program so you can cover "all strings upto length N" for some constant "N", but they do get hairy.
Keeping all this in mind, I'd go about coding your example like the following; restricting password to be precisely 10 characters long:
from z3 import *
s = Solver()
# Work around the fact that z3 has no way of giving us an element at an index. Sigh.
ordHelperCounter = 0
def OrdAt(inp, i):
global ordHelperCounter
v = BitVec("OrdAtHelper_%d_%d" % (i, ordHelperCounter), 8)
ordHelperCounter += 1
s.add(Unit(v) == SubString(inp, i, 1))
return v
# Your original function, but note the addition of len parameter and use of Sum
def add_ascii_values(password, len):
return Sum([OrdAt(password, i) for i in range(len)])
# We'll have to force a constant length
length = 10
password = String("password")
s.add(Length(password) == 10)
ascii_sum = add_ascii_values(password, length)
s.add(ascii_sum == 100)
# Also require characters to be printable so we can view them:
for i in range(length):
v = OrdAt(password, i)
s.add(v >= 0x20)
s.add(v <= 0x7E)
print(s.check())
print(s.model()[password])
The OrdAt function works around the problem of not being able to extract characters. Also note how we use Sum instead of sum, and how all "loops" are of fixed iteration count. I also added constraints to make all the ascii codes printable for convenience.
When you run this, you get:
sat
":X|#`y}###"
Let's check it's indeed good:
>>> len(":X|#`y}###")
10
>>> sum(ord(character) for character in ":X|#`y}###")
868
So, we did get a length 10 string; but how come the ord's don't sum up to 100? Now, you have to remember sequences are composed of 8-bit values, and thus the arithmetic is done modulo 256. So, the sum actually is:
>>> sum(ord(character) for character in ":X|#`y}###") % 256
100
To avoid the overflows, you can either use larger bit-vectors, or more simply use Z3's unbounded Integer type Int. To do so, use the BV2Int function, by simply changing add_ascii_values to:
def add_ascii_values(password, len):
return Sum([BV2Int(OrdAt(password, i)) for i in range(len)])
Now we'd get:
unsat
That's because each of our characters has at least value 0x20 and we wanted 10 characters; so there's no way to make them all sum up to 100. And z3 is precisely telling us that. If you increase your sum goal to something more reasonable, you'd start getting proper values.
Programming with z3py is different than regular programming with Python, and z3 String objects are quite different than those of Python itself. Note that the sequence/string logic isn't even standardized yet by the SMTLib folks, so things can change. (In particular, I'm hoping they'll add functionality for extracting elements at an index!).
Having said all this, going over the https://rise4fun.com/z3/tutorialcontent/sequences would be a good start to get familiar with them, and feel free to ask further questions.

shift operation with hexadecimal output

I have known till date that %x prints in hexadecimals
Now, when I write printf("%x", -1<<4),the output is fffffff0
But when I write printf("%x", 5<<2), the output is 14
Why does the second output a decimal number?
14 is part of hexadecimal characters set, it's just the result of :
floor(20/16) and 20%16

AS3: How to convert ascii code to character actionscript

I want to create a board class from canvas, which will
allow to track click position on it in coordinates like A2, where
A...D is Y coordinate in some scale
and 1...3 is X coordinate
For example
see image http://img.skitch.com/20091001-k6ybfehid6y8irad36tbsiif15.jpg
What I want to create is a kind of convertor from canvas localX and localY to my
new coordinates like A2.
I am thinking of implementing if condition this way
if (0.4 - x*size(from 1-3 here)/canvas.width <= X <= 0.4 + x*size(from 1-3 here)/canvas.width)
X = x;
This way I can assigned needed coordinates in X range. e.g. 1, 2 ,3 etc
But what to do with alphanumeric range. (if for example I want to make it extensible)...
Maybe there is a way to convert ASCII to char? Pls. suggest your solution
The same way as in JavaScript: fromCharCode. If y is an integer starting at 1 for A:
String.fromCharCode(64+y)+x
you can use the function fromCharCode in String class to do that.
for example: String.fromCharCode(ascii code);

Resources