Obtén las diferencias de una lista de amenazas desde la última actualización
Explora más
Para obtener documentación en la que se incluye esta muestra de código, consulta lo siguiente:
Muestra de código
Java
import com.google.cloud.webrisk.v1.WebRiskServiceClient;
import com.google.protobuf.ByteString;
import com.google.webrisk.v1.CompressionType;
import com.google.webrisk.v1.ComputeThreatListDiffRequest;
import com.google.webrisk.v1.ComputeThreatListDiffRequest.Constraints;
import com.google.webrisk.v1.ComputeThreatListDiffResponse;
import com.google.webrisk.v1.ThreatType;
import java.io.IOException;
public class ComputeThreatListDiff {
public static void main(String[] args) throws IOException {
// TODO(developer): Replace these variables before running the sample.
// The threat list to update. Only a single ThreatType should be specified per request.
ThreatType threatType = ThreatType.MALWARE;
// The current version token of the client for the requested list. If the client does not have
// a version token (this is the first time calling ComputeThreatListDiff), this may be
// left empty and a full database snapshot will be returned.
ByteString versionToken = ByteString.EMPTY;
// The maximum size in number of entries. The diff will not contain more entries
// than this value. This should be a power of 2 between 2**10 and 2**20.
// If zero, no diff size limit is set.
int maxDiffEntries = 1024;
// Sets the maximum number of entries that the client is willing to have in the local database.
// This should be a power of 2 between 2**10 and 2**20. If zero, no database size limit is set.
int maxDatabaseEntries = 1024;
// The compression type supported by the client.
CompressionType compressionType = CompressionType.RAW;
computeThreatDiffList(threatType, versionToken, maxDiffEntries, maxDatabaseEntries,
compressionType);
}
// Gets the most recent threat list diffs. These diffs should be applied to a local database of
// hashes to keep it up-to-date.
// If the local database is empty or excessively out-of-date,
// a complete snapshot of the database will be returned. This Method only updates a
// single ThreatList at a time. To update multiple ThreatList databases, this method needs to be
// called once for each list.
public static void computeThreatDiffList(ThreatType threatType, ByteString versionToken,
int maxDiffEntries, int maxDatabaseEntries, CompressionType compressionType)
throws IOException {
// Initialize client that will be used to send requests. This client only needs to be created
// once, and can be reused for multiple requests. After completing all of your requests, call
// the `webRiskServiceClient.close()` method on the client to safely
// clean up any remaining background resources.
try (WebRiskServiceClient webRiskServiceClient = WebRiskServiceClient.create()) {
Constraints constraints = Constraints.newBuilder()
.setMaxDiffEntries(maxDiffEntries)
.setMaxDatabaseEntries(maxDatabaseEntries)
.addSupportedCompressions(compressionType)
.build();
ComputeThreatListDiffResponse response = webRiskServiceClient.computeThreatListDiff(
ComputeThreatListDiffRequest.newBuilder()
.setThreatType(threatType)
.setVersionToken(versionToken)
.setConstraints(constraints)
.build());
// The returned response contains the following information:
// https://cloud.google.com/web-risk/docs/reference/rpc/google.cloud.webrisk.v1#computethreatlistdiffresponse
// Type of response: DIFF/ RESET/ RESPONSE_TYPE_UNSPECIFIED
System.out.println(response.getResponseType());
// List of entries to add and/or remove.
// System.out.println(response.getAdditions());
// System.out.println(response.getRemovals());
// New version token to be used the next time when querying.
System.out.println(response.getNewVersionToken());
// Recommended next diff timestamp.
System.out.println(response.getRecommendedNextDiff());
System.out.println("Obtained threat list diff.");
}
}
}
Python
from google.cloud import webrisk_v1
def compute_threatlist_diff(
threat_type: webrisk_v1.ThreatType,
version_token: bytes,
max_diff_entries: int,
max_database_entries: int,
compression_type: webrisk_v1.CompressionType,
) -> None:
"""Gets the most recent threat list diffs. These diffs should be applied to a local database of
hashes to keep it up-to-date.
If the local database is empty or excessively out-of-date,
a complete snapshot of the database will be returned. This Method only updates a
single ThreatList at a time. To update multiple ThreatList databases, this method needs to be
called once for each list.
Args:
threat_type: The threat list to update. Only a single ThreatType should be specified per request.
threat_type = webrisk_v1.ThreatType.MALWARE
version_token: The current version token of the client for the requested list. If the
client does not have a version token (this is the first time calling ComputeThreatListDiff),
this may be left empty and a full database snapshot will be returned.
max_diff_entries: The maximum size in number of entries. The diff will not contain more entries
than this value. This should be a power of 2 between 2**10 and 2**20.
If zero, no diff size limit is set.
max_diff_entries = 1024
max_database_entries: Sets the maximum number of entries that the client is willing to have in the local database.
This should be a power of 2 between 2**10 and 2**20. If zero, no database size limit is set.
max_database_entries = 1024
compression_type: The compression type supported by the client.
compression_type = webrisk_v1.CompressionType.RAW
"""
webrisk_client = webrisk_v1.WebRiskServiceClient()
constraints = webrisk_v1.ComputeThreatListDiffRequest.Constraints()
constraints.max_diff_entries = max_diff_entries
constraints.max_database_entries = max_database_entries
constraints.supported_compressions = [compression_type]
request = webrisk_v1.ComputeThreatListDiffRequest()
request.threat_type = threat_type
request.version_token = version_token
request.constraints = constraints
response = webrisk_client.compute_threat_list_diff(request)
# The returned response contains the following information:
# https://cloud.google.com/web-risk/docs/reference/rpc/google.cloud.webrisk.v1#computethreatlistdiffresponse
# Type of response: DIFF/ RESET/ RESPONSE_TYPE_UNSPECIFIED
print(response.response_type)
# New version token to be used the next time when querying.
print(response.new_version_token)
# Recommended next diff timestamp.
print(response.recommended_next_diff)
print("Obtained threat list diff.")
¿Qué sigue?
Para buscar y filtrar muestras de código en otros productos de Google Cloud, consulta el navegador de muestra de Google Cloud.