在学习本文技术或工具使用前,请您务必审慎阅读、充分理解各条款内容。
1、本团队分享的任何类型技术、工具文章等文章仅面向合法授权的企业安全建设行为与个人学习行为,严禁任何组织或个人使用本团队技术或工具进行非法活动。
2、在使用本文相关工具及技术进行测试时,您应确保该行为符合当地的法律法规,并且已经取得了足够的授权。如您仅需要测试技术或工具的可行性,建议请自行搭建靶机环境,请勿对非授权目标进行扫描。
3、如您在使用本工具的过程中存在任何非法行为,您需自行承担相应后果,我们将不承担任何法律及连带责任。
4、本团队目前未发起任何对外公开培训项目和其他对外收费项目,严禁任何组织或个人使用本团队名义进行非法盈利。
5、本团队所有分享工具及技术文章,严禁不经过授权的公开分享。
如果发现上述禁止行为,我们将保留追究您法律责任的权利,并由您自身承担由禁止行为造成的任何后果。
C2 Profile中的compile_time参数值,需要填入本地化时间字符串C,在不同语言电脑上会产生时间转换错误和中文读取编码错误。
发现忘记填写convertDate()函数及其内容,为了不影响大家复现还是决定重发一下,感谢outman提示。
set compile_time "14 Apr 2022 11:55:33";SimpleDateFormat("dd MMM yyyy HH:mm:ss");now3: 14 八月 2022 11:55:33package com.itheima.hello;import java.lang.String;import java.text.SimpleDateFormat;public static void main(String[] args) throws Exception {SimpleDateFormat df = new SimpleDateFormat("dd MMM yyyy HH:mm:ss");String now = df.format(System.currentTimeMillis());System.out.println("now: " + now);}}
英文月份:Apr中文月份:八月
set compile_time "14 八月 2022 11:55:33";set compile_time "14 Apr 2022 11:55:33";c2profile/Loader.javapublic static Profile LoadProfile(String fileName)public static Profile LoadProfile(String var0, InputStream var1)common/CommonUtils.javapublic static final long parseDate(String timeString, String timeStringFormat)public static final boolean isDate(String timeString, String timeStringFormat)
修改后C2Profile支持兼容以下文件编码格式:
UTF-8GBK #兼容GB2312等
修改后C2Profile compile_time 支持以下时间格式:
set compile_time "14 08 2022 11:55:33"; #建议set compile_time "14 Apr 2022 11:55:33";set compile_time "14 八月 2022 11:55:33";
public static Profile LoadProfile(String fileName) {try {File file = new File(fileName);// 修改 新增配置文件编码判断,使用 UTF-8 编码和 GBK 系统编码Charset fileCharset = CommonUtils.getCoding(file);CommonUtils.print_info("C2 Profile " + fileName +" Use File Charset: "+ "[" + fileCharset + "]");return LoadProfile(fileName, new FileInputStream(file), fileCharset);} catch (Exception e) {e.printStackTrace();return null;}}
public static Profile LoadProfile(String var0, InputStream var1) {Charset defaultCharset = Charset.defaultCharset();CommonUtils.print_info("C2 Profile " + var0 +" Use Default Charset: "+ "[" + defaultCharset + "]");return LoadProfile(var0, var1, defaultCharset);}
public static Profile LoadProfile(String file, InputStream inputStream, Charset charset) {try {//BufferedReader var2 = new BufferedReader(new InputStreamReader(var1));//修改配置文件读取编码格式BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, charset));StringBuffer stringBuffer = new StringBuffer();String line;while((line = reader.readLine()) != null) {//自动转换compile_time配置中的时间格式到目标系统格式if (line.split("#")[0].trim().indexOf("compile_time" ) !=-1) {String compile_time = line.split("#")[0].split("compile_time")[1].trim().replaceAll("[\";]", "");String mm_format = "dd MM yyyy HH:mm:ss";String mmm_format = "dd MMM yyyy HH:mm:ss";//获取用户输入日期的数据的原始时间long rawTime;rawTime = ( CommonUtils.isDate(compile_time, mm_format) ) ? CommonUtils.getRawTime(compile_time, mm_format) : CommonUtils.getRawTime(compile_time, mmm_format);if (rawTime > 0L) {//如果用户输入的时间可以解析,那就进行替换String new_compile_time = CommonUtils.convertDate(rawTime, mmm_format);line = line.replace(compile_time , new_compile_time);CommonUtils.print_info("C2 Profile " + file +" reset compile_time: "+ "[" + compile_time + "]" + " auto converted to: " +"[" + new_compile_time + "]");}else {CommonUtils.print_error("Function LoadProfile() Parse '" + compile_time + "' Use format '" + mmm_format + "' and '" + mm_format + "'");}}stringBuffer.append(line);stringBuffer.append('\n');}reader.close();Profile profile = new Profile();Loader loader = new Loader(file, stringBuffer.toString(), profile);loader.parse("");return loader.parser.hasErrors() ? null : profile;} catch (Exception exception) {exception.printStackTrace();return null;}}
public static Charset getCoding(File file) {try {byte[] buf = FileUtils.readFileToByteArray(file);CommonUtils ByteArrayUtil = null;//System.out.println(ByteArrayUtil.toHexString(buf));String fileContent = FileUtils.readFileToString(file, "UTF-8");//System.out.println(ByteArrayUtil.toHexString(filecCntent.getBytes("UTF-8")));if (ByteArrayUtil.toHexString(buf).equals(ByteArrayUtil.toHexString(fileContent.getBytes("UTF-8")))) {return Charset.forName("UTF-8");}} catch (IOException e) {e.printStackTrace();}return Charset.forName("GBK");}
//新增 自动尝试MMM的英语、中文从而获取原始时间, 支持显示错误消息开关public static final long getRawTime(String timeString, String timeStringFormat, Boolean ShowException) {//是否为中文格式Pattern pattern = Pattern.compile("[\u4e00-\u9fa5]");Matcher matcher = pattern.matcher(timeString);Locale language = (matcher.find()) ? Locale.CHINESE : Locale.ENGLISH;try {SimpleDateFormat date = new SimpleDateFormat(timeStringFormat, language);return date.parse(timeString).getTime();} catch (Exception exception) {if (ShowException){ MudgeSanity.logException("Function getRawTime() Could not parse '" + timeString + "' with '" + timeStringFormat + " ON " + language + "'", exception, false); }return 0L;}}//新增 自动尝试MMM的英语、中文从而获取原始时间public static final long getRawTime(String timeString, String timeStringFormat) {return getRawTime( timeString, timeStringFormat, true);}
//修改新增时间转换函数,根据原始时间和新的时间格式生成新的时间public static final String convertDate(long rawtime, String newFormat){SimpleDateFormat newSimpleDateFormat = new SimpleDateFormat(newFormat);String newSimpleDate= newSimpleDateFormat.format(new Date(rawtime));return newSimpleDate;}
public static final boolean isDate(String timeString, String timeStringFormat) {try {SimpleDateFormat simpleDateFormat = new SimpleDateFormat(timeStringFormat);simpleDateFormat.parse(timeString).getTime();return true;} catch (Exception exception) {//如果时MMM格式的时间解析错误,就进行一次自动解析, 不显示解析错误的消息if (timeStringFormat.indexOf("MMM" ) !=-1){print_info("Function isDate() isDate Trigger Auto Converted ON '" + timeString + "' with '" + timeStringFormat + "'");long rawTime = getRawTime(timeString, timeStringFormat, false);return rawTime != 0L;}else {return false;}}}
public static final long parseDate(String timeString, String timeStringFormat) {try {SimpleDateFormat simpleDateFormat = new SimpleDateFormat(timeStringFormat);return simpleDateFormat.parse(timeString).getTime();} catch (Exception exception) {//return 0L;// 客户端和服务的本地时间可能不一样,会导致解析失败,需要重新解析MMM格式的时间print_info("Function parseDate() Trigger Auto Converted ON'" + timeString + "' with '" + timeStringFormat + "'");return getRawTime(timeString, timeStringFormat);}}
END
如您有任何投稿、问题、建议、需求、合作、请后台留言NOVASEC公众号!
或添加NOVASEC-余生 以便于及时回复。
感谢大哥们的对NOVASEC的支持点赞和关注
加入我们与萌新一起成长吧!
本团队任何技术及文件仅用于学习分享,请勿用于任何违法活动,感谢大家的支持!!