CS4.5修复C2Profile中本地时间不一致问题【增加convertDate】

admin 2022年9月20日17:30:01评论29 views字数 7162阅读23分52秒阅读模式

CS4.5修复C2Profile中本地时间不一致问题【增加convertDate】


△△△点击上方“蓝字”关注我们了解更多精彩




0x00 免责声明


在学习本文技术或工具使用前,请您务必审慎阅读、充分理解各条款内容。


1、本团队分享的任何类型技术、工具文章等文章仅面向合法授权的企业安全建设行为与个人学习行为,严禁任何组织或个人使用本团队技术或工具进行非法活动。


2、在使用本文相关工具及技术进行测试时,您应确保该行为符合当地的法律法规,并且已经取得了足够的授权。如您仅需要测试技术或工具的可行性,建议请自行搭建靶机环境,请勿对非授权目标进行扫描。


3、如您在使用本工具的过程中存在任何非法行为,您需自行承担相应后果,我们将不承担任何法律及连带责任。


4、本团队目前未发起任何对外公开培训项目和其他对外收费项目,严禁任何组织或个人使用本团队名义进行非法盈利。


5、本团队所有分享工具及技术文章,严禁不经过授权的公开分享。


如果发现上述禁止行为,我们将保留追究您法律责任的权利,并由您自身承担由禁止行为造成的任何后果。




0x01 前言

前段时间编写C2 Profile文件时,发现其中有一个compile_time参数,无法正常配置,仔细研究发现该参数确实存在一些问题。

C2 Profile中的compile_time参数值,需要填入本地化时间字符串C,在不同语言电脑上会产生时间转换错误和中文读取编码错误。


发现忘记填写convertDate()函数及其内容,为了不影响大家复现还是决定重发一下,感谢outman提示。




0x02 分析探讨

网上提供的C2配置demo表示需要如下格式的时间:
set compile_time  "14 Apr 2022 11:55:33";
但是填写上后本地CS服务端报错表示无法解析时间字符串。

进一步分析发现是由于CS中对这个参数的时间字符串解析格式使用的是:
SimpleDateFormat("dd MMM yyyy HH:mm:ss");
由于解析demo格式失败,因此有些不太明白这个到底需要什么格式,尝试获取当前系统时间,并以该格式进行输出:
now3: 14 八月 2022 11:55:33
源码Demo:
package 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);    }}
发现这个时间字符串中的MMM是本地化语言的月份,如果没有指定解析用的语言可能会导致解析错误。
英文月份:Apr中文月份:八月
测试发现本机Windows CS客户端运行时只可以解析中文时间字符串。
另外还发现中文参数的读取可能会由于读取文件时使用编码错误导致报错。
set compile_time   "14 八月 2022 11:55:33";
测试发现服务器Linux CS服务端运行时可以解析英文时间字符串。
set compile_time   "14 Apr 2022 11:55:33";
中途修复时发现,CS服务端和CS客户端都会使用这个时间字符串,如果CS服务端和CS客户端两个系统语言不同依旧会导致错误。

因此需要实现对输入的时间字符串进行动态的格式判断,并机器对应的本地化的语言格式去生成新的时间字符串和判断时间字符串格式。

对CS4.5逆向源码进行分析,发现两种结构主要是涉及文件及方法有:
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)



0x03 修复方案:
1、修改读取C2配置文件的编码选项。
2、对输入C2配置文件中compile_time参数时间字符串进行解析,动态替换为本地化时间。
3、对调用本地化时间字符串解析的函数进行一次猜测还原,再转化为本地化时间。

CS4.5修复C2Profile中本地时间不一致问题【增加convertDate】


CS4.5修复C2Profile中本地时间不一致问题【增加convertDate】


CS4.5修复C2Profile中本地时间不一致问题【增加convertDate】

修改后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";



0x04 修复代码c2profile/Loader.java
LoadProfile函数:修改
 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;      }   }
LoadProfile函数:修改
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);   }
LoadProfile函数3:新增
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; }}



0x05 修复后代码common/CommonUtils.java
getCoding函数: 新增函数,判断C2配置文件编码

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");}
getRawTime函数: 新增,正则判断是否中文,从而获取原始时间,
//新增 自动尝试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);}

convertDate函数:新增, 通过转换原始时间到指定时间格式
//修改新增时间转换函数,根据原始时间和新的时间格式生成新的时间public static final String convertDate(long rawtime, String newFormat){  SimpleDateFormat newSimpleDateFormat = new SimpleDateFormat(newFormat);  String newSimpleDate= newSimpleDateFormat.format(new Date(rawtime));  return newSimpleDate;}

isDate函数:修改, 通过getRawtime进行时间字符串判断
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;     }  }}
parseDate函数:修改, 通过getRawtime进行时间字符串转换
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);  }}




0x06 总结

本文解决了解决C2 Profile中的compile_time参数值,在不同语言电脑上会产生时间转换错误 和 C2 Profile中文参数读取错误问题。

END



如您有任何投稿、问题、建议、需求、合作、后台留言NOVASEC公众号!

CS4.5修复C2Profile中本地时间不一致问题【增加convertDate】

或添加NOVASEC-余生 以便于及时回复。

CS4.5修复C2Profile中本地时间不一致问题【增加convertDate】


感谢大哥们的对NOVASEC的支持点赞和关注

加入我们与萌新一起成长吧!


本团队任何技术及文件仅用于学习分享,请勿用于任何违法活动,感谢大家的支持!


原文始发于微信公众号(NOVASEC):CS4.5修复C2Profile中本地时间不一致问题【增加convertDate】

  • 左青龙
  • 微信扫一扫
  • weinxin
  • 右白虎
  • 微信扫一扫
  • weinxin
admin
  • 本文由 发表于 2022年9月20日17:30:01
  • 转载请保留本文链接(CN-SEC中文网:感谢原作者辛苦付出):
                   CS4.5修复C2Profile中本地时间不一致问题【增加convertDate】http://cn-sec.com/archives/1307260.html

发表评论

匿名网友 填写信息