~jonn/snippets

ref: 5131aad8305d8173d2f98d1bb56a8968c6ce5186 snippets/python/7bitAscii.py -rw-r--r-- 6.0 KiB
5131aad8Jonn Problem: no 7-bit ASCII decoder in ctftools 2 months ago
                                                                                
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
ascii7 = [
  ["0", "00", "0000000",  "Ctrl- @   NUL   Null"],
  ["1", "01", "0000001",  "Ctrl- A   SOH   Start of Heading"],
  ["2", "02", "0000010",  "Ctrl- B   STX   Start of Text"],
  ["3", "03", "0000011",  "Ctrl- C   ETX   End of Text"],
  ["4", "04", "0000100",  "Ctrl- D   EOT   End of Transmission"],
  ["5", "05", "0000101",  "Ctrl- E   ENQ   Enquiry"],
  ["6", "06", "0000110",  "Ctrl- F   ACK   Acknowledge"],
  ["7", "07", "0000111",  "Ctrl- G   BEL   Bell"],
  ["8", "08", "0001000",  "Ctrl- H   Backspace-key   BS  Backspace"],
  ["9", "09", "0001001",  "Ctrl- I   Tab-key   HT  Horizontal Tab"],
  ["10",  "0a", "0001010",  "Ctrl- J   NL  Line Feed"],
  ["11",  "0b", "0001011",  "Ctrl- K   VT  Vertical Tab"],
  ["12",  "0c", "0001100",  "Ctrl- L   FF  Form Feed"],
  ["13", "0d", "0001101",  "Ctrl- M   Enter-key   CR  Carriage Return"],
  ["14",  "0e", "0001110",  "Ctrl- N   SO  Shift Out"],
  ["15",  "0f", "0001111",  "Ctrl- O   SI  Shift In"],
  ["16",  "10", "0010000",  "Ctrl- P   DLE   Data Link Escape"],
  ["17",  "11", "0010001",  "Ctrl- Q   DC1   Device Control 1 (XON)"],
  ["18",  "12", "0010010",  "Ctrl- R   DC2   Device Control 2"],
  ["19",  "13", "0010011",  "Ctrl- S   DC3   Device Control 3 (XOFF)"],
  ["20",  "14", "0010100",  "Ctrl- T   DC4   Device Control 4"],
  ["21",  "15", "0010101",  "Ctrl- U   NAK   Negative Acknowledge"],
  ["22",  "16", "0010110",  "Ctrl- V   SYN   Synchronous Idle"],
  ["23",  "17", "0010111",  "Ctrl- W   ETB   End of Text Block"],
  ["24",  "18", "0011000",  "Ctrl- X   CAN   Cancel"],
  ["25",  "19", "0011001",  "Ctrl- Y   EM  End of Medium"],
  ["26",  "1a", "0011010",  "Ctrl- Z   SUB   Substitute"],
  ["27",  "1b", "0011011",  "Ctrl- [   Esc-key   ESC   Escape"],
  ["28",  "1c", "0011100",  "Ctrl- \   FS  File Separator"],
  ["29",  "1d", "0011101",  "Ctrl- ]   GS  Group Separator"],
  ["30",  "1e", "0011110",  "Ctrl- ^   RS  Record Separator"],
  ["31",  "1f", "0011111",  "Ctrl- _   US  Unit Separator"],
  ["32",  "20", "0100000",  " "],
  ["33",  "21", "0100001",  "!"],
  ["34",  "22", "0100010",  "\""],
  ["35",  "23", "0100011",  "#"],
  ["36",  "24", "0100100",  "$"],
  ["37",  "25", "0100101",  "%"],
  ["38",  "26", "0100110",  "&"],
  ["39",  "27", "0100111",  "'"],
  ["40",  "28", "0101000",  "("],
  ["41",  "29", "0101001",  ")"],
  ["42",  "2a", "0101010",  "*"],
  ["43",  "2b", "0101011",  "+"],
  ["44",  "2c", "0101100",  ","],
  ["45",  "2d", "0101101",  "-"],
  ["46",  "2e", "0101110",  "."],
  ["47",  "2f", "0101111",  "/"],
  ["48",  "30", "0110000",  "0"],
  ["49",  "31", "0110001",  "1"],
  ["50",  "32", "0110010",  "2"],
  ["51",  "33", "0110011",  "3"],
  ["52",  "34", "0110100",  "4"],
  ["53",  "35", "0110101",  "5"],
  ["54",  "36", "0110110",  "6"],
  ["55",  "37", "0110111",  "7"],
  ["56",  "38", "0111000",  "8"],
  ["57",  "39", "0111001",  "9"],
  ["58",  "3a", "0111010",  ":"],
  ["59",  "3b", "0111011",  ";"],
  ["60",  "3c", "0111100",  "<"],
  ["61",  "3d", "0111101",  "="],
  ["62",  "3e", "0111110",  ">"],
  ["63",  "3f", "0111111",  "?"],
  ["64",  "40", "1000000",  "@"],
  ["65",  "41", "1000001",  "A"],
  ["66",  "42", "1000010",  "B"],
  ["67",  "43", "1000011",  "C"],
  ["68",  "44", "1000100",  "D"],
  ["69",  "45", "1000101",  "E"],
  ["70",  "46", "1000110",  "F"],
  ["71",  "47", "1000111",  "G"],
  ["72",  "48", "1001000",  "H"],
  ["73",  "49", "1001001",  "I"],
  ["74",  "4a", "1001010",  "J"],
  ["75",  "4b", "1001011",  "K"],
  ["76",  "4c", "1001100",  "L"],
  ["77",  "4d", "1001101",  "M"],
  ["78",  "4e", "1001110",  "N"],
  ["79",  "4f", "1001111",  "O"],
  ["80",  "50", "1010000",  "P"],
  ["81",  "51", "1010001",  "Q"],
  ["82",  "52", "1010010",  "R"],
  ["83",  "53", "1010011",  "S"],
  ["84",  "54", "1010100",  "T"],
  ["85",  "55", "1010101",  "U"],
  ["86",  "56", "1010110",  "V"],
  ["87",  "57", "1010111",  "W"],
  ["88",  "58", "1011000",  "X"],
  ["89",  "59", "1011001",  "Y"],
  ["90",  "5a", "1011010",  "Z"],
  ["91",  "5b", "1011011",  "["],
  ["92",  "5c", "1011100",  "\\"],
  ["93",  "5d", "1011101",  "]"],
  ["94",  "5e", "1011110",  "^"],
  ["95",  "5f", "1011111",  "_"],
  ["96",  "60", "1100000",  "`"],
  ["97",  "61", "1100001",  "a"],
  ["98",  "62", "1100010",  "b"],
  ["99",  "63", "1100011",  "c"],
  ["100",   "64", "1100100",  "d"],
  ["101",   "65", "1100101",  "e"],
  ["102",   "66", "1100110",  "f"],
  ["103",   "67", "1100111",  "g"],
  ["104",   "68", "1101000",  "h"],
  ["105",   "69", "1101001",  "i"],
  ["106",   "6a", "1101010",  "j"],
  ["107",   "6b", "1101011",  "k"],
  ["108",   "6c", "1101100",  "l"],
  ["109",   "6d", "1101101",  "m"],
  ["110",   "6e", "1101110",  "n"],
  ["111",   "6f", "1101111",  "o"],
  ["112",   "70", "1110000",  "p"],
  ["113",   "71", "1110001",  "q"],
  ["114",   "72", "1110010",  "r"],
  ["115",   "73", "1110011",  "s"],
  ["116",   "74", "1110100",  "t"],
  ["117",   "75", "1110101",  "u"],
  ["118",   "76", "1110110",  "v"],
  ["119",   "77", "1110111",  "w"],
  ["120",   "78", "1111000",  "x"],
  ["121",   "79", "1111001",  "y"],
  ["122",   "7a", "1111010",  "z"],
  ["123",   "7b", "1111011",  "{"],
  ["124",   "7c", "  1111100",  "|"],
  ["125",   "7d", "  1111101",  "}"],
  ["126",   "7e", "  1111110",  "~"],
  ["127",   "7f", "  1111111",  "Delete-key  DEL   Delete or Rubout"],
]

def zoom_binary(vec):
  return vec[2]

def zoom_char(vec):
  return vec[3]

def find_binary(haystack, needle):
  def p(candidate):
    return needle == zoom_binary(candidate)
  return list(filter(p, haystack))

def decodeBigEndian(data):
  x = int.from_bytes(data, "big")
  buff = ''
  flag = ''
  for i in "{0:b}".format(x): # Format python int as a binary string
    buff = buff + i # Build up string of 1s and 0s
    if len(buff) == 7: # When the string is 7-long, look-up character and reset the buffer
      flag = flag + zoom_char(find_binary(ascii7, buff)[0])
      buff = ''
  return flag

def main():
  file = open("../erlang/priv/01-binary-stream-editor.data", "rb")
  data = file.read()
  flag = decodeBigEndian(data)
  print("7-Bit-ASCII representation:")
  print(flag)

if __name__ == "__main__":
  main()