python3 setup.py install
pycalc
Classic Int to hex with keyword to
>>> 12345 to hex
hex: 3039
Base64 to ascii
>>> ZXhhbXBsZQ== to ascii
Ascii: example
Force type with key word as
>>> "30 39" as hex to int
int: 12345
Hexadecimal with 0x prefix
>>> 0x2a
hex: 2a
Binary with 0b prefix
>>> 0x1010
bit: 1010
Base64
>>> ZXhhbXBsZQ==
base64: ZXhhbXBsZQ==
Interger
>>> 12345
int: 12345
>>> 1 + 1
int: 2
Math operation return value of type of the first operand
>>> 0x10 * 0b10
hex: 0x20
TODO: Find syntax for the following
- RSA
- private key generation
- public key generation
- Hash (MD5, SHA1, SHA256, ...)
- Caesar
- With key
- Bruteforce
- ASCII Caesar
- With key
- Bruteforce
- XOR
- With key
- Known-plaintext attack
- Xored plaintext attack (cf cryptopals)
Caesar
>>> '57 69 59 5f 5f 55 59 57 5b 69 57 68' as bytes caesar 10 to ascii
ascii: ascii_caesar
Xor bin file with known key
>>> tmp/file.crypt as binFile xor TheSecretKey write /tmp/out as binFile
bytes: \x41\x20\x62\x69\x6e...\x20\x66\x69\x6c\x65
TODO: Find syntax for the following
- URL encode / decode
- Double encode
- Base64
- Base32
- Hex
Url encode and decode
>>> urlEncode "' or 1=1 #"
ascii: %27%20or%201%3D1%20%23
>>> urlDecode %27%20or%201%3D1%20%23
ascii: ' or 1=1 #
Load data from file
>>> dir/file.txt as textFile
ascii: A text file
And from binary file
>>> dir/file.txt as binFile
bytes: \x41\x20\x62\x69\x6e\x20\x66\x69\x6c\x65
Save value in variable
>>> 1 + 1 in var1
int: 2 (var1)
Use value from varable
>>> $var1 * 0xa
int: 20
List all varables
>>> $
int: 2 (var1)
bin: 1010 (tmp)
unicode: a string (f)
List of possible data types:
- Int
- Ascii
- Unicode
- Bytes
- Bitarray / Bits
- Hex
- Octal ?
- binFile
- textFile
Modifiers affect a single data, it is not an operator
List of modifiers:
to
/.
as
in
List of possible operator:
+
-
/
*
xor
/^
rsa
- `abs'
factorial
/!
Operator like python function but it can take the first parameter at this left. This allow to chain operator
>>> abs -42
int: 42
Factorial
>>> ! 0b101 to int
int: 120
chaining
>>> -4 abs factorial
int: 24
To string operator, not the same as to ascii
>>> 6162 string
ascii: 6162
>>> 6162 to ascii
ascii: ab
Because to
will transform value to bytes before convert.
The interpretor read code from left to right but it is possible to use parenthesis to make action prioritary.
example:
>>> 42 * (a as hex - 8)
int: 84
To escape space it is possible to use the \
escape charactere.
Put text in "
also escape space.
If you want to escape the first sentence and you have forgot to place a "
at the start you can just put a "
at the end to the sentence and the interpretor with automatically place a "
at the start.
Un Type est une class Python qui hérite d'une classe particulière.
Idée de méthodes à redéfinir:
- toString: appelée quand une donnée de ce type est la valeur de retour
- format: appelé par as
- detect: appelé pour détecter automatiquement si un string est de ce type. Si
detect
retourneTrue
, alors la donnée est de ce type et la méthodeformat
est appelée. (Systeme de priorité) - toBytes and fromBytes: for default conversion
Un type aura un nom pour le différencier des autres types, par défaut il prendra le nom de sa classe mais il sera possible de le redéfinir.
Il faudrat ajouter ce type aux registres des types connus Exemple:
from pycalclib import registerType, BaseType
import re
class PCInt(BaseType):
name = 'Int'
def format(self, value):
return int(value)
def detect(self, value):
return re.test(r'(0|[1-9][0-9]*)', value) # ex: 12345 (/!\ 01 not work)
def toString(self, value):
return value
# Register an Object
registerType(Int())
Un *Operator est une classe Python qui hérite d'une classe particulière.
Une opération doit avoir un type par défaut pour effectuer son calcule. Il sera possible de spécifier un comportement en fonction d'un Type particulier.
Il faudra définir le symbol qui permet de reconnaitre l'operation, il pourrait y en avoir plusieurs (xor
et ^
)
Exemple:
from pycalclib import registerOperator, BaseOperator
class Addition(BaseOperator):
default = 'Int'
symbols = ['+']
def compute(self, first, second):
return first + second
@self.registerOp('Hex')
def hexOp(self, first, second):
return first + second
@self.registerOp('Ascii')
def asciiConcat(self, first, second):
return first + second
# Register an Object
registerOperator(Addition())
Une donnée sera stockée dans un objet Data.
Il contiendra:
- la valeur sous n'importe quelle forme
- le nom du type de cette valeur
Les variable seront stockées dans un dictionnaire avec :
- comme clé le nom de la variable
- comme valeur l'objet Data
Ex: convert int to base64, save in variable and write in text file
>>> 12345678 to base64 in myVar write /tmp/file.txt as textFile
base64: vGFO (myVar)
Cut operation:
- 12345678 to base64:
- Call
detect
of all Type and make list of possible Type: Here there is only 'Int' - Call format method of
Int
and save data in Data object as current value - Find modifier: is
to
for conversion - Find Type to convert: is
base64
- Find if there is specific method in
Int
to convert tobase64
- No specific method, so convert
Int
to default Type:Bytes
- Call
toBytes
method ofInt
- Call
fromBytes
method ofbase64
- A data of Type
base64
is created
- ... in myVar
- Find next modifier: is
in
for variable - Get varable name:
myVar
- Save current value in variable dictionnary
- ... write ...
- Find next operator: is
write
, is not a modifier - compute the Second operand, start after
write
and stop at the next operator or the end of the line
- /tmp/file.txt as textFile
- Detect the
as
modifier - Find the Type: is
textFile
- Call format of
textFile
- A data of Type
textFile
is created
Return at write
operator
- ... write ...
base64
write intextFile
- Search for specific method for
base64
inwrite
operator: there is not - Call
compute
ofwrite
- get the return and continue: for write the return is the first operand
- No more operations
- get the current value
- call toString
- print the type name
- print a column (:)
- print return of toString
Example:
KnownXorAttack.py /tmp/crypt ${pycalc -c "169846466316 to ascii"} > /tmp/file.decrypt