Command Cheat Sheet

Command Cheat Sheet

Python 基本用語

  • Bytes $\to$ Hex
    1
    2
    3
      >>> example_str = b'\x17\x10\x06Ar\xe4G\xc9\xb5\xd7y\xbc'
      >>> example_str.hex()
      '1710064172e447c9b5d779bc'
    
  • Hex $\to$ String
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
      >>> bytes.fromhex('68656c6c6f').decode('utf-8')
      'hello'
      >>> # or
      >>> import binascii
      >>> binascii.unhexlify('68656c6c6f')
      b'hello"
        
      >>> # or
      >>> import codecs
      >>> decode_hex = codecs.getdecoder("hex_codec")
      >>> decode_hex(s)[0]
      b'hello'
    
  • String $\to$ Hex
    1
    2
    3
      >>> str= 'linuxhint'.encode('utf-8')
      >>> str.hex()
      '6c696e757868696e74'
    
  • Hex(String Type) $\to$ Decimal
    1
    2
    3
      >>> a = '123456'
      >>> int(a, 16)
      1193046
    
  • Decimal $\to$ Hex
    1
    2
    3
      >>> a = 1234
      >>> hex(a)
      '0x4d2'
    
  • Hex $\to$ Binary
    1
    2
      >>> bin(int('abc', 16))[2:].zfill(8)
      '101010111100'
    
  • String $\to$ Binary :::info if you’d like to do this transformation, 1st conversion is better :::
    1
    2
    3
    4
    5
      # string to hex to binary
      >>> bin(int('I love CNS'.encode('utf-8').hex(), 16))[2:].zfill(8)
      '1001001001000000110110001101111011101100110010100100000010000110100111001010011'
      >>> ''.join(format(ord(x), 'b') for x in 'I love CNS')
      '10010011000001101100110111111101101100101100000100001110011101010011'
    
  • Byte $\to$ String
    1
    2
      >>> b'abc\n'.decode('utf-8')
      'abc\n'
    
  • Binary $\to$ Hex
    1
    2
    3
    4
      >>> hex(int('010110', 2))
      '0x16'
      >>> hex(int('0000010010001101', 2))
      '0x48d'
    
  • Binary $\to$ Hex $\to$ String
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
      def binToHexa(n):
          bnum = int(n)
          temp = 0
          mul = 1
          count = 1
          hexaDeciNum = ['0'] * 100
          i = 0
          while bnum != 0:
              rem = bnum % 10
              temp = temp + (rem*mul)
              if count % 4 == 0:
                  if temp < 10:
                      hexaDeciNum[i] = chr(temp+48)
                  else:
                      hexaDeciNum[i] = chr(temp+55)
                  mul = 1
                  temp = 0
                  count = 1
                  i = i+1
              else:
                  mul = mul*2
                  count = count+1
              bnum = int(bnum/10)
          if count != 1:
              hexaDeciNum[i] = chr(temp+48)
          if count == 1:
              i = i-1
          hex_string = ''
          while i >= 0:
              hex_string += hexaDeciNum[i]
              i = i-1
          if hex_string == '':
              hex_string = '00'
          return hex_string
        
      plaintext_hex = binToHexa(plaintext_bin).encode().hex() 
      print(bytes.fromhex(plaintext_hex).decode())
    
  • Decimal(int type) $\to$ Hex(String type)
    1
    2
    3
    4
    5
    6
      >>> '{0:0>2x}'.format(0)
      '00'
      >>> '{0:0>2x}'.format(255)
      'ff'
      >>> '{:x}'.format(290275030195850039473456618367455885069965748851278076756743720446703314517401359267322769037469251445384426639837648598397)
      '7069636f4354467b6d347962335f54683073655f6d337335346733735f3472335f646966757272656e745f313737323733357d'
    
  • String(str type) $\iff$ Decimal
      >>> chr(97)
      'a'
      >>> ord('a')
      97
    
  • Decimal $\to$ Binary
      >>> bin(10)
      '0b1010'
    
  • Decimal $\to$ Bytes Type
    1
    2
    3
    4
      >>> bytes([10])
      b'\n'
      >>> bytes([70])
      b'F'
    
  • Array $\to$ List
      >>> import numpy as np
      >>> a = np.array([1,2,3])
      >>> a.tolist()
      [1, 2, 3]
    

Python 組合技

  • XOR Two Decimal
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
      >>> from itertools import cycle
      >>> def hex_xor(s1, s2):
      ...    b = bytearray()
      ...    for c1, c2 in zip(bytes.fromhex(s1), cycle(bytes.fromhex(s2))):
      ...        b.append(c1 ^ c2)
      ...    return b.hex()
      >>> s1 = 'aaab'
      >>> s2 = 'ccbc'
      >>> hex_xor(s1, s2)
      '6617'
    
  • Decimal $\to$ Ascii String
    1
    2
    3
      >>> tmp = 4028375274964940959047587304025089628177332141172593013450629550958369516176531641246900741346661851279741
      >>> bytes.fromhex('{:x}'.format(tmp)).decode('utf-8')
      'picoCTF{p0ll4rd_f4ct0r1z4at10n_FTW_7c8625a1}'
    

Python 酷酷的寫法

  • string倒續輸出
    1
    2
      >>> "galf"[::-1]
      'flag'
    
  • 取2的補數(取有號數的負號)
    1
    2
    3
    4
    5
    6
    7
      >>> import ctypes
      >>> a = 0x17c7cc6e
      >>> ctypes.c_int32(a).value
      398969966
      >>> b = 0xc158a854
      >>> ctypes.c_int32(b).value
      -1051154348
    

Linux 奇技淫巧語法