/usr/share/pyshared/impacket/dcerpc/svcctl.py is in python-impacket 0.9.10-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 | # Copyright (c) 2003-2012 CORE Security Technologies
#
# This software is provided under under a slightly modified version
# of the Apache Software License. See the accompanying LICENSE file
# for more information.
#
# $Id: svcctl.py 715 2012-09-10 19:40:25Z bethus@gmail.com $
#
# Author: Alberto Solino
#
# Description:
# SVCCTL (Services Control) interface implementation.
#
import array
from struct import *
from impacket import ImpactPacket
from impacket.structure import Structure
from impacket import dcerpc
from impacket.dcerpc import ndrutils, dcerpc
from impacket.uuid import uuidtup_to_bin
MSRPC_UUID_SVCCTL = uuidtup_to_bin(('367ABB81-9844-35F1-AD32-98F038001003', '2.0'))
# Error Codes
ERROR_PATH_NOT_FOUND = 3
ERROR_ACCESS_DENIED = 5
ERROR_INVALID_HANDLE = 6
ERROR_INVALID_DATA = 13
ERROR_INVALID_PARAMETER = 87
ERROR_INSUFICIENT_BUFFER = 122
ERROR_INVALID_NAME = 123
ERROR_INVALID_LEVEL = 124
ERROR_MORE_DATA = 234
ERROR_DEPENDENT_SERVICES_RUNNING = 1051
ERROR_INVALID_SERVICE_CONTROL = 1052
ERROR_SERVICE_REQUEST_TIMEOUT = 1053
ERROR_SERVICE_ALREADY_RUNNING = 1056
ERROR_INVALID_SERVICE_ACCOUNT = 1057
ERROR_SERVICE_DISABLED = 1058
ERROR_CIRCULAR_DEPENDENCY = 1059
ERROR_SERVICE_DOES_NOT_EXISTS = 1060
ERROR_SERVICE_CANNOT_ACCEPT_CTRL = 1061
ERROR_SERVICE_NOT_ACTIVE = 1062
ERROR_DATABASE_DOES_NOT_EXIST = 1065
ERROR_SERVICE_LOGON_FAILURE = 1069
ERROR_SERVICE_MARKED_FOR_DELETE = 1072
ERROR_SERVICE_EXISTS = 1073
ERROR_DUPLICATE_SERVICE_NAME = 1078
ERROR_SHUTDOWN_IN_PROGRESS = 1115
# Access codes
SERVICE_ALL_ACCESS = 0X000F01FF
SERVICE_CHANGE_CONFIG = 0X00000002
SERVICE_ENUMERATE_DEPENDENTS = 0X00000008
SERVICE_INTERROGATE = 0X00000080
SERVICE_PAUSE_CONTINUE = 0X00000040
SERVICE_QUERY_CONFIG = 0X00000001
SERVICE_QUERY_STATUS = 0X00000004
SERVICE_START = 0X00000010
SERVICE_STOP = 0X00000020
SERVICE_USER_DEFINED_CTRL = 0X00000100
SERVICE_SET_STATUS = 0X00008000
# Service Types
SERVICE_KERNEL_DRIVER = 0x00000001
SERVICE_FILE_SYSTEM_DRIVER = 0x00000002
SERVICE_WIN32_OWN_PROCESS = 0x00000010
SERVICE_WIN32_SHARE_PROCESS = 0x00000020
SERVICE_INTERACTIVE_PROCESS = 0x00000100
# Start Types
SERVICE_BOOT_START = 0x00000000
SERVICE_SYSTEM_START = 0x00000001
SERVICE_AUTO_START = 0x00000002
SERVICE_DEMAND_START = 0x00000003
SERVICE_DISABLED = 0x00000004
# Error Control
SERVICE_ERROR_IGNORE = 0x00000000
SERVICE_ERROR_NORMAL = 0x00000001
SERVICE_ERROR_SEVERE = 0x00000002
SERVICE_ERROR_CRITICAL = 0x00000003
# Service Control Codes
SERVICE_CONTROL_CONTINUE = 0x00000003
SERVICE_CONTROL_INTERROGATE = 0x00000004
SERVICE_CONTROL_PARAMCHANGE = 0x00000006
SERVICE_CONTROL_PAUSE = 0x00000002
SERVICE_CONTROL_STOP = 0x00000001
# Service State
SERVICE_ACTIVE = 0x00000001
SERVICE_INACTIVE = 0x00000002
SERVICE_STATE_ALL = 0x00000003
# Current State
SERVICE_CONTINUE_PENDING = 0x00000005
SERVICE_PAUSE_PENDING = 0x00000006
SERVICE_PAUSED = 0x00000007
SERVICE_RUNNING = 0x00000004
SERVICE_START_PENDING = 0x00000002
SERVICE_STOP_PENDING = 0x00000003
SERVICE_STOPPED = 0x00000001
class SVCCTLSessionError(Exception):
error_messages = {
ERROR_PATH_NOT_FOUND : ("ERROR_PATH_NOT_FOUND", "The system cannot find the path specified."),
ERROR_ACCESS_DENIED : ("ERROR_ACCESS_DENIED", "Access is denied."),
ERROR_INVALID_HANDLE : ("ERROR_INVALID_HANDLE", "The handle is invalid."),
ERROR_INVALID_DATA : ("ERROR_INVALID_DATA", "The data is invalid."),
ERROR_INVALID_PARAMETER : ("ERROR_INVALID_PARAMETER", "The parameter is incorrect."),
ERROR_INSUFICIENT_BUFFER : ("ERROR_INSUFICIENT_BUFFER", "The data area passed to a system call is too small."),
ERROR_INVALID_NAME : ("ERROR_INVALID_NAME", "The specified name is invalid."),
ERROR_INVALID_LEVEL : ("ERROR_INVALID_LEVEL", "The level specified contains an unsupported value."),
ERROR_MORE_DATA : ("ERROR_MORE_DATA", "More data is available."),
ERROR_DEPENDENT_SERVICES_RUNNING: ("ERROR_DEPENDENT_SERVICES_RUNNING", "The service cannot be stopped because other running services are dependent on it."),
ERROR_INVALID_SERVICE_CONTROL : ("ERROR_INVALID_SERVICE_CONTROL", "The requested control code is not valid, or it is unacceptable to the service."),
ERROR_SERVICE_REQUEST_TIMEOUT : ("ERROR_SERVICE_REQUEST_TIMEOUT", "The request timed out."),
ERROR_SERVICE_ALREADY_RUNNING : ("ERROR_SERVICE_ALREADY_RUNNING", "The target service is already running."),
ERROR_INVALID_SERVICE_ACCOUNT : ("ERROR_INVALID_SERVICE_ACCOUNT", "The service account specified does not exist."),
ERROR_SERVICE_DISABLED : ("ERROR_SERVICE_DISABLED", "The service is disabled."),
ERROR_CIRCULAR_DEPENDENCY : ("ERROR_CIRCULAR_DEPENDENCY", "A circular dependency was specified."),
ERROR_SERVICE_DOES_NOT_EXISTS : ("ERROR_SERVICE_DOES_NOT_EXISTS", "The service does not exist in the SCM database."),
ERROR_SERVICE_CANNOT_ACCEPT_CTRL: ("ERROR_SERVICE_CANNOT_ACCEPT_CTRL", "The requested control code cannot be sent to the service."),
ERROR_SERVICE_NOT_ACTIVE : ("ERROR_SERVICE_NOT_ACTIVE", "The service has not been started."),
ERROR_DATABASE_DOES_NOT_EXIST : ("ERROR_DATABASE_DOES_NOT_EXIST", "The database specified does not exists."),
ERROR_SERVICE_LOGON_FAILURE : ("ERROR_SERVICE_LOGON_FAILURE", "The service did not start due to a logon failure."),
ERROR_SERVICE_MARKED_FOR_DELETE : ("ERROR_SERVICE_MARKED_FOR_DELETE", "The service has been marked for deletion."),
ERROR_SERVICE_EXISTS : ("ERROR_SERVICE_EXISTS", "The service already exists."),
ERROR_DUPLICATE_SERVICE_NAME : ("ERROR_DUPLICATE_SERVICE_NAME", "The service already exists."),
ERROR_SHUTDOWN_IN_PROGRESS : ("ERROR_SHUTDOWN_IN_PROGRESS", "The system is shutting down."),
}
def __init__( self, error_code):
Exception.__init__(self)
self.error_code = error_code
def get_error_code( self ):
return self.error_code
def __str__( self ):
key = self.error_code
if (SVCCTLSessionError.error_messages.has_key(key)):
error_msg_short = SVCCTLSessionError.error_messages[key][0]
error_msg_verbose = SVCCTLSessionError.error_messages[key][1]
return 'SVCCTL SessionError: code: %s - %s - %s' % (str(self.error_code), error_msg_short, error_msg_verbose)
else:
return 'SVCCTL SessionError: unknown error code: %s' % (str(self.error_code))
class SVCCTLServiceStatus(Structure):
structure = (
('ServiceType','<L'),
('CurrentState','<L'),
('ControlsAccepted','<L'),
('Win32ExitCode','<L'),
('ServiceSpecificExitCode','<L'),
('CheckPoint','<L'),
('WaitHint','<L'),
)
class SVCCTLQueryServiceConfigW(Structure):
structure = (
('ServiceType','<L'),
('StartType','<L'),
('ErrorControl','<L'),
('pBinaryPathName','<L'),
('pLoadOrderGroup','<L'),
('TagID','<L'),
('pDependencies','<L'),
('pServiceStartName','<L'),
('pDisplayName','<L'),
)
class SVCCTLRQueryServiceConfigW(Structure):
opnum = 17
alignment = 4
structure = (
('ContextHandle','20s'),
('BuffSize','<L=0'),
)
class SVCCTLRQueryServiceConfigWResponse(Structure):
structure = (
('QueryConfig',':',SVCCTLQueryServiceConfigW),
('BufferLen','_-StringsBuffer','self["BufferSize"]'),
('StringsBuffer',':'),
('BytesNeeded','<L'),
('ErrorCode','<L'),
)
class SVCCTLRQueryServiceStatus(Structure):
opnum = 6
alignment = 4
structure = (
('ContextHandle','20s'),
)
class SVCCTLRDeleteService(Structure):
opnum = 2
alignment = 4
structure = (
('ContextHandle','20s'),
)
class SVCCTLRControlService(Structure):
opnum = 1
alignment = 4
structure = (
('ContextHandle','20s'),
('Control','<L'),
)
class SVCCTLRControlServiceResponse(Structure):
alignment = 4
structure = (
('ServiceStatus',':',SVCCTLServiceStatus),
#('ErrorCode','<L'),
)
class SVCCTLRStartServiceW(Structure):
opnum = 19
alignment = 4
structure = (
('ContextHandle','20s'),
('argc','<L=0'),
('argv',':'),
)
class SVCCTLROpenServiceW(Structure):
opnum = 16
alignment = 4
structure = (
('SCManager','20s'),
('ServiceName',':',ndrutils.NDRStringW),
('DesiredAccess','<L'),
)
class SVCCTLROpenServiceA(Structure):
opnum = 28
alignment = 4
structure = (
('SCManager','20s'),
('ServiceName',':',ndrutils.NDRStringA),
('DesiredAccess','<L'),
)
class SVCCTLROpenServiceResponse(Structure):
alignment = 4
structure = (
('ContextHandle','20s'),
('ErrorCode','<L'),
)
class SVCCTLROpenSCManagerW(Structure):
opnum = 15
alignment = 4
structure = (
('MachineName',':',ndrutils.NDRUniqueStringW),
('DatabaseName','"\x00'),
('DesiredAccess','<L'),
)
class SVCCTLROpenSCManagerAResponse(Structure):
alignment = 4
structure = (
('ContextHandle','20s'),
('ErrorCode','<L'),
)
class SVCCTLRCloseServiceHandle(Structure):
opnum = 0
alignment = 4
structure = (
('ContextHandle','20s'),
)
class SVCCTLRCloseServiceHandlerResponse(Structure):
alignment = 4
structure = (
('ContextHandle','20s'),
('ErrorCode','<L'),
)
class SVCCTLRCreateServiceW(Structure):
opnum = 12
alignment = 4
structure = (
('SCManager','20s'),
('ServiceName',':',ndrutils.NDRStringW),
('DisplayName',':',ndrutils.NDRUniqueStringW),
('DesiredAccess','<L'),
('ServiceType','<L'),
('StartType','<L'),
('ErrorControl','<L'),
('BinaryPathName',':',ndrutils.NDRStringW),
('LoadOrderGroup','<L=0'),
('TagID','<L=0'),
('Dependencies','<L=0'),
('DependenciesSize','<L=0'),
#('pServiceStartName','<L-&ServiceStartName'),
#('ServiceStartName','w'),
('ServiceStartName','<L=0'),
('Password','<L=0'),
('PwSize','<L=0'),
)
class SVCCTLRCreateServiceWResponse(Structure):
alignment = 4
structure = (
('TagID','<L'),
('ContextHandle','20s'),
('ErrorCode','<L'),
)
class SVCCTLREnumServicesStatusW(Structure):
opnum = 14
alignment = 4
structure = (
('ContextHandle','20s'),
('ServiceType','<L'),
('ServiceState','<L'),
('BuffSize','<L=0'),
('pResumeIndex','<L=123'),
('ResumeIndex','<L=0'),
)
class SVCCTLREnumServicesStatusWResponse(Structure):
alignment = 4
structure = (
('BuffSize','<L'),
('BufferLen','_-Buffer','self["BuffSize"]'),
('Buffer',':'),
('BytesNeeded','<L'),
('ServicesReturned','<L'),
('Dontknow','<L'),
('Dontknow','<L'),
('ErrorCode','<L'),
)
# OLD Style structs.. leaving this stuff for compatibility purpose. Don't use these structs/functions anymore
class SVCCTLOpenSCManagerHeader(ImpactPacket.Header):
OP_NUM = 0x1B
__SIZE = 32
def __init__(self, aBuffer = None):
ImpactPacket.Header.__init__(self, SVCCTLOpenSCManagerHeader.__SIZE)
self.set_referent_id(0xFFFFFF)
self.set_access_mask(0xF003F)
if aBuffer: self.load_header(aBuffer)
def get_referent_id(self):
return self.get_long(0, '<')
def set_referent_id(self, id):
self.set_long(0, id, '<')
def get_max_count(self):
return self.get_long(4, '<')
def set_max_count(self, num):
self.set_long(4, num, '<')
def get_offset(self):
return self.get_long(8, '<')
def set_offset(self, num):
self.set_long(8, num, '<')
def get_cur_count(self):
return self.get_long(12, '<')
def set_cur_count(self, num):
self.set_long(12, num, '<')
def get_machine_name(self):
return self.get_bytes().tostring()[:20]
def set_machine_name(self, name):
assert len(name) <= 8
self.set_max_count(len(name) + 1)
self.set_cur_count(len(name) + 1)
self.get_bytes()[16:24] = array.array('B', name + (8 - len(name)) * '\x00')
def get_access_mask(self):
return self.get_long(28, '<')
def set_access_mask(self, mask):
self.set_long(28, mask, '<')
def get_header_size(self):
return SVCCTLOpenSCManagerHeader.__SIZE
class SVCCTLRespOpenSCManagerHeader(ImpactPacket.Header):
__SIZE = 24
def __init__(self, aBuffer = None):
ImpactPacket.Header.__init__(self, SVCCTLRespOpenSCManagerHeader.__SIZE)
if aBuffer: self.load_header(aBuffer)
def get_context_handle(self):
return self.get_bytes().tolist()[:20]
def set_context_handle(self, handle):
assert 20 == len(handle)
self.get_bytes()[:20] = array.array('B', handle)
def get_return_code(self):
return self.get_long(20, '<')
def set_return_code(self, code):
self.set_long(20, code, '<')
def get_header_size(self):
return SVCCTLRespOpenSCManagerHeader.__SIZE
class SVCCTLOpenServiceHeader(ImpactPacket.Header):
OP_NUM = 0x1C
__SIZE = 48
def __init__(self, aBuffer = None):
ImpactPacket.Header.__init__(self, SVCCTLOpenServiceHeader.__SIZE)
self.set_max_count(9)
self.set_cur_count(9)
# Write some unknown fluff.
self.get_bytes()[40:] = array.array('B', '\x00\x10\x48\x60\xff\x01\x0f\x00')
if aBuffer: self.load_header(aBuffer)
def get_context_handle(self):
return self.get_bytes().tolist()[:20]
def set_context_handle(self, handle):
assert 20 == len(handle)
self.get_bytes()[:20] = array.array('B', handle)
def get_max_count(self):
return self.get_long(20, '<')
def set_max_count(self, num):
self.set_long(20, num, '<')
def get_offset(self):
return self.get_long(24, '<')
def set_offset(self, num):
self.set_long(24, num, '<')
def get_cur_count(self):
return self.get_long(28, '<')
def set_cur_count(self, num):
self.set_long(28, num, '<')
def get_service_name(self):
return self.get_bytes().tostring()[32:40]
def set_service_name(self, name):
assert len(name) <= 8
self.get_bytes()[32:40] = array.array('B', name + (8 - len(name)) * '\x00')
def get_header_size(self):
return SVCCTLOpenServiceHeader.__SIZE
class SVCCTLRespOpenServiceHeader(ImpactPacket.Header):
__SIZE = 24
def __init__(self, aBuffer = None):
ImpactPacket.Header.__init__(self, SVCCTLRespOpenServiceHeader.__SIZE)
if aBuffer: self.load_header(aBuffer)
def get_context_handle(self):
return self.get_bytes().tolist()[:20]
def set_context_handle(self, handle):
assert 20 == len(handle)
self.get_bytes()[:20] = array.array('B', handle)
def get_return_code(self):
return self.get_long(20, '<')
def set_return_code(self, code):
self.set_long(20, code, '<')
def get_header_size(self):
return SVCCTLRespOpenServiceHeader.__SIZE
class SVCCTLCloseServiceHeader(ImpactPacket.Header):
OP_NUM = 0x0
__SIZE = 20
def __init__(self, aBuffer = None):
ImpactPacket.Header.__init__(self, SVCCTLCloseServiceHeader.__SIZE)
if aBuffer: self.load_header(aBuffer)
def get_context_handle(self):
return self.get_bytes().tolist()[:]
def set_context_handle(self, handle):
assert 20 == len(handle)
self.get_bytes()[:] = array.array('B', handle)
def get_header_size(self):
return SVCCTLCloseServiceHeader.__SIZE
class SVCCTLRespCloseServiceHeader(ImpactPacket.Header):
__SIZE = 24
def __init__(self, aBuffer = None):
ImpactPacket.Header.__init__(self, SVCCTLRespCloseServiceHeader.__SIZE)
if aBuffer: self.load_header(aBuffer)
def get_context_handle(self):
return self.get_bytes().tolist()[:20]
def set_context_handle(self, handle):
assert 20 == len(handle)
self.get_bytes()[:20] = array.array('B', handle)
def get_return_code(self):
return self.get_long(20, '<')
def set_return_code(self, code):
self.set_long(20, code, '<')
def get_header_size(self):
return SVCCTLRespCloseServiceHeader.__SIZE
class SVCCTLCreateServiceHeader(ImpactPacket.Header):
OP_NUM = 0x18
__SIZE = 132
def __init__(self, aBuffer = None):
ImpactPacket.Header.__init__(self, SVCCTLCreateServiceHeader.__SIZE)
self.set_name_max_count(9)
self.set_name_cur_count(9)
self.set_service_flags(0x110)
self.set_start_mode(2)
self.get_bytes()[40:48] = array.array('B', '\x00\x10\x48\x60\xe4\xa3\x40\x00')
self.get_bytes()[68:76] = array.array('B', '\x00\x00\x00\x00\xff\x01\x0f\x00')
self.get_bytes()[84:88] = array.array('B', '\x01\x00\x00\x00')
if aBuffer: self.load_header(aBuffer)
def get_context_handle(self):
return self.get_bytes().tolist()[:20]
def set_context_handle(self, handle):
assert 20 == len(handle)
self.get_bytes()[:20] = array.array('B', handle)
def get_name_max_count(self):
return self.get_long(4, '<')
def set_name_max_count(self, num):
self.set_long(20, num, '<')
self.set_long(48, num, '<')
def get_name_offset(self):
return self.get_long(8, '<')
def set_name_offset(self, num):
self.set_long(24, num, '<')
self.set_long(52, num, '<')
def get_name_cur_count(self):
return self.get_long(12, '<')
def set_name_cur_count(self, num):
self.set_long(28, num, '<')
self.set_long(56, num, '<')
def get_service_name(self):
return self.get_bytes().tostring()[32:40]
def set_service_name(self, name):
self.get_bytes()[32:40] = array.array('B', name + (8 - len(name)) * '\x00')
self.get_bytes()[60:68] = array.array('B', name + (8 - len(name)) * '\x00')
# 0x0000100 = Allow service to interact with desktop (needed by vnc server for example)
# 0x0000010 = Log as: Local System Account
def get_service_flags(self):
return self.get_long(76, '<')
def set_service_flags(self, flags):
self.set_long(76, flags, '<')
# 2 Automatic
# 3 Manual
# 4 Disabled
def get_start_mode(self):
return self.get_long(80, '<')
def set_start_mode(self, mode):
self.set_long(80, mode, '<')
def get_path_max_count(self):
return self.get_long(88, '<')
def set_path_max_count(self, num):
self.set_long(88, num, '<')
def get_path_offset(self):
return self.get_long(92, '<')
def set_path_offset(self, num):
self.set_long(92, num, '<')
def get_path_cur_count(self):
return self.get_long(96, '<')
def set_path_cur_count(self, num):
self.set_long(96, num, '<')
def get_service_path(self):
return self.get_bytes().tostring()[100:-32]
def set_service_path(self, path):
self.get_bytes()[100:-32] = array.array('B', path)
self.set_path_max_count(len(path)+1)
self.set_path_cur_count(len(path)+1)
def get_header_size(self):
var_size = len(self.get_bytes()) - SVCCTLCreateServiceHeader.__SIZE
assert var_size > 0
return SVCCTLCreateServiceHeader.__SIZE + var_size
class SVCCTLRespCreateServiceHeader(ImpactPacket.Header):
__SIZE = 28
def __init__(self, aBuffer = None):
ImpactPacket.Header.__init__(self, SVCCTLRespCreateServiceHeader.__SIZE)
if aBuffer: self.load_header(aBuffer)
def get_context_handle(self):
return self.get_bytes().tolist()[4:24]
def set_context_handle(self, handle):
assert 20 == len(handle)
self.get_bytes()[4:24] = array.array('B', handle)
def get_return_code(self):
return self.get_long(24, '<')
def set_return_code(self, code):
self.set_long(24, code, '<')
def get_header_size(self):
return SVCCTLRespCreateServiceHeader.__SIZE
class SVCCTLDeleteServiceHeader(ImpactPacket.Header):
OP_NUM = 0x2
__SIZE = 20
def __init__(self, aBuffer = None):
ImpactPacket.Header.__init__(self, SVCCTLDeleteServiceHeader.__SIZE)
if aBuffer: self.load_header(aBuffer)
def get_context_handle(self):
return self.get_bytes().tolist()[:20]
def set_context_handle(self, handle):
assert 20 == len(handle)
self.get_bytes()[:20] = array.array('B', handle)
def get_header_size(self):
return SVCCTLDeleteServiceHeader.__SIZE
class SVCCTLRespDeleteServiceHeader(ImpactPacket.Header):
__SIZE = 4
def __init__(self, aBuffer = None):
ImpactPacket.Header.__init__(self, SVCCTLRespDeleteServiceHeader.__SIZE)
if aBuffer: self.load_header(aBuffer)
def get_return_code(self):
return self.get_long(0, '<')
def set_return_code(self, code):
self.set_long(0, code, '<')
def get_header_size(self):
return SVCCTLRespDeleteServiceHeader.__SIZE
class SVCCTLStopServiceHeader(ImpactPacket.Header):
OP_NUM = 0x1
__SIZE = 24
def __init__(self, aBuffer = None):
ImpactPacket.Header.__init__(self, SVCCTLStopServiceHeader.__SIZE)
# Write some unknown fluff.
self.get_bytes()[20:] = array.array('B', '\x01\x00\x00\x00')
if aBuffer: self.load_header(aBuffer)
def get_context_handle(self):
return self.get_bytes().tolist()[:20]
def set_context_handle(self, handle):
assert 20 == len(handle)
self.get_bytes()[:20] = array.array('B', handle)
def get_header_size(self):
return SVCCTLStopServiceHeader.__SIZE
class SVCCTLRespStopServiceHeader(ImpactPacket.Header):
__SIZE = 32
def __init__(self, aBuffer = None):
ImpactPacket.Header.__init__(self, SVCCTLRespStopServiceHeader.__SIZE)
if aBuffer: self.load_header(aBuffer)
def get_return_code(self):
return self.get_long(28, '<')
def set_return_code(self, code):
self.set_long(28, code, '<')
def get_header_size(self):
return SVCCTLRespStopServiceHeader.__SIZE
class SVCCTLStartServiceHeader(ImpactPacket.Header):
OP_NUM = 0x1F
__SIZE = 32
def __init__(self, aBuffer = None):
ImpactPacket.Header.__init__(self, SVCCTLStartServiceHeader.__SIZE)
if aBuffer: self.load_header(aBuffer)
def get_context_handle(self):
return self.get_bytes().tolist()[:20]
def set_context_handle(self, handle):
assert 20 == len(handle)
self.get_bytes()[:20] = array.array('B', handle)
def get_arguments(self):
raise Exception, "method not implemented"
def set_arguments(self, arguments):
args_data = apply(pack, ['<' + 'L'*len(arguments)] + map(id, arguments) )
args_data += reduce(lambda a, b: a+b,
map(lambda element: pack('<LLL', len(element)+1, 0, len(element)+1) + element + '\x00' + '\x00' * ((4 - (len(element) + 1) % 4) % 4), arguments),
'')
data = pack('<LLL', len(arguments), id(arguments) & 0xffffffff, len(arguments)) + args_data
self.get_bytes()[20:] = array.array('B', data)
def get_header_size(self):
var_size = len(self.get_bytes()) - SVCCTLStartServiceHeader.__SIZE
assert var_size > 0
return SVCCTLStartServiceHeader.__SIZE + var_size
class SVCCTLRespStartServiceHeader(ImpactPacket.Header):
__SIZE = 4
def __init__(self, aBuffer = None):
ImpactPacket.Header.__init__(self, SVCCTLRespStartServiceHeader.__SIZE)
if aBuffer: self.load_header(aBuffer)
def get_return_code(self):
return self.get_long(0, '<')
def set_return_code(self, code):
self.set_long(0, code, '<')
def get_header_size(self):
return SVCCTLRespStartServiceHeader.__SIZE
class DCERPCSvcCtl:
def __init__(self, dcerpc):
self._dcerpc = dcerpc
def open_manager(self):
hostname = 'IMPACT'
opensc = SVCCTLOpenSCManagerHeader()
opensc.set_machine_name(hostname)
self._dcerpc.send(opensc)
data = self._dcerpc.recv()
retVal = SVCCTLRespOpenSCManagerHeader(data)
return retVal
def create_service(self, context_handle, service_name, service_path):
creates = SVCCTLCreateServiceHeader()
creates.set_context_handle(context_handle)
creates.set_service_name(service_name)
creates.set_service_path(service_path)
self._dcerpc.send(creates)
data = self._dcerpc.recv()
retVal = SVCCTLRespCreateServiceHeader(data)
return retVal
def close_handle(self, context_handle):
closeh = SVCCTLCloseServiceHeader()
closeh.set_context_handle(context_handle)
self._dcerpc.send(closeh)
data = self._dcerpc.recv()
retVal = SVCCTLRespCloseServiceHeader(data)
return retVal
def delete_service(self, context_handle):
deletes = SVCCTLDeleteServiceHeader()
deletes.set_context_handle(context_handle)
self._dcerpc.send(deletes)
data = self._dcerpc.recv()
retVal = SVCCTLRespDeleteServiceHeader(data)
return retVal
def open_service(self, context_handle, service_name):
opens = SVCCTLOpenServiceHeader()
opens.set_context_handle(context_handle)
opens.set_service_name(service_name)
self._dcerpc.send(opens)
data = self._dcerpc.recv()
retVal = SVCCTLRespOpenServiceHeader(data)
return retVal
def stop_service(self, context_handle):
stops = SVCCTLStopServiceHeader()
stops.set_context_handle(context_handle)
self._dcerpc.send(stops)
data = self._dcerpc.recv()
retVal = SVCCTLRespStopServiceHeader(data)
return retVal
def start_service(self, context_handle, arguments):
starts = SVCCTLStartServiceHeader()
starts.set_arguments( arguments )
starts.set_context_handle(context_handle)
self._dcerpc.send(starts)
data = self._dcerpc.recv()
retVal = SVCCTLRespStartServiceHeader(data)
return retVal
# Use these functions to manipulate services. The previous ones are left for backward compatibility reasons.
def doRequest(self, request, noAnswer = 0, checkReturn = 1):
self._dcerpc.call(request.opnum, request)
if noAnswer:
return
else:
answer = self._dcerpc.recv()
if checkReturn and answer[-4:] != '\x00\x00\x00\x00':
error_code = unpack("<L", answer[-4:])[0]
raise SVCCTLSessionError(error_code)
return answer
def DeleteService(self, handle):
deleteService = SVCCTLRDeleteService()
deleteService['ContextHandle'] = handle
ans = self.doRequest(deleteService, checkReturn = 1)
return ans
def StopService(self, handle):
controlService = SVCCTLRControlService()
controlService['ContextHandle'] = handle
controlService['Control'] = SERVICE_CONTROL_STOP
ans = self.doRequest(controlService, checkReturn = 1)
return SVCCTLServiceStatus(ans)
def OpenServiceA(self, handle, name):
openService = SVCCTLROpenServiceA()
openService['SCManager'] = handle
openService['ServiceName'] = ndrutils.NDRStringA()
openService['ServiceName']['Data'] = (name+'\x00')
openService['DesiredAccess'] = SERVICE_ALL_ACCESS
ans = self.doRequest(openService, checkReturn = 1)
return SVCCTLROpenServiceResponse(ans)
def OpenServiceW(self, handle, name):
# We MUST receive Unicode data here
openService = SVCCTLROpenServiceW()
openService['SCManager'] = handle
openService['ServiceName'] = ndrutils.NDRStringW()
openService['ServiceName']['Data'] = (name+'\x00'.encode('utf-16le'))
openService['DesiredAccess'] = SERVICE_ALL_ACCESS
ans = self.doRequest(openService, checkReturn = 1)
return SVCCTLROpenServiceResponse(ans)
def StartServiceW(self, handle, arguments = ''):
# TODO: argv has to be a pointer to a buffer that contains an array
# of pointers to null-terminated UNICODE strings that are passed as
# arguments to the service
startService = SVCCTLRStartServiceW()
startService['ContextHandle'] = handle
startService['argc'] = len(arguments)
if len(arguments) == 0:
startService['argv'] = '\x00'*4
else:
args_data = pack('<LL', id(arguments) & 0xffffffff, len(arguments))
for argument in arguments:
args_data += pack('<L',id(argument)&0xffffffff)
for i in range(len(arguments)):
item = ndrutils.NDRStringW()
item['Data'] = arguments[i]+'\x00'.encode('utf-16le')
args_data += str(item)
startService['argv'] = args_data
ans = self.doRequest(startService, checkReturn = 1)
return ans
def CreateServiceW(self, handle, serviceName, displayName, binaryPathName):
# We MUST receive Unicode data here
createService = SVCCTLRCreateServiceW()
createService['SCManager'] = handle
createService['ServiceName'] = ndrutils.NDRStringW()
createService['ServiceName']['Data'] = (serviceName+'\x00'.encode('utf-16le'))
createService['DisplayName'] = ndrutils.NDRUniqueStringW()
createService['DisplayName']['Data'] = (displayName+'\x00'.encode('utf-16le'))
createService['DesiredAccess'] = SERVICE_ALL_ACCESS
createService['ServiceType'] = SERVICE_WIN32_OWN_PROCESS | SERVICE_INTERACTIVE_PROCESS
createService['StartType'] = SERVICE_AUTO_START
#createService['StartType'] = SERVICE_DEMAND_START
createService['ErrorControl'] = SERVICE_ERROR_IGNORE
createService['BinaryPathName'] = ndrutils.NDRStringW()
createService['BinaryPathName']['Data'] = (binaryPathName+'\x00'.encode('utf-16le'))
createService['TagID'] = 0
ans = self.doRequest(createService, checkReturn = 1)
return SVCCTLRCreateServiceWResponse(ans)
def OpenSCManagerW(self):
openSCManager = SVCCTLROpenSCManagerW()
openSCManager['MachineName'] = ndrutils.NDRUniqueStringW()
openSCManager['MachineName']['Data'] = 'DUMMY\x00'.encode('utf-16le')
openSCManager['DesiredAccess'] = SERVICE_START | SERVICE_STOP | SERVICE_CHANGE_CONFIG | SERVICE_QUERY_CONFIG | SERVICE_QUERY_STATUS | SERVICE_ENUMERATE_DEPENDENTS
ans = self.doRequest(openSCManager, checkReturn = 1)
return SVCCTLROpenSCManagerAResponse(ans)
def CloseServiceHandle(self, handle):
closeHandle = SVCCTLRCloseServiceHandle()
closeHandle['ContextHandle'] = handle
ans = self.doRequest(closeHandle, checkReturn = 1)
return SVCCTLRCloseServiceHandlerResponse(ans)
def EnumServicesStatusW(self, handle, serviceType = SERVICE_KERNEL_DRIVER | SERVICE_FILE_SYSTEM_DRIVER | SERVICE_WIN32_OWN_PROCESS | SERVICE_WIN32_SHARE_PROCESS | SERVICE_INTERACTIVE_PROCESS, serviceState = SERVICE_STATE_ALL ):
enumServices = SVCCTLREnumServicesStatusW()
enumServices['ContextHandle'] = handle
enumServices['ServiceType'] = serviceType
enumServices['ServiceState'] = serviceState
enumServices['BuffSize'] = 0x0
# First packet is to get the buffer size we need to hold the answer
ans = self.doRequest(enumServices, checkReturn = 0)
packet = SVCCTLREnumServicesStatusWResponse(ans)
enumServices['BuffSize'] = packet['BytesNeeded']
# Now the actual request
ans = self.doRequest(enumServices, checkReturn = 1)
packet = SVCCTLREnumServicesStatusWResponse(ans)
data = packet['Buffer']
# TODO: There are a few NDR types that I still don't know how they are marshalled... I'm sure this could be done way cleaner..
index = 0
enumServicesList = []
for i in range(packet['ServicesReturned']):
tmpDict = {}
serviceNamePtr = unpack('<L',data[index:index+4])[0]
index += 4
displayNamePtr = unpack('<L',data[index:index+4])[0]
index += 4
serviceStatus = SVCCTLServiceStatus(data[index:])
tmpDict['ServiceType'] = serviceStatus['ServiceType']
tmpDict['CurrentState'] = serviceStatus['CurrentState']
tmpDict['ControlsAccepted'] = serviceStatus['ControlsAccepted']
# Now Parse the strings
string = data[displayNamePtr:].split('\x00\x00\x00')[0]
tmpDict['DisplayName'] = string + '\x00'
string = data[serviceNamePtr:].split('\x00\x00\x00')[0]
tmpDict['ServiceName'] = string + '\x00'
enumServicesList.append(tmpDict)
index += len(serviceStatus)
return enumServicesList
def QueryServiceStatus(self, handle):
queryStatus = SVCCTLRQueryServiceStatus()
queryStatus['ContextHandle'] = handle
ans = self.doRequest(queryStatus, checkReturn = 1)
return SVCCTLServiceStatus(ans)
def QueryServiceConfigW(self, handle):
class configStrings(Structure):
structure = (
('BinaryPathName',':',ndrutils.NDRStringW),
('LoadOrderGroup',':',ndrutils.NDRStringW),
('Dependencies',':',ndrutils.NDRStringW),
('ServiceStartName',':',ndrutils.NDRStringW),
('DisplayName',':',ndrutils.NDRStringW),
)
serviceConfig = SVCCTLRQueryServiceConfigW()
# First packet is to get the buffer size we need to hold the answer
serviceConfig['ContextHandle'] = handle
serviceConfig['BuffSize'] = 0
ans = self.doRequest(serviceConfig, checkReturn = 0)
packet = SVCCTLRQueryServiceConfigWResponse()
packet['BufferSize'] = 0
packet.fromString(ans)
bytesNeeded = packet['BytesNeeded']
serviceConfig['BuffSize'] = bytesNeeded
# Now the actual request
ans = self.doRequest(serviceConfig, checkReturn = 1)
packet = SVCCTLRQueryServiceConfigWResponse()
packet['BufferSize'] = len(ans) - 36 - 8
packet.fromString(ans)
if packet['ErrorCode'] == 0:
confStr = configStrings(packet['StringsBuffer'])
packet['QueryConfig']['BinaryPathName'] = confStr['BinaryPathName']['Data']
packet['QueryConfig']['LoadOrderGroup'] = confStr['LoadOrderGroup']['Data']
packet['QueryConfig']['Dependencies'] = confStr['Dependencies']['Data']
packet['QueryConfig']['ServiceStartName'] = confStr['ServiceStartName']['Data']
packet['QueryConfig']['DisplayName'] = confStr['DisplayName']['Data']
return packet
|