概要
cscの作法、調べてみた。
練習問題やってみた。
練習問題
guiでファイルをhashしてvirustotalに投げて、判定せよ。
写真
サンプルコード
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Windows.Forms;
using System.Drawing;
using LitJson;
using System.Net;
using System.Net.Http;
class form1: Form {
private TextBox textBox1;
private TextBox textBox2;
form1() {
Text = "virustotal";
ClientSize = new Size(300, 400);
Button btn1 = new Button();
btn1.Location = new Point(150, 150);
btn1.Text = "check";
btn1.Click += btn1_Click;
textBox1 = new TextBox();
textBox1.Location = new System.Drawing.Point(30, 30);
textBox1.Multiline = true;
textBox1.Size = new System.Drawing.Size(250, 50);
textBox1.TabIndex = 2;
textBox2 = new TextBox();
textBox2.Location = new System.Drawing.Point(30, 230);
textBox2.Multiline = true;
textBox2.Size = new System.Drawing.Size(250, 50);
textBox2.TabIndex = 2;
Controls.AddRange(new Control[] {
btn1,
textBox1,
textBox2
});
}
void btn1_Click(object sender, System.EventArgs e) {
string BaseDirectory = Environment.CurrentDirectory;
string fileContent = string.Empty;
string filePath = string.Empty;
using (OpenFileDialog openFileDialog = new OpenFileDialog())
{
openFileDialog.InitialDirectory = BaseDirectory;
openFileDialog.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
openFileDialog.FilterIndex = 2;
openFileDialog.RestoreDirectory = true;
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
filePath = openFileDialog.FileName;
Console.WriteLine(filePath);
byte[] byteValue = File.ReadAllBytes(filePath);
SHA256 crypto = new SHA256CryptoServiceProvider();
byte[] hashValue = crypto.ComputeHash(byteValue);
var hash = String.Join("", hashValue.Select(x => x.ToString("x2")).ToArray());
Console.WriteLine(hash);
textBox1.Text = hash;
}
}
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12;
string url = "https://www.virustotal.com/vtapi/v2/file/report?apikey=******&resource=" + textBox1.Text;
HttpClient client = new HttpClient();
string response = client.GetStringAsync(url).Result;
LitJson.JsonData json = LitJson.JsonMapper.ToObject(response);
try
{
Console.WriteLine("total: " + json["total"]);
Console.WriteLine("positive: " + json["positives"]);
textBox2.Text = json["positives"] + " / " + json["total"];
}
catch (Exception ex)
{
Console.WriteLine("pass");
textBox2.Text = " pass";
}
}
[STAThread]
public static void Main() {
Application.Run(new form1());
}
}
以上。