테스트는 C# 기반의 ASP.NET MVC 에서!
[참고 : http://j07051.tistory.com/556]
(test 하기 전에 Global.asax.cs 파일에서 초기 경로 수정해 줌. 안하고 url에 경로 치고 들어가도 되긴 하는데 귀찮아서....
url 경로 치고 들어가려면 테스트 url 뒤에 MaliciousCodeDetection)
Global.asax.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
namespace testProject
{
// Note: For instructions on enabling IIS6 or IIS7 classic mode,
// visit http://go.microsoft.com/?LinkId=9394801
public class MvcApplication : System.Web.HttpApplication
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "MaliciousCodeDetection", action = "Index", id = "" } // Parameter defaults
);
}
protected void Application_Start()
{
RegisterRoutes(RouteTable.Routes);
}
}
}
/Views/MaliciousCodeDetection/Index.aspx
<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>MaliciousCodeDetection</title>
</head>
<body>
<div>
<div>
VirusTotal
</div>
<form action="/MaliciousCodeDetection/callVirusTotalAPI">
<div>
<input type="hidden" name="function" value="/url/report" />
<input type="hidden" name="method" value="GET" />
<input name="url" type="text"/> <input type="submit" value="scan"/>
</div>
<div>
<%=ViewData["VirusTotalResult"]%>
</div>
</form>
</div>
</body>
</html>
/Controllers/MaliciousCodeDetectionController.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Ajax;
using System.Net;
using System.Text;
using System.IO;
using System.Web.Script.Serialization;
namespace moveDB.Controllers
{
public class MaliciousCodeDetectionController : Controller
{
String virusTotalPrivateAPIKey = "==YOUR API KEY HERE==";
String virusTotalURL = "https://www.virustotal.com/vtapi/v2";
Dictionary<string, string> virusTotalParam = new Dictionary<string, string>();
Dictionary<string, string> apiOptions = new Dictionary<string, string>()
{
{"/url/scan","&url="},
{"/url/report","&resource="}
};
public MaliciousCodeDetectionController()
{
this.virusTotalParam["apikey"] = virusTotalPrivateAPIKey;
}
public ActionResult Index()
{
ViewData["VirusTotalResult"] = "default";
return View();
}
public ActionResult callVirusTotalAPI(string function , string method, string url)
{
string response = this.CallApi(function, method, url); //api call 하고 결과 받아옴
ViewData["VirusTotalResult"] = this.buildHTML(response); //받아온 결과 출력 형식을 지정함
return View("Index");
}
public string CallApi(string function, string method, string url)
{
string response;
if (method.Equals("GET"))
{
response = SendWebGetRequest(function, url);
}
else
{
response = "Undefined Submit Method";
}
return response;
}
public object json2obj(string st)
{
JavaScriptSerializer deserializer = new JavaScriptSerializer();
return deserializer.Deserialize<object>(st);
}
public string obj2json(object obj)
{
JavaScriptSerializer serializer = new JavaScriptSerializer();
return serializer.Serialize(obj);
}
private string SendWebGetRequest(string function, string url)
{
string strUri = this.virusTotalURL + function + "?apikey=" + this.virusTotalPrivateAPIKey + this.apiOptions[function] + url;
WebRequest wb = WebRequest.Create(strUri);
wb.ContentType = @"application/json-rpc";
wb.Credentials = CredentialCache.DefaultCredentials;
wb.Method = "GET";
HttpWebResponse response = (HttpWebResponse)wb.GetResponse();
Stream dataStream = response.GetResponseStream();
StreamReader reader = new StreamReader(dataStream);
string responseFromServer = reader.ReadToEnd();
return responseFromServer;
}
private string buildHTML(string resource)
{
string html_body = "";
Dictionary<string, object> response_dic = (Dictionary<string, object>)(json2obj(resource));
Dictionary<string, object> scans_dic = (Dictionary<string, object>)response_dic["scans"];
KeyValuePair<string, object> component_dic;
KeyValuePair<string, object> result_dic;
foreach (object scan_result in scans_dic)
{
component_dic = (KeyValuePair<string, object>)scan_result;
html_body += "<b>" + component_dic.Key.ToString() + "</b><br />";
foreach (object component_result in (Dictionary<string, object>)component_dic.Value)
{
result_dic = (KeyValuePair<string, object>)component_result;
html_body += result_dic.Key.ToString() + " : " + result_dic.Value.ToString() + "<br />";
}
}
return html_body;
}
}
}
'000.프로그래밍 > 00. Personal Notes' 카테고리의 다른 글
passive 모드의 ftp 포트를 방화벽에서 차단하는 법 (0) | 2016.11.22 |
---|---|
iptables geoip 패키지 설치 (0) | 2016.11.21 |
php shell_exec로 sudo 명령어 수행하기 (0) | 2016.11.21 |
[번역]MongoDB Tutorials - Overview (0) | 2015.05.07 |
개념 정리 (asp.net -1) (0) | 2015.02.11 |