Skip to content

Commit

Permalink
Fix CSV upcasing
Browse files Browse the repository at this point in the history
Skip empty lines in csv, more descriptive errors
  • Loading branch information
rvazarkar committed Sep 1, 2016
1 parent 3d3576b commit 5f8ef38
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 12 deletions.
20 changes: 11 additions & 9 deletions src/components/Menu/MenuContainer.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import MenuButton from './MenuButton';
import ProgressBarMenuButton from './ProgressBarMenuButton';
import { buildMergeQuery, defaultAjaxSettings } from 'utils';
import { If, Then, Else } from 'react-if';
const { dialog } = require('electron').remote
const { dialog, clipboard } = require('electron').remote
var fs = require('fs')

export default class MenuContainer extends Component {
Expand Down Expand Up @@ -111,6 +111,7 @@ export default class MenuContainer extends Component {
header: true,
dynamicTyping: true,
chunkSize: 25600,
skipEmptyLines: true,
chunk: function(rows, parser){
if (rows.data.length === 0){
parser.abort()
Expand All @@ -134,7 +135,15 @@ export default class MenuContainer extends Component {
options.url = appStore.databaseInfo.url + '/db/data/transaction/' + transactionID
//var data = JSON.stringify(buildMergeQuery(rows.data, filetype), null, 2)
options.data = JSON.stringify(buildMergeQuery(rows.data, filetype))
options.success = function(){
options.success = function(json){
if (json.errors.length > 0){
parser.abort()
fs.writeFile("error.log", JSON.stringify(json.errors, null, 2))
clipboard.writeText(JSON.stringify(json.errors, null, 2))
dialog.showErrorBox("Ingestion Error", "An error occurred in ingestion.\nIf possible please post the error.log file to a git issue (check for sensitive data first!) or contact cptjesus directly on the BloodHound Slack Channel.\nThe error has also been copied to your clipboard.")
this.cancelUpload()
return
}
completed += rows.data.length
sent += rows.data.length
this.setState({progress: Math.floor((completed / count) * 100)})
Expand All @@ -158,13 +167,6 @@ export default class MenuContainer extends Component {
parser.resume()
}
}.bind(this)
options.error = function(xhr, status, error){
if (xhr.statusText !== 'abort'){
console.log(xhr)
console.log(status)
console.log(error)
}
}
var a = $.ajax(options);
this.setState({currentAjax: a})
}.bind(this)
Expand Down
21 changes: 18 additions & 3 deletions src/js/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ export function escapeRegExp(s) {
return s.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\\\$&');
}

export function escapeSingleQuotes(s){
return s.replace("'", "\\'")
}

export function buildAuthHeader(){
db = storage.get('database-info', function(error, data){
return "Basic " + btoa(db.user + ":" + db.password)
Expand Down Expand Up @@ -356,9 +360,11 @@ export function buildMergeQuery(rows, type){
if (type === 'sessions'){
userQuery = "MERGE (user:User {name:'{}'}) WITH user MERGE (computer:Computer {name: '{}'}) WITH user,computer MERGE (computer)-[:HasSession {Weight : '{}'}]-(user)"
$.each(rows, function(i, row){
// if (row.UserName === 'ANONYMOUS LOGON' || row.UserName === ''){
// return
// }
if (row.UserName === 'ANONYMOUS LOGON@UNKNOWN' || row.UserName === ''){
return
}
row.UserName = row.UserName.toUpperCase()
row.ComputerName = escapeSingleQuotes(row.ComputerName.toUpperCase())
queries.statements.push({"statement": userQuery.format(row.UserName, row.ComputerName, row.Weight)})
})
}else if (type === 'groupmembership'){
Expand All @@ -367,6 +373,8 @@ export function buildMergeQuery(rows, type){
computerQuery = 'MERGE (computer:Computer {name: "{}"}) WITH computer MERGE (group:Group {name: "{}"}) with computer,group MERGE (computer)-[:MemberOf]-(group)'

$.each(rows, function(i, row){
row.AccountName = escapeSingleQuotes(row.AccountName.toUpperCase())
row.GroupName = escapeSingleQuotes(row.GroupName.toUpperCase())
switch(row.AccountType){
case 'user':
queries.statements.push({"statement": userQuery.format(row.AccountName, row.GroupName)})
Expand All @@ -385,6 +393,11 @@ export function buildMergeQuery(rows, type){
computerQuery = 'MERGE (computer1:Computer {name: "{}"}) WITH computer1 MERGE (computer2:Computer {name: "{}"}) WITH computer1,computer2 MERGE (computer1)-[:AdminTo]->(computer2)'

$.each(rows, function(i, row){
row.AccountName = escapeSingleQuotes(row.AccountName.toUpperCase())
row.ComputerName = escapeSingleQuotes(row.ComputerName.toUpperCase())
if (row.AccountName.startsWith('@')){
return
}
switch(row.AccountType){
case 'user':
queries.statements.push({"statement": userQuery.format(row.AccountName, row.ComputerName)})
Expand All @@ -400,6 +413,8 @@ export function buildMergeQuery(rows, type){
}else{
domainQuery = 'MERGE (domain1:Domain {name: "{}"}) WITH domain1 MERGE (domain2:Domain {name: "{}"}) WITH domain1,domain2 MERGE (domain1)-[:TrustedBy {TrustType : "{}", Transitive: "{}"}]->(domain2)'
$.each(rows, function(i, row){
row.TargetDomain = escapeSingleQuotes(row.TargetDomain.toUpperCase())
row.SourceDomain = escapeSingleQuotes(row.SourceDomain.toUpperCase())
switch(row.TrustDirection){
case 'Inbound':
queries.statements.push({"statement": domainQuery.format(row.TargetDomain, row.SourceDomain, row.TrustType, row.Transitive)})
Expand Down

0 comments on commit 5f8ef38

Please sign in to comment.