- Joined
- 11 yrs. 6 mth. 26 days
- Messages
- 5,381
- Reaction score
- 18,380
- Age
- 45
- Wallet
- 11,590$
- [email protected]
[0x01] Bit Manipulation:
Bit manipulation is the act of algorithmically manipulating bits or other pieces of data shorter than a word or string. Programming tasks that require bit manipulation include low-level device control. error detection and correction algorithms, data compression, encryption algorithms, and optimization. Code that does bit manipulation makes of of the bitwise operations: AND, OR, XOR, NOT, and bit shifts. This can be quite useful for those who are into low-level exploitation when creating and optimizing your asm, and shell code to contains the least number of bytes. So Bit manipulation can be done in just about any language, but for this tutorial I will be doing various bit manipulations in the python programming language.
[0x02] Manipulations:
[*] Find Bytes of Hex String!
Okay so we want to find how many bytes Phizo's hex string contains. Python code below
Hex
We declare the hex value as a string, using r (raw string literal) to get the raw value of the hex and not have the interpreter parse the backslashes. Also declare the encoding "utf-8", hex.decode() to decode the encoding, hex.count() to count the number of occurrences of 'x' in the hex string. Then using string formatting to print an integer after a string. %s is a value holder for the statement after %, which is the counter string.
OUTPUT:Number of Bytes: 39
[*] Binary String to Integer!
Convert a binary string to integer.
input = raw_input('Enter Binary String: ')print 'Interger is: %s' % (int(input, 2))
Simple version of the code above, or rather the main part is: int(00100001, 2). Where 00100001 can be any binary string of your choice. You can test this in the python IDLE gui. The above code just takes the raw input of the string input. raw_input prompts "Enter Binary String" in your terminal or cmd and whatever you type in will be the value of the input string. Then once again using string formatting we make %s equal to int(input, 2). input being whatever you entered, and then 2 being the base. The int() function just validates that the final product will be an integer.
OUPUT:Enter Binary String: 00100001Integer is: 33
[*] Binary String to Hex String!
Convert a binary string to a hex string.
input = raw_input('Enter Binary String: ')print 'Hex String is: 0x%x' % (int(input, 2))
Should not have to discuss the above code because it is pretty much the Binary to Integer code. The only difference here is adding 0x to the prefix of the output to represent hex value and the '%x' integer presentation. %x is for the hex format to as where %s is the string format and a string presentation. Note: You dont need to use x8 bits like above. Example below.
OUTPUT:Enter Binary String: 11111111Hex String is: 0xff
[*] Binary String to Character!
Convert Binary string to Character.
print '8 bits Max'input = raw_input('Enter Binary String: ')print 'Character String is: %s' % chr(int(input, 2))
Simple version of this in IDLE would be, chr(int(1110110, 2)). Which you can replace 1110110 with any 8 bit binary string of your liking. The above code, just takes input variable to the base of 2, converts it to an integer then uses the chr() function to turn the integer into a corresponding character. Example below.
OUTPUT:Enter Binary String: 1110110Character String is: v
[*] Hex String to Integer!
Convert Hex string to Integer.
input = raw_input('Enter Hex String: ')print 'Integer of Hex is: %s' % int(input, 16)
Simple version of this in IDLE would be, int(0xff, 16). The above code take input to the base of 16 and takes the integer value with the int() function. Example below.
Enter Hex String: 0xffInteger of Hex is: 255
[*] Character to Integer!
Convert Single Character to Integer value.
input = raw_input('Enter Character: ')print 'Integer String is: %s' % ord(input)
Simple version of this can be done in IDLE: ord('u'). Replacing u with any character of your choice. The above code uses the ord() function, which returns the integer value of a character. This is also equivalent to converting an 8 bit binary string to an integer. Like so: int('01110101', 2) = 117. Then validate this, we can take the binary string and convert it into a character and then using the above code see if the integer value of that character is equivalent to 117. chr(int('01110101', 2)) = u. Place 'u' into the above code.
OUTPUT:Enter Character: uInteger String is: 117
[*] Binary String to Single Character!
Convert Binary string to single character, this was discussed a bit above, but lets look at the code.
input = raw_input('Enter Binary String: ')print 'Single Character is: %s' % chr(int(input, 2))
Like discussed above in the Character to Integer section, we take the int() value of input to the base of 2 and then take that value to convert it to the corresponding character using the chr() function. Example below, using the u example we discussed earlier.
OUTPUT:Enter Binary String: 01110101Single Character is: u
[*] Individual Bits:
Individual bits of Integers.
input = raw_input('Enter Integer: ')print 'Individual Bit: %s' % int(1 << int(input))
The above code take the integer value of input and uses the << bitwise operator to compare the value to 1. This a small example of bit shifting. The << is the Binary Left Shift Operator. The left operands value is moved left by the number of bits specified by the right operand. Example below:
OUTPUT:Enter Integer: 2Individual Bit: 4
[*] Integer to Binary String!
Convert Integer to Binary string.
input = raw_input('Enter Integer: ')print 'Binary String is: %s' % bin(int(input))print "Binary String to 0&1's: %s" % bin(int(input))[2:]
The above code has to values returned. Dont freak, its just a clean up of the first value returned, to just output straight binary. Here we are using the built in bin() function to convert the integer value of input into binary. The bin() function does this all for us.
OUTPUT:Enter Integer: 117Binary String is: 0b1110101Binary String to 0&1's: 1110101# The [2:] removes the first two character which are 0b. But in reality the 0 is needed.So the actual Binary would be: 01110101. Adding the 0 back.
[*] Bit Length Count of Python Integer!
Get the Bit length count of a integer.
input = int(raw_input('Enter Integer: '))print 'Bit Count Length Of Integer: %s' % input.bit_length()
The above code, takes the int value of our raw_input() in the input string. So whatever is typed is parsed as an integer. Then we just simply use the built in bit_length() function. Applying it to our input string like so, input.bit_length(). Example Below:
Enter Integer: 8Bit Count Length Of Integer: 4
[*] Some other awesome tricks you can do in python that I dont feel like discussing but Im sure you python guys will understand.:
[*] Size of a integer of an arbitrary base:
def f(num, base=10)
= 0while num: o+= 1 num/= basereturn odef bitLen(int_type):length = 0while (int_type): int_type >>= 1 length += 1return(length)
[*] Binary Prefix:
def binary_prefix(value, binary=True):"""Parameters:- `value`: numeric type to trim down- `binary`: use binary (ICE) or decimal (SI) prefix"""SI = 'kMGTPEZY'unit = 1024. if binary else 1000.for i in range(-1, len(SI)): if abs(value) < unit: break value/= unitreturn (value, '' if i<0 else (SI.upper() + 'i' if binary else SI))
[0x03] Above Techniques in one script:
Just added command arguments. You know how it is.
[0x04] Conclusion:
Well there you go. Some basic bit manipulations all you low-level beginners can look at and use if needed. Hope you guys enjoy this :> anything that you think needs to be added just post it and Ill add it. Thanks for viewing.
Bit manipulation is the act of algorithmically manipulating bits or other pieces of data shorter than a word or string. Programming tasks that require bit manipulation include low-level device control. error detection and correction algorithms, data compression, encryption algorithms, and optimization. Code that does bit manipulation makes of of the bitwise operations: AND, OR, XOR, NOT, and bit shifts. This can be quite useful for those who are into low-level exploitation when creating and optimizing your asm, and shell code to contains the least number of bytes. So Bit manipulation can be done in just about any language, but for this tutorial I will be doing various bit manipulations in the python programming language.
[0x02] Manipulations:
[*] Find Bytes of Hex String!
Okay so we want to find how many bytes Phizo's hex string contains. Python code below
Hex
We declare the hex value as a string, using r (raw string literal) to get the raw value of the hex and not have the interpreter parse the backslashes. Also declare the encoding "utf-8", hex.decode() to decode the encoding, hex.count() to count the number of occurrences of 'x' in the hex string. Then using string formatting to print an integer after a string. %s is a value holder for the statement after %, which is the counter string.
OUTPUT:Number of Bytes: 39
[*] Binary String to Integer!
Convert a binary string to integer.
input = raw_input('Enter Binary String: ')print 'Interger is: %s' % (int(input, 2))
Simple version of the code above, or rather the main part is: int(00100001, 2). Where 00100001 can be any binary string of your choice. You can test this in the python IDLE gui. The above code just takes the raw input of the string input. raw_input prompts "Enter Binary String" in your terminal or cmd and whatever you type in will be the value of the input string. Then once again using string formatting we make %s equal to int(input, 2). input being whatever you entered, and then 2 being the base. The int() function just validates that the final product will be an integer.
OUPUT:Enter Binary String: 00100001Integer is: 33
[*] Binary String to Hex String!
Convert a binary string to a hex string.
input = raw_input('Enter Binary String: ')print 'Hex String is: 0x%x' % (int(input, 2))
Should not have to discuss the above code because it is pretty much the Binary to Integer code. The only difference here is adding 0x to the prefix of the output to represent hex value and the '%x' integer presentation. %x is for the hex format to as where %s is the string format and a string presentation. Note: You dont need to use x8 bits like above. Example below.
OUTPUT:Enter Binary String: 11111111Hex String is: 0xff
[*] Binary String to Character!
Convert Binary string to Character.
print '8 bits Max'input = raw_input('Enter Binary String: ')print 'Character String is: %s' % chr(int(input, 2))
Simple version of this in IDLE would be, chr(int(1110110, 2)). Which you can replace 1110110 with any 8 bit binary string of your liking. The above code, just takes input variable to the base of 2, converts it to an integer then uses the chr() function to turn the integer into a corresponding character. Example below.
OUTPUT:Enter Binary String: 1110110Character String is: v
[*] Hex String to Integer!
Convert Hex string to Integer.
input = raw_input('Enter Hex String: ')print 'Integer of Hex is: %s' % int(input, 16)
Simple version of this in IDLE would be, int(0xff, 16). The above code take input to the base of 16 and takes the integer value with the int() function. Example below.
Enter Hex String: 0xffInteger of Hex is: 255
[*] Character to Integer!
Convert Single Character to Integer value.
input = raw_input('Enter Character: ')print 'Integer String is: %s' % ord(input)
Simple version of this can be done in IDLE: ord('u'). Replacing u with any character of your choice. The above code uses the ord() function, which returns the integer value of a character. This is also equivalent to converting an 8 bit binary string to an integer. Like so: int('01110101', 2) = 117. Then validate this, we can take the binary string and convert it into a character and then using the above code see if the integer value of that character is equivalent to 117. chr(int('01110101', 2)) = u. Place 'u' into the above code.
OUTPUT:Enter Character: uInteger String is: 117
[*] Binary String to Single Character!
Convert Binary string to single character, this was discussed a bit above, but lets look at the code.
input = raw_input('Enter Binary String: ')print 'Single Character is: %s' % chr(int(input, 2))
Like discussed above in the Character to Integer section, we take the int() value of input to the base of 2 and then take that value to convert it to the corresponding character using the chr() function. Example below, using the u example we discussed earlier.
OUTPUT:Enter Binary String: 01110101Single Character is: u
[*] Individual Bits:
Individual bits of Integers.
input = raw_input('Enter Integer: ')print 'Individual Bit: %s' % int(1 << int(input))
The above code take the integer value of input and uses the << bitwise operator to compare the value to 1. This a small example of bit shifting. The << is the Binary Left Shift Operator. The left operands value is moved left by the number of bits specified by the right operand. Example below:
OUTPUT:Enter Integer: 2Individual Bit: 4
[*] Integer to Binary String!
Convert Integer to Binary string.
input = raw_input('Enter Integer: ')print 'Binary String is: %s' % bin(int(input))print "Binary String to 0&1's: %s" % bin(int(input))[2:]
The above code has to values returned. Dont freak, its just a clean up of the first value returned, to just output straight binary. Here we are using the built in bin() function to convert the integer value of input into binary. The bin() function does this all for us.
OUTPUT:Enter Integer: 117Binary String is: 0b1110101Binary String to 0&1's: 1110101# The [2:] removes the first two character which are 0b. But in reality the 0 is needed.So the actual Binary would be: 01110101. Adding the 0 back.
[*] Bit Length Count of Python Integer!
Get the Bit length count of a integer.
input = int(raw_input('Enter Integer: '))print 'Bit Count Length Of Integer: %s' % input.bit_length()
The above code, takes the int value of our raw_input() in the input string. So whatever is typed is parsed as an integer. Then we just simply use the built in bit_length() function. Applying it to our input string like so, input.bit_length(). Example Below:
Enter Integer: 8Bit Count Length Of Integer: 4
[*] Some other awesome tricks you can do in python that I dont feel like discussing but Im sure you python guys will understand.:
[*] Size of a integer of an arbitrary base:
def f(num, base=10)
[*] Binary Prefix:
def binary_prefix(value, binary=True):"""Parameters:- `value`: numeric type to trim down- `binary`: use binary (ICE) or decimal (SI) prefix"""SI = 'kMGTPEZY'unit = 1024. if binary else 1000.for i in range(-1, len(SI)): if abs(value) < unit: break value/= unitreturn (value, '' if i<0 else (SI.upper() + 'i' if binary else SI))
[0x03] Above Techniques in one script:
Just added command arguments. You know how it is.
[0x04] Conclusion:
Well there you go. Some basic bit manipulations all you low-level beginners can look at and use if needed. Hope you guys enjoy this :> anything that you think needs to be added just post it and Ill add it. Thanks for viewing.