431 lines
18 KiB
C#
431 lines
18 KiB
C#
/*
|
|
*
|
|
* Copyright (c) Microsoft Corporation.
|
|
* All rights reserved.
|
|
*
|
|
* This code is licensed under the MIT License.
|
|
*
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
* of this software and associated documentation files(the "Software"), to deal
|
|
* in the Software without restriction, including without limitation the rights
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
* furnished to do so, subject to the following conditions :
|
|
*
|
|
* The above copyright notice and this permission notice shall be included in
|
|
* all copies or substantial portions of the Software.
|
|
*
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
* THE SOFTWARE.
|
|
*
|
|
*/
|
|
|
|
using System;
|
|
using System.Threading.Tasks;
|
|
using Microsoft.InformationProtection;
|
|
using Microsoft.InformationProtection.File;
|
|
using Microsoft.InformationProtection.Protection;
|
|
using Microsoft.InformationProtection.Policy;
|
|
using System.Collections.Generic;
|
|
using System.Configuration;
|
|
using static BSOneMip.Define;
|
|
using System.Diagnostics.Eventing.Reader;
|
|
using static PassMessage.MessagePasser;
|
|
using PassMessage;
|
|
using System.Runtime.InteropServices;
|
|
using System.Data;
|
|
using System.IO;
|
|
|
|
namespace BSOneMip
|
|
{
|
|
class Program
|
|
{
|
|
private static readonly string clientId = APP_ID; // ConfigurationManager.AppSettings["ida:ClientId"];
|
|
private static readonly string appName = APP_NAME; // ConfigurationManager.AppSettings["app:Name"];
|
|
private static readonly string appVersion = APP_VER; // ConfigurationManager.AppSettings["app:Version"];
|
|
|
|
[DllImport("kernel32.dll", EntryPoint = "LoadLibrary")]
|
|
private extern static IntPtr LoadLibrary(string librayName);
|
|
|
|
[DllImport("kernel32.dll", EntryPoint = "GetProcAddress", CharSet = CharSet.Ansi)]
|
|
private extern static IntPtr GetProcAddress(IntPtr hwnd, string procedureName);
|
|
|
|
[DllImport("kernel32.dll", EntryPoint = "FreeLibrary")]
|
|
private extern static bool FreeLibrary(IntPtr hModule);
|
|
|
|
[DllImport("kernel32")]
|
|
public static extern Int32 GetLastError();
|
|
|
|
[UnmanagedFunctionPointer(CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
|
|
private delegate int FP_DSCSIsEncryptedFile(string sPath);
|
|
|
|
[UnmanagedFunctionPointer(CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
|
|
private delegate int FP_DSCSDecryptFile(string sSrcPath, string sDestPath);
|
|
|
|
private static FP_DSCSIsEncryptedFile DSCSIsEncryptedFile;
|
|
private static FP_DSCSDecryptFile DSCSDecryptFile;
|
|
|
|
static void Main(string[] args)
|
|
{
|
|
string sEncPath = "";
|
|
string sDecPath = "";
|
|
string sLabelId = "";
|
|
string sDelegatedUserEmail = "";
|
|
bool bInputMode = false;
|
|
int nMode = 0; // 0 : 복호화, 1 : 암호화, 2 : 레이블 설정, 3 : 통신 모드, 4 : 레이블 제거
|
|
|
|
try
|
|
{
|
|
for (var i = 0; i < args.Length; i++)
|
|
{
|
|
var arg = args[i];
|
|
|
|
if (arg == "-i" || arg == "-I")
|
|
{
|
|
bInputMode = true;
|
|
break;
|
|
}
|
|
|
|
if (arg == "-e" || arg == "-E")
|
|
{
|
|
nMode = 1;
|
|
continue;
|
|
}
|
|
|
|
if (arg == "-l" || arg == "-L")
|
|
{
|
|
nMode = 2;
|
|
continue;
|
|
}
|
|
|
|
if (arg == "-r" || arg == "-R")
|
|
{
|
|
nMode = 3;
|
|
if (i < args.Length - 1)
|
|
sDelegatedUserEmail = args[i + 1];
|
|
break;
|
|
}
|
|
|
|
if (arg == "-x" || arg == "-X")
|
|
{
|
|
nMode = 4;
|
|
continue;
|
|
}
|
|
|
|
if (arg == "-p" || arg == "-P")
|
|
{
|
|
nMode = 5;
|
|
continue;
|
|
}
|
|
|
|
if (arg == "-b" || arg == "-B")
|
|
{
|
|
nMode = 6;
|
|
continue;
|
|
}
|
|
|
|
sEncPath = arg;
|
|
if (i < args.Length - 1)
|
|
{
|
|
sDecPath = args[i + 1];
|
|
i++;
|
|
|
|
if (nMode == 2)
|
|
{
|
|
sLabelId = args[i + 1];
|
|
i++;
|
|
}
|
|
}
|
|
}
|
|
|
|
bool result = false;
|
|
|
|
if (nMode == 3)
|
|
{
|
|
ApplicationInfo apInfo = new ApplicationInfo()
|
|
{
|
|
// ApplicationId should ideally be set to the same ClientId found in the Azure AD App Registration.
|
|
// This ensures that the clientID in AAD matches the AppId reported in AIP Analytics.
|
|
ApplicationId = clientId,
|
|
ApplicationName = appName,
|
|
ApplicationVersion = appVersion
|
|
};
|
|
|
|
BS1Aip.RcvCmd(new Action(apInfo, sDelegatedUserEmail), new Action(apInfo)); // 이렇게 하면 권한 오류 발생함. 재부팅 전까지 권한발급 안됨 24_0723 kku
|
|
//BS1Aip.RcvCmd(new Action(apInfo, sDelegatedUserEmail));
|
|
|
|
return;
|
|
}
|
|
|
|
//return; // 연동 외 cmd 명령 실행방식 지원하지 않도록 조치 23_1025 kku
|
|
|
|
if (bInputMode)
|
|
{
|
|
Console.Write("AIP로 암호화된 파일 경로 : ");
|
|
sEncPath = Console.ReadLine();
|
|
|
|
Console.Write("복호화해서 저장될 파일 경로: ");
|
|
sDecPath = Console.ReadLine();
|
|
}
|
|
|
|
//if (sEncPath == "" || ((nMode < 2) && sDecPath == ""))
|
|
// return;
|
|
//Console.WriteLine(string.Format("SrcPath : {0}", sEncPath));
|
|
//Console.WriteLine(string.Format("DestPath : {0}", sDecPath));
|
|
|
|
// Create ApplicationInfo, setting the clientID from Azure AD App Registration as the ApplicationId
|
|
// If any of these values are not set API throws BadInputException.
|
|
ApplicationInfo appInfo = new ApplicationInfo()
|
|
{
|
|
// ApplicationId should ideally be set to the same ClientId found in the Azure AD App Registration.
|
|
// This ensures that the clientID in AAD matches the AppId reported in AIP Analytics.
|
|
ApplicationId = clientId,
|
|
ApplicationName = appName,
|
|
ApplicationVersion = appVersion
|
|
};
|
|
|
|
// Initialize Action class, passing in AppInfo.
|
|
Action action = new Action(appInfo);
|
|
|
|
//if (nMode == 1)
|
|
//{
|
|
/*
|
|
var templates = action.ListTemplates();
|
|
|
|
for (int i = 0; i < templates.Count; i++)
|
|
{
|
|
Console.WriteLine("{0}: Name={1}, ID={2}", i.ToString(), templates[i].Name, templates[i].Id);
|
|
}
|
|
*/
|
|
//}
|
|
|
|
|
|
// List all labels available to the engine created in Action
|
|
IEnumerable<Label> labels = action.ListLabels();
|
|
|
|
Console.WriteLine("레이블 정보 확인 ..");
|
|
Console.WriteLine("");
|
|
// Enumerate parent and child labels and print name/ID.
|
|
foreach (var label in labels)
|
|
{
|
|
Console.WriteLine(string.Format("{0} - {1}", label.Name, label.Id));
|
|
|
|
if (label.Children.Count > 0)
|
|
{
|
|
foreach (Label child in label.Children)
|
|
{
|
|
Console.WriteLine(string.Format("\t{0} - {1}", child.Name, child.Id));
|
|
}
|
|
}
|
|
}
|
|
Console.WriteLine("");
|
|
|
|
|
|
/*
|
|
// Prompt user to enter a label ID from above
|
|
Console.Write("Enter a label identifier from above: ");
|
|
var labelId = Console.ReadLine();
|
|
|
|
// Prompt for path inputs
|
|
Console.Write("Enter an input file path: ");
|
|
string sEncPath = Console.ReadLine();
|
|
|
|
Console.Write("Enter an output file path: ");
|
|
string sDecPath = Console.ReadLine();
|
|
|
|
// Set file options from FileOptions struct. Used to set various parameters for FileHandler
|
|
Action.FileOptions options = new Action.FileOptions
|
|
{
|
|
FileName = sEncPath,
|
|
OutputName = sDecPath,
|
|
ActionSource = ActionSource.Manual,
|
|
AssignmentMethod = AssignmentMethod.Privileged,
|
|
DataState = DataState.Rest,
|
|
GenerateChangeAuditEvent = true,
|
|
IsAuditDiscoveryEnabled = true,
|
|
LabelId = labelId
|
|
};
|
|
|
|
//Set the label on the file handler object
|
|
Console.WriteLine(string.Format("Set label ID {0} on {1}", labelId, sEncPath));
|
|
|
|
// Set label, commit change to outputfile, and send audit event if enabled.
|
|
bool result = action.SetLabel(options);
|
|
*/
|
|
|
|
// Set file options from FileOptions struct. Used to set various parameters for FileHandler
|
|
Action.FileOptions options = new Action.FileOptions
|
|
{
|
|
FileName = sEncPath,
|
|
OutputName = sDecPath,
|
|
ActionSource = ActionSource.Manual,
|
|
AssignmentMethod = AssignmentMethod.Privileged,
|
|
DataState = DataState.Rest,
|
|
GenerateChangeAuditEvent = true,
|
|
IsAuditDiscoveryEnabled = true,
|
|
LabelId = sLabelId
|
|
};
|
|
|
|
//Set the label on the file handler object
|
|
|
|
// Set label, commit change to outputfile, and send audit event if enabled.
|
|
|
|
switch (nMode)
|
|
{
|
|
case 0:
|
|
result = action.RemoveProtection(options);
|
|
break;
|
|
//string sMail = "2323308@hec.co.kr";
|
|
//int i = sMail.IndexOf('@');
|
|
//if (i > 0)
|
|
//{
|
|
// sMail = sMail.Substring(0, i);
|
|
//}
|
|
|
|
//string sOwner = action.GetOwner(sEncPath);
|
|
//i = sOwner.IndexOf('@');
|
|
//if ( i > 0)
|
|
//{
|
|
// sOwner = sOwner.Substring(0, i);
|
|
//}
|
|
|
|
//if (sMail == sOwner)
|
|
// Console.WriteLine(string.Format("사용자 (true) : {0}, Chk : {1}", sOwner, sMail));
|
|
//else
|
|
// Console.WriteLine(string.Format("사용자 (false) : {0}, Chk : {1}", sOwner, sMail));
|
|
//return;
|
|
case 1:
|
|
result = action.SetProtection(options);
|
|
break;
|
|
case 2:
|
|
result = action.SetLabel(options);
|
|
break;
|
|
case 4:
|
|
result = action.RemoveLabel(options);
|
|
break;
|
|
case 5:
|
|
//Directory.SetCurrentDirectory("C:\\Windows");
|
|
IntPtr hMod;
|
|
//if (4 < nVoidPtrLen)
|
|
hMod = LoadLibrary("C:\\Windows\\DSCSLink64.dll");
|
|
//else
|
|
//hMod = LoadLibrary("C:\\Windows\\DSCSLink.dll");
|
|
|
|
if (hMod != IntPtr.Zero)
|
|
{
|
|
IntPtr pFuncAddr = IntPtr.Zero;
|
|
pFuncAddr = GetProcAddress(hMod, "DSCSIsEncryptedFile");
|
|
if (pFuncAddr != IntPtr.Zero)
|
|
DSCSIsEncryptedFile = (FP_DSCSIsEncryptedFile)Marshal.GetDelegateForFunctionPointer(
|
|
pFuncAddr, typeof(FP_DSCSIsEncryptedFile));
|
|
else
|
|
Console.WriteLine(string.Format("GetProcAddress 실패 .. Error = {0}", GetLastError()));
|
|
|
|
pFuncAddr = GetProcAddress(hMod, "DSCSDecryptFile");
|
|
if (pFuncAddr != IntPtr.Zero)
|
|
DSCSDecryptFile = (FP_DSCSDecryptFile)Marshal.GetDelegateForFunctionPointer(
|
|
pFuncAddr, typeof(FP_DSCSDecryptFile));
|
|
|
|
if ((DSCSIsEncryptedFile != null) && (DSCSDecryptFile != null))
|
|
{
|
|
if (DSCSIsEncryptedFile(sEncPath) == 1)
|
|
result = DSCSDecryptFile(sEncPath, sDecPath) == 1;
|
|
}
|
|
//FreeLibrary(hMod);
|
|
}
|
|
break;
|
|
case 6:
|
|
var ALabel = action.GetLabel(options);
|
|
var sLabelName = "None";
|
|
if (ALabel != null)
|
|
sLabelName = ALabel.Label.Name;
|
|
|
|
Console.WriteLine(string.Format("레이블 : {0}", sLabelName));
|
|
return;
|
|
}
|
|
|
|
if (result)
|
|
{
|
|
//Console.WriteLine(string.Format("Committed label ID {0} to {1}", labelId, sDecPath));
|
|
|
|
// Create a new handler to read the labeled file metadata.
|
|
//Console.WriteLine(string.Format("Getting the label committed to file: {0}", sDecPath));
|
|
switch (nMode)
|
|
{
|
|
case 0:
|
|
Console.WriteLine(string.Format("복호화가 완료되었습니다 : {0}", sDecPath));
|
|
break;
|
|
case 1:
|
|
Console.WriteLine(string.Format("암호화가 완료되었습니다 : {0}", sDecPath));
|
|
break;
|
|
case 2:
|
|
options.FileName = options.OutputName;
|
|
var ContentLabel = action.GetLabel(options);
|
|
Console.WriteLine(string.Format("레이블이 지정 되었습니다 : {0}", ContentLabel.Label.Name));
|
|
break;
|
|
case 4:
|
|
Console.WriteLine(string.Format("레이블이 제거 되었습니다. : {0}", sDecPath));
|
|
break;
|
|
case 5:
|
|
Console.WriteLine(string.Format("소캠 복호화가 완료되었습니다 : {0}", sDecPath));
|
|
break;
|
|
}
|
|
|
|
// Update options to read the previously generated file output.
|
|
//options.FileName = options.OutputName;
|
|
|
|
// Read label from the previously labeled file.
|
|
//var contentLabel = action.GetLabel(options);
|
|
|
|
// Display the label with protection information.
|
|
//Console.WriteLine(string.Format("File Label: {0} \r\nIsProtected: {1}", contentLabel.Label.Name, contentLabel.IsProtectionAppliedFromLabel.ToString()));
|
|
}
|
|
|
|
else
|
|
{
|
|
//Console.WriteLine("No changes to file {0}", sEncPath);
|
|
switch (nMode)
|
|
{
|
|
case 0:
|
|
Console.WriteLine(string.Format("복호화 중 오류가 발생했습니다"));
|
|
break;
|
|
case 1:
|
|
Console.WriteLine(string.Format("AIP로 암호화 중 오류가 발생했습니다."));
|
|
break;
|
|
case 2:
|
|
Console.WriteLine(string.Format("레이블 처리 중 오류가 발생했습니다."));
|
|
break;
|
|
case 3:
|
|
Console.WriteLine(string.Format("레이블 지정 오류."));
|
|
break;
|
|
case 4:
|
|
Console.WriteLine(string.Format("레이블 제거 오류."));
|
|
break;
|
|
case 5:
|
|
Console.WriteLine(string.Format("소캠 복호화 중 오류가 발생했습니다."));
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Console.WriteLine("Fail .. " + ex.Message);
|
|
|
|
if (ex.InnerException != null)
|
|
Console.WriteLine("Fail .. 내부 예외 : " + ex.InnerException.Message);
|
|
}
|
|
|
|
#if DEBUG
|
|
Console.WriteLine("Press a key to quit.");
|
|
Console.WriteLine("아무키나 누르면 종료 됩니다.");
|
|
Console.ReadKey();
|
|
#endif
|
|
}
|
|
}
|
|
}
|