コード例 #1
0
 def snmp_server(self):
     """
     SNMP server coroutine
     :return:
     """
     self.logger.info("Stating BEEF SNMP server")
     yield self.server_iostream.connect()
     decoder = BERDecoder()
     encoder = BEREncoder()
     while True:
         # Wait for SNMP request
         request = yield self.server_iostream.read_bytes(self.MAX_REQUEST_SIZE, partial=True)
         pdu = decoder.parse_sequence(request)[0]
         self.logger.info("SNMP REQUEST: %r", pdu)
         version = pdu[0]
         community = pdu[1]
         pdu_type = pdu[2][0]
         request_id = pdu[2][1]
         if pdu_type == PDU_GET_REQUEST:
             err_status, err_index, data = self.snmp_get_response(pdu)
         elif pdu_type == PDU_GETNEXT_REQUEST:
             err_status, err_index, data = self.snmp_getnext_response(pdu)
         elif pdu_type == PDU_GETBULK_REQUEST:
             err_status, err_index, data = self.snmp_getbulk_response(pdu)
         else:
             # @todo: Unsupported PDU type
             pass
         # Build response
         resp = encoder.encode_sequence(
             [
                 encoder.encode_int(version),
                 encoder.encode_octet_string(community),
                 encoder.encode_choice(
                     PDU_RESPONSE,
                     [
                         encoder.encode_int(request_id),
                         encoder.encode_int(err_status),
                         encoder.encode_int(err_index),
                         encoder.encode_sequence(
                             [
                                 encoder.encode_sequence([encoder.encode_oid(str(oid)), value])
                                 for oid, value in data
                             ]
                         ),
                     ],
                 ),
             ]
         )
         self.logger.info("RESPONSE = %r", data)
         yield self.server_iostream.write(resp)
コード例 #2
0
 async def send_and_receive(
         self, data: bytes,
         address: Tuple[str, int]) -> Tuple[bytes, Tuple[str, int]]:
     pdu = BERDecoder().parse_sequence(data)[0]
     self.logger.info("SNMP REQUEST: %r", pdu)
     version = pdu[0]
     community = pdu[1]
     pdu_type = pdu[2][0]
     request_id = pdu[2][1]
     if pdu_type == PDU_GET_REQUEST:
         err_status, err_index, data = self.snmp_get_response(pdu)
     elif pdu_type == PDU_GETNEXT_REQUEST:
         err_status, err_index, data = self.snmp_getnext_response(pdu)
     elif pdu_type == PDU_GETBULK_REQUEST:
         err_status, err_index, data = self.snmp_getbulk_response(pdu)
     else:
         raise ScriptError("Unknown PDU type")
     # Build response
     encoder = BEREncoder()
     response = encoder.encode_sequence([
         encoder.encode_int(version),
         encoder.encode_octet_string(community),
         encoder.encode_choice(
             PDU_RESPONSE,
             [
                 encoder.encode_int(request_id),
                 encoder.encode_int(err_status),
                 encoder.encode_int(err_index),
                 encoder.encode_sequence([
                     encoder.encode_sequence(
                         [encoder.encode_oid(str(oid)),
                          smart_bytes(value)]) for oid, value in data
                 ]),
             ],
         ),
     ])
     self.logger.info("RESPONSE = %r", data)
     return response, address
コード例 #3
0
def test_decode_null(raw):
    decoder = BERDecoder()
    assert decoder.parse_null(raw) is None
コード例 #4
0
def test_decode_p_octetstring(raw, value):
    decoder = BERDecoder()
    assert decoder.parse_p_octetstring(raw) == value
コード例 #5
0
def test_decode_real_error(raw, value):
    decoder = BERDecoder()
    with pytest.raises(Exception):
        assert decoder.parse_real(raw) == value
コード例 #6
0
def test_decode_real(raw, value):
    decoder = BERDecoder()
    assert decoder.parse_real(raw) == value
コード例 #7
0
def test_decode_opaque(raw, value):
    decoder = BERDecoder()
    assert decoder.parse_tlv(raw)[0] == value
コード例 #8
0
def test_decode_bool(raw, value):
    decoder = BERDecoder()
    assert decoder.parse_boolean(raw) is value
コード例 #9
0
def test_decode_a_ipaddress(raw, value):
    decoder = BERDecoder()
    assert decoder.parse_a_ipaddress(raw) == value