테스트는 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"/>&nbsp<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;

        }

    }

}









Posted by Righ
,