Ejemplo n.º 1
0
    def handle(self):
        try:
            self.request.settimeout(0.5)
            data = self.request.recv(1024)

            if data[0] == "\x81":  #session request 139
                Buffer = "\x82\x00\x00\x00"
                self.request.send(Buffer)
                data = self.request.recv(1024)

            if data[8:10] == "\x72\x00":  #Negotiate proto answer.
                head = SMBHeader(cmd="\x72",flag1="\x80", flag2="\x00\x00",pid=pidcalc(data),mid=midcalc(data))
                Body = SMBNegoAnsLM(Dialect=Parse_Nego_Dialect(data),Domain="",Key=settings.Config.Challenge)
                Body.calculate()
                Packet = str(head)+str(Body)
                Buffer = struct.pack(">i", len(''.join(Packet)))+Packet
                self.request.send(Buffer)
                data = self.request.recv(1024)

            if data[8:10] == "\x73\x00":  #Session Setup AndX Request
                if Is_LMNT_Anonymous(data):
                    head = SMBHeader(cmd="\x73",flag1="\x90", flag2="\x53\xc8",errorcode="\x72\x00\x00\xc0",pid=pidcalc(data),tid=tidcalc(data),uid=uidcalc(data),mid=midcalc(data))
                    Packet = str(head)+str(SMBSessEmpty())
                    Buffer = struct.pack(">i", len(''.join(Packet)))+Packet
                    self.request.send(Buffer)
                else:
                    ParseLMNTHash(data,self.client_address[0])
                    head = SMBHeader(cmd="\x73",flag1="\x90", flag2="\x53\xc8",errorcode="\x22\x00\x00\xc0",pid=pidcalc(data),tid=tidcalc(data),uid=uidcalc(data),mid=midcalc(data))
                    Packet = str(head) + str(SMBSessEmpty())
                    Buffer = struct.pack(">i", len(''.join(Packet))) + Packet
                    self.request.send(Buffer)
                    data = self.request.recv(1024)
        except Exception:
            self.request.close()
            pass
Ejemplo n.º 2
0
    def handle(self):
        try:
            self.ntry = 0
            while True:
                data = self.request.recv(1024)
                self.request.settimeout(1)

                if not data:
                    break

                if data[0] == "\x81":  #session request 139
                    Buffer = "\x82\x00\x00\x00"
                    try:
                        self.request.send(Buffer)
                        data = self.request.recv(1024)
                    except:
                        pass

                if data[8:10] == "\x72\x00":  # Negociate Protocol Response
                    Header = SMBHeader(cmd="\x72",
                                       flag1="\x88",
                                       flag2="\x01\xc8",
                                       pid=pidcalc(data),
                                       mid=midcalc(data))
                    Body = SMBNegoKerbAns(Dialect=Parse_Nego_Dialect(data))
                    Body.calculate()

                    Packet = str(Header) + str(Body)
                    Buffer = struct.pack(">i", len(''.join(Packet))) + Packet

                    self.request.send(Buffer)
                    data = self.request.recv(1024)

                if data[8:10] == "\x73\x00":  # Session Setup AndX Request
                    IsNT4ClearTxt(data, self.client_address[0])

                    # STATUS_MORE_PROCESSING_REQUIRED
                    Header = SMBHeader(cmd="\x73",
                                       flag1="\x88",
                                       flag2="\x01\xc8",
                                       errorcode="\x16\x00\x00\xc0",
                                       uid=chr(randrange(256)) +
                                       chr(randrange(256)),
                                       pid=pidcalc(data),
                                       tid="\x00\x00",
                                       mid=midcalc(data))
                    if settings.Config.CaptureMultipleCredentials and self.ntry == 0:
                        Body = SMBSession1Data(
                            NTLMSSPNtServerChallenge=settings.Config.Challenge,
                            NTLMSSPNTLMChallengeAVPairsUnicodeStr="NOMATCH")
                    else:
                        Body = SMBSession1Data(
                            NTLMSSPNtServerChallenge=settings.Config.Challenge)
                    Body.calculate()

                    Packet = str(Header) + str(Body)
                    Buffer = struct.pack(">i", len(''.join(Packet))) + Packet

                    self.request.send(Buffer)
                    data = self.request.recv(4096)

                    if data[8:10] == "\x73\x00":  # STATUS_SUCCESS
                        if Is_Anonymous(data):
                            Header = SMBHeader(
                                cmd="\x73",
                                flag1="\x98",
                                flag2="\x01\xc8",
                                errorcode="\x72\x00\x00\xc0",
                                pid=pidcalc(data),
                                tid="\x00\x00",
                                uid=uidcalc(data),
                                mid=midcalc(data)
                            )  ###should always send errorcode="\x72\x00\x00\xc0" account disabled for anonymous logins.
                            Body = SMBSessEmpty()

                            Packet = str(Header) + str(Body)
                            Buffer = struct.pack(">i", len(
                                ''.join(Packet))) + Packet

                            self.request.send(Buffer)

                        else:
                            # Parse NTLMSSP_AUTH packet
                            ParseSMBHash(data, self.client_address[0])

                            if settings.Config.CaptureMultipleCredentials and self.ntry == 0:
                                # Send ACCOUNT_DISABLED to get multiple hashes if there are any
                                Header = SMBHeader(
                                    cmd="\x73",
                                    flag1="\x98",
                                    flag2="\x01\xc8",
                                    errorcode="\x72\x00\x00\xc0",
                                    pid=pidcalc(data),
                                    tid="\x00\x00",
                                    uid=uidcalc(data),
                                    mid=midcalc(data)
                                )  ###should always send errorcode="\x72\x00\x00\xc0" account disabled for anonymous logins.
                                Body = SMBSessEmpty()

                                Packet = str(Header) + str(Body)
                                Buffer = struct.pack(">i", len(
                                    ''.join(Packet))) + Packet

                                self.request.send(Buffer)
                                self.ntry += 1
                                continue

                            # Send STATUS_SUCCESS
                            Header = SMBHeader(cmd="\x73",
                                               flag1="\x98",
                                               flag2="\x01\xc8",
                                               errorcode="\x00\x00\x00\x00",
                                               pid=pidcalc(data),
                                               tid=tidcalc(data),
                                               uid=uidcalc(data),
                                               mid=midcalc(data))
                            Body = SMBSession2Accept()
                            Body.calculate()

                            Packet = str(Header) + str(Body)
                            Buffer = struct.pack(">i", len(
                                ''.join(Packet))) + Packet

                            self.request.send(Buffer)
                            data = self.request.recv(1024)

                if data[8:10] == "\x75\x00":  # Tree Connect AndX Request
                    ParseShare(data)
                    Header = SMBHeader(cmd="\x75",
                                       flag1="\x88",
                                       flag2="\x01\xc8",
                                       errorcode="\x00\x00\x00\x00",
                                       pid=pidcalc(data),
                                       tid=chr(randrange(256)) +
                                       chr(randrange(256)),
                                       uid=uidcalc(data),
                                       mid=midcalc(data))
                    Body = SMBTreeData()
                    Body.calculate()

                    Packet = str(Header) + str(Body)
                    Buffer = struct.pack(">i", len(''.join(Packet))) + Packet

                    self.request.send(Buffer)
                    data = self.request.recv(1024)

                if data[8:10] == "\x71\x00":  #Tree Disconnect
                    Header = SMBHeader(cmd="\x71",
                                       flag1="\x98",
                                       flag2="\x07\xc8",
                                       errorcode="\x00\x00\x00\x00",
                                       pid=pidcalc(data),
                                       tid=tidcalc(data),
                                       uid=uidcalc(data),
                                       mid=midcalc(data))
                    Body = "\x00\x00\x00"

                    Packet = str(Header) + str(Body)
                    Buffer = struct.pack(">i", len(''.join(Packet))) + Packet

                    self.request.send(Buffer)
                    data = self.request.recv(1024)

                if data[8:10] == "\xa2\x00":  #NT_CREATE Access Denied.
                    Header = SMBHeader(cmd="\xa2",
                                       flag1="\x98",
                                       flag2="\x07\xc8",
                                       errorcode="\x22\x00\x00\xc0",
                                       pid=pidcalc(data),
                                       tid=tidcalc(data),
                                       uid=uidcalc(data),
                                       mid=midcalc(data))
                    Body = "\x00\x00\x00"

                    Packet = str(Header) + str(Body)
                    Buffer = struct.pack(">i", len(''.join(Packet))) + Packet

                    self.request.send(Buffer)
                    data = self.request.recv(1024)

                if data[8:10] == "\x25\x00":  # Trans2 Access Denied.
                    Header = SMBHeader(cmd="\x25",
                                       flag1="\x98",
                                       flag2="\x07\xc8",
                                       errorcode="\x22\x00\x00\xc0",
                                       pid=pidcalc(data),
                                       tid=tidcalc(data),
                                       uid=uidcalc(data),
                                       mid=midcalc(data))
                    Body = "\x00\x00\x00"

                    Packet = str(Header) + str(Body)
                    Buffer = struct.pack(">i", len(''.join(Packet))) + Packet

                    self.request.send(Buffer)
                    data = self.request.recv(1024)

                if data[8:10] == "\x74\x00":  # LogOff
                    Header = SMBHeader(cmd="\x74",
                                       flag1="\x98",
                                       flag2="\x07\xc8",
                                       errorcode="\x22\x00\x00\xc0",
                                       pid=pidcalc(data),
                                       tid=tidcalc(data),
                                       uid=uidcalc(data),
                                       mid=midcalc(data))
                    Body = "\x02\xff\x00\x27\x00\x00\x00"

                    Packet = str(Header) + str(Body)
                    Buffer = struct.pack(">i", len(''.join(Packet))) + Packet

                    self.request.send(Buffer)
                    data = self.request.recv(1024)

        except socket.timeout:
            pass
Ejemplo n.º 3
0
    def handle(self):
        try:
            while True:
                data = self.request.recv(1024)
                self.request.settimeout(1)

                if len(data) < 1:
                    break

                ##session request 139
                if data[0] == "\x81":
                    Buffer = "\x82\x00\x00\x00"
                    try:
                        self.request.send(Buffer)
                        data = self.request.recv(1024)
                    except:
                        pass

                # Negociate Protocol Response
                if data[8:10] == "\x72\x00":
                    # \x72 == Negociate Protocol Response
                    Header = SMBHeader(cmd="\x72",flag1="\x88", flag2="\x01\xc8", pid=pidcalc(data),mid=midcalc(data))
                    Body = SMBNegoKerbAns(Dialect=Parse_Nego_Dialect(data))
                    Body.calculate()
        
                    Packet = str(Header)+str(Body)
                    Buffer = struct.pack(">i", len(''.join(Packet)))+Packet

                    self.request.send(Buffer)
                    data = self.request.recv(1024)

                # Session Setup AndX Request
                if data[8:10] == "\x73\x00":
                    IsNT4ClearTxt(data, self.client_address[0])
                    
                    # STATUS_MORE_PROCESSING_REQUIRED
                    Header = SMBHeader(cmd="\x73",flag1="\x88", flag2="\x01\xc8", errorcode="\x16\x00\x00\xc0", uid=chr(randrange(256))+chr(randrange(256)),pid=pidcalc(data),tid="\x00\x00",mid=midcalc(data))
                    Body = SMBSession1Data(NTLMSSPNtServerChallenge=settings.Config.Challenge)
                    Body.calculate()
        
                    Packet = str(Header)+str(Body)
                    Buffer = struct.pack(">i", len(''.join(Packet)))+Packet

                    self.request.send(Buffer)
                    data = self.request.recv(4096)

                    # STATUS_SUCCESS
                    if data[8:10] == "\x73\x00":
                        if Is_Anonymous(data):
                            Header = SMBHeader(cmd="\x73",flag1="\x98", flag2="\x01\xc8",errorcode="\x72\x00\x00\xc0",pid=pidcalc(data),tid="\x00\x00",uid=uidcalc(data),mid=midcalc(data))###should always send errorcode="\x72\x00\x00\xc0" account disabled for anonymous logins.
                            Body = SMBSessEmpty()

                            Packet = str(Header)+str(Body)
                            Buffer = struct.pack(">i", len(''.join(Packet)))+Packet

                            self.request.send(Buffer)

                        else:
                            # Parse NTLMSSP_AUTH packet
                            ParseSMBHash(data,self.client_address[0])

                            # Send STATUS_SUCCESS
                            Header = SMBHeader(cmd="\x73",flag1="\x98", flag2="\x01\xc8", errorcode="\x00\x00\x00\x00",pid=pidcalc(data),tid=tidcalc(data),uid=uidcalc(data),mid=midcalc(data))
                            Body = SMBSession2Accept()
                            Body.calculate()

                            Packet = str(Header)+str(Body)
                            Buffer = struct.pack(">i", len(''.join(Packet)))+Packet

                            self.request.send(Buffer)
                            data = self.request.recv(1024)
                
                # Tree Connect AndX Request
                if data[8:10] == "\x75\x00":
                    ParseShare(data)
                    # Tree Connect AndX Response
                    Header = SMBHeader(cmd="\x75",flag1="\x88", flag2="\x01\xc8", errorcode="\x00\x00\x00\x00", pid=pidcalc(data), tid=chr(randrange(256))+chr(randrange(256)), uid=uidcalc(data), mid=midcalc(data))
                    Body = SMBTreeData()
                    Body.calculate()

                    Packet = str(Header)+str(Body)
                    Buffer = struct.pack(">i", len(''.join(Packet)))+Packet

                    self.request.send(Buffer)
                    data = self.request.recv(1024)

                ##Tree Disconnect.
                if data[8:10] == "\x71\x00":
                    Header = SMBHeader(cmd="\x71",flag1="\x98", flag2="\x07\xc8", errorcode="\x00\x00\x00\x00",pid=pidcalc(data),tid=tidcalc(data),uid=uidcalc(data),mid=midcalc(data))
                    Body = "\x00\x00\x00"

                    Packet = str(Header)+str(Body)
                    Buffer = struct.pack(">i", len(''.join(Packet)))+Packet
                    
                    self.request.send(Buffer)
                    data = self.request.recv(1024)
                
                ##NT_CREATE Access Denied.
                if data[8:10] == "\xa2\x00":
                    Header = SMBHeader(cmd="\xa2",flag1="\x98", flag2="\x07\xc8", errorcode="\x22\x00\x00\xc0",pid=pidcalc(data),tid=tidcalc(data),uid=uidcalc(data),mid=midcalc(data))
                    Body = "\x00\x00\x00"

                    Packet = str(Header)+str(Body)
                    Buffer = struct.pack(">i", len(''.join(Packet)))+Packet

                    self.request.send(Buffer)
                    data = self.request.recv(1024)
                
                ##Trans2 Access Denied.
                if data[8:10] == "\x25\x00":
                    Header = SMBHeader(cmd="\x25",flag1="\x98", flag2="\x07\xc8", errorcode="\x22\x00\x00\xc0",pid=pidcalc(data),tid=tidcalc(data),uid=uidcalc(data),mid=midcalc(data))
                    Body = "\x00\x00\x00"

                    Packet = str(Header)+str(Body)
                    Buffer = struct.pack(">i", len(''.join(Packet)))+Packet

                    self.request.send(Buffer)
                    data = self.request.recv(1024)
                
                ##LogOff.
                if data[8:10] == "\x74\x00":
                    Header = SMBHeader(cmd="\x74",flag1="\x98", flag2="\x07\xc8", errorcode="\x22\x00\x00\xc0",pid=pidcalc(data),tid=tidcalc(data),uid=uidcalc(data),mid=midcalc(data))
                    Body = "\x02\xff\x00\x27\x00\x00\x00"

                    Packet = str(Header)+str(Body)
                    Buffer = struct.pack(">i", len(''.join(Packet)))+Packet

                    self.request.send(Buffer)
                    data = self.request.recv(1024)

        except socket.timeout:
            pass